sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #63: Applying substitutes to a visual block

 tip karma   Rating 155/45, Viewed by 8550 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   March 28, 2001 8:26      complexity:   intermediate
author:   Chip Campbell      as of Vim:   5.7

If you'd like to apply a substitute, or even any ex command, to a visual-block
selected text region (ctrl-v and move), then you'll want Stefan Roemer's
http://www.erols.com/astronaut/vim/vimscript/vis.vim .  Just source it in,
and then press ":B".  On the command line you'll see

:'<,'>BCtrl-V

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 Select..........|......Type
..................just the central.......|......:B s/abc/ABC/g
..................four "abc"s.................|
..................---------............|...-------------
..................abcabcabcabc............|......abcabcabcabc
..................abcabcabcabc............|......abcABCABCabc
..................abcabcabcabc............|......abcABCABCabc
..................abcabcabcabc............|......abcabcabcabc
(dots inserted to retain tabular format)

 rate this tip  Life Changing Helpful Unfulfilling 

<< Applying substitutes to a visual block | Always set your working directory to the file you're editing >>

Additional Notes

Anonymous, June 15, 2001 6:51
Sorry about the "Ctrl-V" in :'<,'>BCtrl-V, it shouldn't be there.
Also I tried to get the dots to line up, but proportional fonts won out.
--Chip Campbell
[email protected], February 15, 2002 20:59
I use the following pretty much the same, but no need to import a file...

Just the :g/ command after using V, Shift-V, or CTRL-V to select.

For example:

to change this line:
word one word two word three

to a vertical list, put the cursor at the start of the line, then
Shift-V
:g/ /s//CTRL-vCTRL-m/g <CR>

It's a global replace command. :g/
The first pattern to find is a space :g/ /
Now start a substitution :g/ /s//
Use CTRL-v followed by CTRL-m to compose a carriage return (<CR>)
Now finish the regex with a slash and 'g' for global :g/ /s//CTRL-vCTRL-m/g
and press enter.

Seems like a ridiculously large amount of stuff to remember but it's been 2nd nature for years now :)
I use it mostly to comment blocks when writing scripts until I found the b_v_I

try

:g/^/s//# /g

after selecting a block of text to comment out in a script.

OH MY GOD I JUST REMEMBERED THIS IS PART OF VI NOT VIM. hari kari





[email protected], February 28, 2002 7:41
I fear I may not have conveyed the idea properly.  Its not referring
merely to a block of lines but to a visual-block (ie. a subset of the
characters) and restricting the substitute to refer only to that visual
block.

I want to convert just the some of the central "cab" strings to CAB;
use ctrl-v and motion (hjkl or cursor keys) to select the "cab" strings
to be transformed via a visual-block selection:

                       to
                      ---->
    abcabcabcabcabc          abcabcabcabcabc
    abcabcabcabcabc          abcabCABcabcabc
    abcabcabcabcabc          abcabCABcabcabc
    abcabcabcabcabc          abcabCABcabcabc
    abcabcabcabcabc          abcabcabcabcabc

This operation has nothing much to do with converting a line to a
vertical list.  Often it is used to change a variable:

                           to
                          ---->
printf("...",                      printf("...",          
  abc[0],def[0],ghi[0],              abc[0],DEF[0],ghi[0],
  abc[1],def[1],ghi[1],              abc[1],DEF[1],ghi[1],
  abc[2],def[2],ghi[2],              abc[2],DEF[2],ghi[2],
  abc[3],def[3],ghi[3],              abc[3],DEF[3],ghi[3],

One could break this on the commas, [range]s/def/DEF/,
and rejoin, but that's a lot more work and less intuitive
than simply using ctrl-v to select a visual block and
doing the substitute just on that visual block.
[email protected], June 19, 2002 7:13
Vis.vim has been improved as of 6/19/2002 -- it is now immune to tabs and
is considerably faster.  Its a complete internal re-write, although its
interface has been retained.
[email protected] - NOSPAM, October 12, 2004 13:41
website address change: to http://mysite.verizon.net/astronaut/index.html#VimFuncs,
see "Visual Block Commands" and click on the link to get <vis.vim.gz>.  A help document
is provided and installed automatically.
[email protected] - NOSPAM, February 4, 2005 7:29
vis.vim is now also available as vimscript#1195
[email protected], October 20, 2006 7:25
Well in VIM70 just make make your selection in visual mode then type in your command starting with :
the '<,'> stuff is inserted automatically !
[email protected] - NOSPAM, January 25, 2007 6:57
Francky Teston -- please try your idea on the Feb28 note above's block of abcabcs...  .  Be sure to use ctrl-v to highlight the second block of cab, and apply  s/cab/CAB/ as shown in the example above.  You'll see that your idea capitalizes the *first* column of cab, not the *second*.  That's because the visual-block nature is ignored by the substitute command; it gets applied on a linewise basis.  Vim 7.0 does not change this behavior.  vis.vim allows one to select exactly where the substitute is to be applied on a visual block basis.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo