How to edit multiple locations simultaneously in Vim

You may be looking for visual mode blockwise, which will allow insertion, deletion etc on several lines at once.

Blockwise mode will allow square selections with the column and line of the initial point in one corner, and the current cursor position defining the column and line of the other corner. This, as opposed to the line based selection that is the default.

CTRL-v will place you in blockwise visual mode.

If you have several lines like so:

INSERT INTO Users VALUES(1, 'Jim');
INSERT INTO Users VALUES(2, 'Jack');
INSERT INTO Users VALUES(3, 'Joseph');

And wanted to insert "0," after the id for each line, then place the cursor after the comma in the first line:

INSERT INTO Users VALUES(1,* 'Jim');

With the asterisk representing the cursor the command sequence would be:

CTRL-v  # Put into blockwise visual mode
j       # Down a line
j       # Down a line
CTRL-I  # Captial I for insert
0,      # the text to insert
Esc     # escape

The text should now look like:

INSERT INTO Users VALUES(1, 0, 'Jim');
INSERT INTO Users VALUES(2, 0, 'Jack');
INSERT INTO Users VALUES(3, 0, 'Joseph');

Also blockwise visual mode, x will delete a selection, y will yank it.

:help CTRL-V will give further documentation.


Here's how I would probably edit those particular lines (there are many ways):

/""<enter>
aText to replace...<esc>
n
.

First, search for the empty quotes to put the cursor on the first one. Using the "a" (append) command, type the new text to put inside the quotes. When you're done, use "n" (next) to go to the next instance, and "." (repeat last command) to insert the same text again. Repeat the "n ." as many times as necessary.

This method takes less up-front preparation and lets you get started right away without identifying ahead of time all the locations where you might want to add the text.

Tags:

Vim