Vim: How to shrink the visual area - vim

Let's say that I've got the following text:
This allows you to select some text using Vim's visual mode
With the cursor on the 'a' in 'allows'. If I type vaw I switch to visual mode and then select "a word" ("allows "). If I repeat the aw then Vim also selects the "you ". Let's say I do this once more (highlighting "to ") and then realize that I've gone too far.
Is there a way to shrink/reduce the size of the visual area (other than using the h,j,k,l keys)?
I'd love to be able to either 'undo' my last command (i.e., have Vim move the selection back to before I typed that last 'aw') or else use the motion/text object commands to reduce the size of the visual area.

yes, you can keep pressing ge till it selecting allows again.

Based on your post (and on this one), I decided to make the vim-visual-history plugin that keeps
a traversable record of previous visual selections with [v, ]v, [V and ]V. So
in your case, you would just do [v to go back one selection. You can keep on pressing [v to go back further in time, and use ]v to go forwards again. The plugin works for your case, and also selections using text objects etc.
[count][v : Reselect previous visual selection
[count]]v : Reselect next visual selection
[V : Reselect first visual selection
]V : Reselect last visual selection
You can also give a count to jump through the history faster:

Related

vim copy/yank without leaving visual mode

In normal text edit after I select and copy, the selection stays still, so I can copy again quickly. But in vim when I press y in visual mode, I enter normal mode, so I need to select again. I want a quicker way.
Reasons:
When testing a console app, I copy test data from text editor and paste into console. Then I find bugs and edit code, the clipboard may change and I need to copy and paste test data to test again
You can reselect last visual region typing gv
*gv* *v_gv* *reselect-Visual*
gv Start Visual mode with the same area as the previous
area and the same mode.
In Visual mode the current and the previous Visual
area are exchanged.
After using "p" or "P" in Visual mode the text that
was put will be selected.
You do not need to be in visual mode in order to yank text. While in normal mode, the yank command can be combined with a motion: y{motion}. You can read more about that here: http://vim.wikia.com/wiki/VimTip312#Copying_and_cutting_in_normal_mode
It also sounds like you would like to yank text into the system clipboard. On most Linux distributions, you can do this by yanking text into the + register. Depending on the distro, you may need to install gvim.

How to turn off auto-indenting in Java in Android Studio (2.1.1)? (Windows 7)

If I have a line with 8 spaces (yes, spaces, not tabs) and some text...
x=3;
... and my cursor is placed after the semicolon, when I hit Enter it positions the cursor under the x. In other words it indents it 8 positions. If I then hit backspace it positions the cursor after the semicolon! I expect the backspace to take me back 1 space on the new line.
In Editor>Code Style > Java, Tab size = 1, Indent = 0, Continuation indent = 0. N.B. that if I try to set Tab size to 0 it's 1 next time I bring up that dialog, so 0 is apparently not acceptable to Android Studio, even though I see it suggested in several S.O. questions.
In Editor > Code Style I UNchecked Detect and use existing file indents for editing and now if I hit Enter with the cursor after the semicolon it positions the cursor 4 spaces in the from the right, in other words it still indents, just not as much.
N.B, that after making these settings I did a File > Invalidate Caches / Restart.
I don't want any automatic indenting. When I hit Enter I want it to position the cursor at the first position of the new line, and when I hit backspace I want it to go back 1 position, just like an ordinary "dumb" editor. How do I turn off all automatic indenting in Android Studio?
Android studio tries its best, like most IDEs, to help you organize your work and make it successful. It is normal, but of course, there is a way to disable it.
Note: Your project can be really messy when disabling this, if you want to properly indent a block of code, highlight it then press CTRL+ALT+L, this will indent the highlighted for you.
So, to disable it, press CTRL+ALT+S, this will open settings. Now, go to the Editor tab at your left, where you then go to General, then Smart Keys. You should see a "Backspace" part. Over there, there will be a dropdown menu, click that then select the "Disabled" option. (Settings>Editor>General>Smart Keys> )
Is this just the normal, expected behavior of A.S.?
I'd say it's the normal expected behavior of any IDE to indent correctly while you are typing. Your next line is supposed to start at the same position than the previous and if you close a bracket A.S. will automatically unindent it once.
Now on your topic :
To make your backspace key just delete the space you can go to : Settings -> Editor -> General -> Smart Keys -> Backspace -> Unindent and put 'Disabled' instead of 'To proper indent position'

Start Selection After Cursor in ViEmu Visual Mode

Ok, this was previously a question about Vim until I learned it was particular to the Visual Studio plugin ViEmu.
In ViEmu, 'v' puts the highlighter cursor between the previous character and the current one, such that walking backward leaves the letter that was under cursor when 'v' was pushed unselected. This is particularly annoying when trying to use visual mode from the end of the line. Is there a key that is to 'v' as 'a' vs. 'i' and 'p' vs 'P'.
Here's an example:
I have the following text with my cursor sitting over the trailing s of the word dances.
The fat yellow dog dances
and I wish to to change it to:
The quick brown fox jumps
I'd like to enter visual mode with the highlighter positioned to the right of the s in dances. That way, as I use shift+f to search backward to say the f in fat I select everything. Using v->shift+f->f will highlight everything but the s in dances which is annoying.
If we think about how a change made with an operator, say d3e, is different from the same change made using Visual mode, v3ed, we find that the distinctive element in Visual mode is that of interactivity.
In Visual mode it is natural to start selecting and then steadily honing in on the target area: Instead of v3ed I might as well use veeed or veeeebbed or v4ebbed. Or maybe after vee I realise I need to include stuff that comes before my selection, thus o, bb, and finally d.
The point is, when using
operators, we need to be precise: operator plus target – a hole-in-one shot;
Visual mode, we are free to employ the full array of Vim's motions to describe our target area, whatever shape it may be, and there's no pressure to be precise or hit the mark immediately.
In case you often need to start Visual mode just after the current cursor position (or you have another similar use case), you can always create a custom mapping. Here are some ideas:
:nnoremap <Leader>v <Space>v
:nnoremap <Leader>v $hv
:nnoremap <Leader>v $F;v

How to jump to the start or the end of visual selection in Vim?

Is there a motion for moving to the start or end of a visual selection?
I know that o while in visual mode alternates between the two, but I need to be able to select precisely the start.
The overall goal is to surround a visually selected area with parentheses.
Follow-Up:
Based on the comments, I was able to implement this using the following macro. The idea is to:
Esc to exit visual mode;
`> to go to the end of the previous visual selection;
a) to append a closing parentheses;
Esc to exit insert mode;
`< to go to the start of the previous visual selection;
i( to insert an opening parentheses;
Esc to exit insert mode again.
For example:
map \q <ESC>`>a)<ESC>`<i(<ESC>
Based on another comment, we have an even more concise solution:
map \q c()<ESC>P
There are two relevant built-in marks holding the positions of the first
and last characters of the last visual selection in the current buffer.
In order to move the cursor to these marks, use the commands `<
and `>, respectively (see :help `> and :help `<).
While you are in Visual Selection click o. It will change position of cursor to other end of selection. Then O to jump back.
The easiest way to "surround a visually selected area with parentheses" is:
change the visually selected area to () and Put it back in the middle: c()<ESC>P
I suggest defining in the .vimrc file a new visual-mode command (e.g., \q) with that:
:vmap \q c()<ESC>P
This approach also works with visual rectangular areas (<C-V>): it
puts ( and ) around each block-line.
if you just want to surround a visual selection there has already work been done, namely by tim pope, who wrote this plugin called surround. It surrounds words or visual selection with delimiters of your liking.
select your visual selection, say i like vim hit S) to get (i like vim) or S( to get ( i like vim ), to change this to [i like vim] type cs] (change surrounding) and to delete ds] to get i like vim at last.
If you can't use Surrond.vim, here is one way to do it:
Do your visual selection with v or V.
Get out of it with <Esc>.
Type `>a)<Esc> to insert a closing parenthesis after the last character of the selection.
Type `<i(<Esc> to insert an open parenthesis before the first character of the selection.

When is Visual mode used in Vim?

I'm relatively new to the world of Vim. I've been learning my way around it but have yet to find a practical purpose to enter visual mode.
What are some scenarios when visual mode is especially useful?
Are there actions that can only be performed from within visual mode?
I use visual mode when I want to highlight a section of text. I start by typing v in standard mode, which then enables the visual mode. Then I use the arrow keys to move the cursor. This causes the text between my starting point and the current cursor location to be highlighted. Once you select a section of text like this, entering a command (e.g. search/replace) in command mode (by typing :) will only affect the selected area.
Another useful visual command is shift+v (visual line). This does the same as above, but it selects entire lines at a time instead of individual characters.
When you want to comment a block of text.
In command mode :
Shift + v
,ctrl +v,
j or k,
I , #(comment
character) and then Esc
Vim inserts the comment character to
the start of the block..
is when I am using Gvim, I find it much easier to copy data
to the clipboard through visual mode.
In command mode :
Shift + v
, j or k ,
" , +
,y
Here + is the clipboard
register
This to me is much more legible that using markers
is for manual indenting
Shift + v,
Shift + > for
moving to the right.
Shift + < for
moving to the left. .
repeats
this is fun :-)
One of the nice things about visual mode is that, because of Vim's focus on modality, you can perform most of the commands that you are used to (such as search/replace with :s, d to delete text, or r to replace text) while also seeing exactly what will be affected -- this allows you to determine the exact scope of whatever you are doing.
Furthermore, as someone else mentioned, you can easily insert a prefix (like a comment character or, say, & for alignment or \item in LaTeX) by selecting the first character of each line in visual block mode (ctrl+v), pressing I to insert before the first character, typing in whatever you want to insert, and then Escing back to normal mode.
The last kind of visual mode is visual line (Shift+v), which allows you to quickly select a number of lines. From there, you can change their indentation using > or < (prefix this with a number to indent by that many tabs), use d or y to delete or copy those lines, use zf to create a new fold from those lines, or use any other selection-based command.
Finally, there are a lot of other cool things you can do with visual mode, including gv to reselect your last visual[line/block] mode selection, gU to convert a visual selection to uppercase or gu for lowercase, and many more.
In addition to the other (great) answers, it's an easy way to define scope for an action. For example, to limit a search & replace to a specific method...
Say you have this code:
function foo() {
abc();
while (1) {
def();
abc();
}
}
You can place the cursor on any of the braces or parentheses and press v, %, :, s/abc/xyz/g and your search & replace will have a defined scope in which the action will occur.
Visual mode is useful if you want to apply a command to a section of text that isn't easily described as a primitive movement command. You can select some text in visual mode with a complex sequence of movements and then apply a command to that selection.
I often find myself using visual-block mode (Ctrl + v) more than any of the other visual modes.
You can easily remove indentation, comments, etc. once you are aware of this mode. In my experience, this is often faster than figuring out how to form an equivalent search-and-delete statement.
You can also add indentation (or comments as Cherian stated) by selecting a block of text and pressing I, typing whatever you want to add, and pressing Esc (note: you may need to redraw the screen (e.g. by moving the cursor) to see the effects of this).
I haven't seen the following mentioned, probably because they're subtle.
1 - Less hassle with the unnamed register
Whenever you copy (yank) some text, and then want to d to change some other text, e.g., diw to "delete inner word," Vim will place the deleted text into the unnamed register. Then if you try to paste, it will just paste the deleted text right back unless you do "0p to paste from the 0 register.
But with visual mode, you can just do something like viwp and don't have to mess with registers.
So, for comparison, to copy and replace inside some parens:
yiw -> move somewhere -> vi(p
vs
yiw -> move -> ci(<C-r>0p
yiw -> move -> "_di(p
yiw -> move -> di("0P
Note: this also works for deleting text and pasting it back over a text object. See here.
2 - Jumping to parts of a text object
If you want to jump to the beginning or end of a text object, you can select it in visual mode and press o. For example, va" to select anywhere inside quotes, and then press o to jump to the matching quotes, kind of like % for matching brackets.

Resources