Vim Tips Wiki
Tip 63 Printable Monobook Previous Next

created 2001 · complexity intermediate · author Chip Campbell · version 6.0

If you'd like to apply a substitute, or even any Ex command, to a text region you've selected using visual-blocks (i.e. ctrl-v and move, or ctrl-q in Windows), then you'll be wanting to use Charles Campbell's vis.vim plugin.

Note that applying Ex commands to a simple visual selection is a much simpler process, as is searching in a visual selection (see Tip 438). The vis.vim plugin is useful for applying Ex commands to the area of a blockwise visual selection.

It's a plugin and comes as a vimball; so to install it with Vim 7.1 just

vim vis.vba.gz
:so %

Once installed:

ctrl-v (or ctrl-Q if using default mswin mappings) (move)
:B s/pattern/newtext/

You can also perform any Ex command (:B !sort, for example). On the command line, when you enter :B, what you'll actually see is:

:'<,'>B s/pattern/newtext/

Just continue with the substitute or whatever...

:'<,'>B s/abc/ABC/g

and the substitute will be applied to just that block of text!

Example: ctrl-v move-to-select the central four "abc"s

Initial           Select inner four "abc"s     :B s/abc/ABC/g
abcabcabcabc       abc--------abc              abcabcabcabc
abcabcabcabc       abc|abcabc|abc              abcABCABCabc
abcabcabcabc       abc|abcabc|abc              abcABCABCabc
abcabcabcabc       abc--------abc              abcabcabcabc

Vis.vim will also work with V (visual-line) and v (visual-character) selections.

Often it is used to change a variable. It's not the only way that the substitute can be done (you can use column selection regexp patterns), but it is, perhaps, more straightforward. As another example:

Initial                  After Substitute:
printf("...",            printf("...",
abc[0],def[0],def[0],    abc[0],def[0],DEF[0],
abc[1],def[1],def[1],    abc[1],def[1],DEF[1],
abc[2],def[2],def[2],    abc[2],def[2],DEF[2],
abc[3],def[3],def[3],    abc[3],def[3],DEF[3],

To do this, use ctrl-v and motion to select the defs, then apply the substitute:
:B s/def/DEF/


The def => DEF example is ill-chosen. If the variables are perfectly aligned, you can simply select all the 'def' variables and use S to replace the selected parts with 'DEF'.

There is no need of plugins:


Perhaps we need another example. The plugin is needed more so that changes don't take effect outside the visual area.

For example, just using /\%V in your pattern will change this (visual selection denoted by |):


into this:


when you do:


If you use the plugin, it becomes:


Note the difference in the last line.

Or at least, that's how I understand it. I don't use the plugin myself.

--Fritzophrenic 15:52, January 20, 2010 (UTC)

Correct. Note that in order to fix that particular example, you could add another "visual selection" match at the last matched character:
But this is not only difficult to remember, but soon becomes virtually impossible to specify with more complex patterns. -- Inkarkat 19:31, January 20, 2010 (UTC)
If you think about it a different way, you just need to wrap your pattern in %V:
But now you have to remember that it will never match against the right column of the selection. (%V just matches the selection, but if the i is the last character in the selection, then the second %V isn't inside the selection.) --Pydave 14:42, August 9, 2011 (UTC)

The original example works correctly for me in Vim 7.3-822 when using




Is this tip/plugin no longer needed? CStubing (talk) 18:53, March 10, 2013 (UTC)

See the comments above yours. /\%V only anchors the pattern at that specific location to be within the visual selection. You can still have matches for the pattern which start inside the visual selection but extend outside of it, and these will be matched by the substitute command without further finagling of the pattern adding more %V items. Plus the plugin allows using more commands than just :s. --Fritzophrenic (talk) 15:42, March 11, 2013 (UTC)

I've been a heavy vim user for 15 years now, and to me this has always seemed like a bug in vim. I don't understand why the *default* action of vim isn't to apply substitutions to the visually selected region when the 's' Ex command is run in visual mode. Applying the substitution to the whole line makes the visual selection meaningless. I don't understand why you would *want* it that way. I wish Bram would fix it.

I audibly grumble almost every time I try this and then remember it doesn't work the way it should.

Standard visual block commands[]

In the 90's I had a bit of time and added to the set of commands documented under:

:h blockwise-operators

Personally I use for example adding line comments to Perl see documentation under v_b_I


... which is "start of line, enter visual block, down a few lines, Insert #, ESC which inserts the change on all lines.

I recommend playing with the available commands before you resort to plugins, which are excellent but not necessarily required for most tasks.

Cheers, User:Allankelly-68