Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I perform multiple actions on a visual-block selection in Vim?

I'm using GVIM on Windows, if it matters.

I often select a block, do something, and then need to do something else with the same block, but of course once I do anything with the block, I'm out of visual mode.

Is there a way to re-select or act on the previously selected visual block?

like image 671
Jay Avatar asked Jan 20 '11 21:01

Jay


People also ask

How do I select a visual block in Vim?

Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block. Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement. Press d (delete) to cut, or y (yank) to copy.

What does visual mode do in Vim?

Visual mode makes it easier to highlight and manipulate text in Vim. Ansible playbook files are text files in a YAML format. People who work regularly with them have their favorite editors and plugin extensions to make the formatting easier.

How do you use visual mode?

To use visual mode operators, first visually select an area of text ( v/V/Ctrl-v + motion), then press a visual-mode operator key. That's it.

How do I select vertically in Vim?

Use ctrl+v to select a column of text consisting of the first character of the place you want your new column to go in front of. Use I to go into insert mode, and type one space. Press Esc, and you'll see you have inserted a column of single spaces. Now use ctrl+v again to highlight the column of spaces.


3 Answers

I believe gv will reselect the previous block...

like image 184
Joe Kington Avatar answered Oct 02 '22 21:10

Joe Kington


gv as Joe pointed out does the trick (+1), but an extra tip as well is if you do a :s with a visual selection, it will automatically populate the marks '< and '> and those will persist until you make another visual selection. So, you can do :'<,'>s/foo/bar/ without having to go back into visual mode and it will still apply to the same range. Same thing with anything else that uses those marks.

like image 26
Daniel DiPaolo Avatar answered Oct 02 '22 22:10

Daniel DiPaolo


If you have just pasted a visual block, there is also a way to reselect it easily.

See the following question and answers on SO.

like image 33
Xavier T. Avatar answered Oct 02 '22 20:10

Xavier T.