Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit multiple locations simultaneously in Vim

Tags:

vim

In certain text editors, like E, I can select multiple locations and, as I type, all the selected locations get replaced with the characters I am typing.

For example if I have:

<tag1 class=""></tag1>
<tag2><tag3 class=""></tag3></tag2>

In E I could select two locations inside sets of quotations marks then start typing and both locations would be updated simulataneously. In Vim, I can select several connected columns at once and then edit them, but I'm wondering if there is any way to select multiple locations that aren't lined up.

like image 225
Daniel Avatar asked Sep 05 '09 20:09

Daniel


People also ask

How do you edit multiple files in vi?

Invoking vi on Multiple Files Open the two files practice and note. The first-named file, practice, appears on your screen. Perform any edits. Save the edited file practice with the ex command w .

How do you edit multiple files in Unix?

You can type :wn both to save the current file changes and to go to the next file. Typing :q! discards changes and closes the current file. Type vi * to edit all the files in a directory, though this will give you an error in some Unix systems.


4 Answers

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.

like image 111
Greg Hewgill Avatar answered Oct 17 '22 07:10

Greg Hewgill


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.

like image 31
John Kane Avatar answered Oct 17 '22 08:10

John Kane


Have a look at SnippetsEmu. It should be doing something very similar to what you need.

It emulates TextMates snippets. You should be able to have one snippet with the same tag repeated, and editing will do the right thing, updating the same tag in all locations, as you type.

like image 41
Ayman Avatar answered Oct 17 '22 08:10

Ayman


For your example I would use a substitution:

:%s/class=""/class="something"/g
like image 32
bjelli Avatar answered Oct 17 '22 07:10

bjelli