How to jump to the start or the end of visual selection in Vim? - 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.

Related

Vim: Select all without scrolling away

is it possible in vim to select all lines in the current file, but leave the position where my cursor is unchanged?
Let's say I am currently at line 500 (of 3000) and want to quickly select everything (not yank), as my selection is simply set up to show whitespace characters. Can this be done without leaving my current line?
To achieve exactly what you like, you can press the following:
ggVG<Esc><Ctrl-O><Ctrl-O>
gg moves to the beginning of the file
V starts visual line mode
G moves to the and of the file (now you have selected the whole
file)
<Esc> leaves visual mode
<Ctrl-O> moves your cursor back to the prevois location (first to the beginning of the file, then the second time to your last position before pressing gg)
And if you like to select only the visible lines in you window (to not scroll away). You can use HVL instead of ggVG (H moves to the top of your window and L to the bottom).
You also could show whitespaces without using visual selection with something like this in your .vimrc:
set list listchars=tab:»·,trail:·,nbsp:·
This helps me to detect trailing whitespaces, and mixed (spaces/tabs) indentation.
usually pressing
ggVG
in normal mode will select all the lines, but it will leave your cursor at the last line of the file.
If you wants to highlights the whitespace characters then you can highlight this by using the below command in command mode (this white color chosen is for dark theme)
: hi ExtraWhitespace ctermbg=White guibg=White
Depending on what you are trying to achieve you can use something like :
%cmd
To apply the command to the whole file.
For example, %y will yank the whole file, %=will format the whole file, without moving your cursor. It does not really work if you do something like %d...
It is not a real selection though but rather a way to apply a command on the whole file.
To go further you can use something like
%norm Atest
To add 'test' at the end of each line. (Actually this is a bad example, because this command will move to the last line...)
It is not possible to have the cursor inside a visual selection. This caused by that, vim defines visual selection through two marks. As soon as you move the cursor one of the marks gets updated. Basically this means one of the marks is always lays where the cursor is(at least when using "v" to select). You cannot have the border in the middle of the region that the border defines :)

Vim: How to shrink the visual area

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:

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

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.

Configure Macvim's text selection to not include character under cursor

Using macvim, when I copy a text selection, it always includes the character under the cursor.
For example, if the cursor is at the far left and I press shift-down arrow, it selects the entire line plus the first character of the next line (since the cursor is sitting over the next line's first character).
Is there a way to configure macvim to not include the cursor character in text selections?
Take a look at the selection option. By default it's set to inclusive, but you can change it to exclusive to make text selections act the way you want:
:set selection=exclusive
You can also set it to exclusive with the behave command:
:behave mswin
This also sets several other options, however, which may or may not be what you want. See the Vim help for the specifics.
:help :behave
:help 'selection'
I am guessing that shift-down arrow activates visual character mode, and moves the cursor down a line. If you are trying to select entire lines, you would be better off using visual line mode, which is activated from normal mode by pressing V (shift-v). This will select the current line in its entirety. You can then extend your selection to include the lines above and below using the k (or up arrow) and j (or down arrow) keys.
When using Vim, I think it is better to go with the grain rather than to fight against it. Don't expect it to work the same way as other text editors. Accept that the Vim way is different.

Resources