Vim Tips Wiki
No edit summary
 
(Uploaded by JohnBot from a locally edited file)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=63
 
|id=63
  +
|previous=58
|title=Applying substitutes to a visual block
 
  +
|next=64
|created=March 28, 2001 8:26
+
|created=March 28, 2001
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Chip Campbell
 
|author=Chip Campbell
 
|version=5.7
 
|version=5.7
 
|rating=151/44
 
|rating=151/44
|text=
 
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)
 
 
 
 
 
 
}}
 
}}
 
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
== Comments ==
 
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
 
   
 
:'<,'>BCtrl-V
   
 
Just continue with the substitute or whatever...
'''Anonymous'''
 
, June 15, 2001 6:51
 
----
 
I use the following pretty much the same, but no need to import a file...
 
   
 
:'<,'>B s/abc/ABC/g
Just the :g/ command after using V, Shift-V, or CTRL-V to select.
 
   
 
and the substitute will be applied to just that block of text!
For example:
 
   
  +
<pre>
to change this line:
 
 
Example: Ctrl-V Select..........|......Type
word one word two word three
 
 
..................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)
  +
</pre>
   
 
==Comments==
to a vertical list, put the cursor at the start of the line, then
 
 
Sorry about the "Ctrl-V" in :'&lt;,'&gt;BCtrl-V, it shouldn't be there.
Shift-V
 
 
Also I tried to get the dots to line up, but proportional fonts won out.
:g/ /s//CTRL-vCTRL-m/g &lt;CR&gt;
 
   
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 (&lt;CR&gt;)
 
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//&#35; /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
 
 
 
 
 
 
 
 
hsinclai--AT--speakeasy.net
 
, February 15, 2002 20:59
 
 
----
 
----
I fear I may not have conveyed the idea properly. Its not referring
+
I fear I may not have conveyed the idea properly. It's not referring
merely to a block of lines but to a visual-block (ie. a subset of the
+
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
+
characters) and restricting the substitute to refer only to that visual
block.
+
block.
   
I want to convert just the some of the central "cab" strings to CAB;
+
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
+
use ctrl-v and motion (hjkl or cursor keys) to select the "cab" strings
to be transformed via a visual-block selection:
+
to be transformed via a visual-block selection:
   
to
+
to
----&gt;
+
----&gt;
abcabcabcabcabc abcabcabcabcabc
+
abcabcabcabcabc abcabcabcabcabc
abcabcabcabcabc abcabCABcabcabc
+
abcabcabcabcabc abcabCABcabcabc
abcabcabcabcabc abcabCABcabcabc
+
abcabcabcabcabc abcabCABcabcabc
abcabcabcabcabc abcabCABcabcabc
+
abcabcabcabcabc abcabCABcabcabc
abcabcabcabcabc abcabcabcabcabc
+
abcabcabcabcabc abcabcabcabcabc
   
This operation has nothing much to do with converting a line to a
+
This operation has nothing much to do with converting a line to a
vertical list. Often it is used to change a variable:
+
vertical list. Often it is used to change a variable:
   
to
+
to
----&gt;
+
----&gt;
printf("...", printf("...",
+
printf("...", printf("...",
abc[0],def[0],ghi[0], abc[0],DEF[0],ghi[0],
+
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[1],def[1],ghi[1], abc[1],DEF[1],ghi[1],
abc[2],def[2],ghi[2], abc[2],DEF[2],ghi[2],
+
abc[2],def[2],ghi[2], abc[2],DEF[2],ghi[2],
abc[3],def[3],ghi[3], abc[3],DEF[3],ghi[3],
+
abc[3],def[3],ghi[3], abc[3],DEF[3],ghi[3],
   
One could break this on the commas, [range]s/def/DEF/,
+
One could break this on the commas, [range]s/def/DEF/,
and rejoin, but that's a lot more work and less intuitive
+
and rejoin, but that's a lot more work and less intuitive
than simply using ctrl-v to select a visual block and
+
than simply using ctrl-v to select a visual block and
doing the substitute just on that visual block.
+
doing the substitute just on that visual block.
   
 
cec--AT--NgrOyphSon.gPsfAc.nMasa.gov
 
, February 28, 2002 7:41
 
 
----
 
----
Vis.vim has been improved as of 6/19/2002 -- it is now immune to tabs and
+
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
+
is considerably faster. Its a complete internal re-write, although its
 
interface has been retained.
 
interface has been retained.
   
cec--AT--NgrOyphSon.gPsfAc.nMasa.gov
 
, June 19, 2002 7:13
 
----
 
website address change: to http://mysite.verizon.net/astronaut/index.html&#35;VimFuncs,
 
see "Visual Block Commands" and click on the link to get &lt;vis.vim.gz&gt;. A help document
 
is provided and installed automatically.
 
 
 
NdrOchip--AT--ScampbellPfamily.AbizM - NOSPAM
 
, October 12, 2004 13:41
 
----
 
vis.vim is now also available as [/scripts/script.php?script_id=1195 vimscript&#35;1195]
 
 
NdrOchip--AT--ScampbellPfamily.AbizM - NOSPAM
 
, February 4, 2005 7:29
 
 
----
 
----
 
website address change: to http://mysite.verizon.net/astronaut/index.html#VimFuncs,
Well in VIM70 just make make your selection in visual mode then type in your command starting with :
 
 
see "Visual Block Commands" and click on the link to get &lt;vis.vim.gz&gt;. A help document
the '&lt;,'&gt; stuff is inserted automatically !
 
 
is provided and installed automatically.
   
francky.teston--AT--caramail.com
 
, October 20, 2006 7:25
 
 
----
 
----
 
vis.vim is now also available as {{script|id=1195}}.
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.
 
   
NdrOchip--AT--ScampbellPfamily.AbizM - NOSPAM
 
, January 25, 2007 6:57
 
 
----
 
----
<!-- parsed by vimtips.py in 0.476554 seconds-->
 

Revision as of 10:32, 30 October 2007

Tip 63 Printable Monobook Previous Next

created March 28, 2001 · complexity intermediate · author Chip Campbell · version 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)

Comments

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.


I fear I may not have conveyed the idea properly. It's 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.


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.


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.


vis.vim is now also available as script#1195.