tags:

views:

189

answers:

4

In certain text editors like e, you can select multiple locations and as you type, all the selected locations get replaced with the characters you are typing.

For example let's say you have

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

In e, you could just select two locations (inside sets of quotations marks), then start typing and both locations would be updated simulataneously. In vim, you 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.

+1  A: 

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.

John Kane
The limitation with CTRL-v is that it only does contiguous blocks, if the text you want to edit isn't lined up nicely in a column, you can't use it!
Daniel
Ah, in that case you have me. There is a multiselect plugin, but I have never used it:http://www.vim.org/scripts/script.php?script_id=953
John Kane
+4  A: 

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.

Greg Hewgill
That definitely a good way to get text around quickly. However, that isn't an example of simultaneous editing in the same way that e does it. Perhaps this is the 'vim' way of doing things (and as you indicate has the advantage that you don't need to know all places ahead of time).
Daniel
Vim can't do simultaneous edits (possible it can be emulated through vimscript). Setting up a repeatable command is a bit more work IMO, but it's the way to do it Vim.
Svend
+2  A: 

For your example I would use a substitution:

:%s/class=""/class="something"/g
bjelli
Thanks for the answer! However, my question is not about finding any solution to the admittedly contrived example but actually about solving the example using simultaneous editing.
Daniel
+1  A: 

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.

Ayman
It turns out that Ayman you are exactly correct! There is also another script called snipMate that does this as well. See the screen cast referenced here: http://www.vim.org/scripts/script.php?script_id=2540. It is almost identical to the screencast features demoed in e.
Daniel