Vim Tips Wiki
(adding category)
(Fix URL of plugin + couple of typos + standard pre)
Line 9: Line 9:
 
|rating=151/44
 
|rating=151/44
 
}}
 
}}
  +
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, available at http://mysite.verizon.net/astronaut/vim/index.html#VIS
   
 
It's a plugin and comes as a vimball; so to install it with Vim 7.1 just
If you'd like to apply a substitute, or even any ex command, to a text region you've
 
  +
<pre>
selected using visual-blocks (ie. ctrl-v and move), then you'll be wanting to use
 
 
vim vis.vba.gz
Charles Campbell's ''vis.vim'' plugin, available at
 
 
:so %
 
 
:q
http://vim.sf.net/astronaut/vim/index.html#VIS
 
  +
</pre>
 
Its a plugin and comes as a vimball; so to install it with vim 7.1 just
 
<code><pre>
 
vim vis.vba.gz
 
:so %
 
:q
 
</pre></code>
 
   
 
Once installed:
 
Once installed:
  +
<pre>
 
ctrl-v (move)
 
:B s/pattern/newtext/
  +
</pre>
   
 
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:
<code><pre>
 
  +
<pre>
ctrl-v (move)
 
:B s/pattern/newtext/
+
:'&lt;,'&gt;B s/pattern/newtext/
</pre></code>
+
</pre>
 
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:
 
 
<code><pre>
 
:'&lt;,'&gt;B s/pattern/newtext/
 
</pre></code>
 
   
 
Just continue with the substitute or whatever...
 
Just continue with the substitute or whatever...
  +
<pre>
 
:'&lt;,'&gt;B s/abc/ABC/g
+
:'&lt;,'&gt;B s/abc/ABC/g
  +
</pre>
   
 
and the substitute will be applied to just that block of text!
 
and the substitute will be applied to just that block of text!
   
<code><pre>
+
<pre>
 
Example: ctrl-v move-to-select the central four "abc"s
Example:
 
ctrl-v move-to-select the central four "abc"s
 
   
Initial Select inner four "abc"s :B s/abc/ABC/g
+
Initial Select inner four "abc"s :B s/abc/ABC/g
abcabcabcabc ab --------abc abcabcabcabc
+
abcabcabcabc abc--------abc abcabcabcabc
abcabcabcabc abc|abcabc|abc abcABCABCabc
+
abcabcabcabc abc|abcabc|abc abcABCABCabc
abcabcabcabc abc|abcabc|abc abcABCABCabc
+
abcabcabcabc abc|abcabc|abc abcABCABCabc
abcabcabcabc ab --------abc abcabcabcabc
+
abcabcabcabc abc--------abc abcabcabcabc
</pre></code>
+
</pre>
   
Vis.vim will also work with V (visual-line) and v (visual-character)
+
Vis.vim will also work with V (visual-line) and v (visual-character) selections.
selections.
 
   
Often it is used to change a variable. Its not the only way that
+
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:
the substitute can be done (you can use column selection regexp
 
patterns), but it is, perhaps, more straightforward. As another
 
example:
 
   
<code><pre>
+
<pre>
Initial After Substitute:
+
Initial After Substitute:
printf("...", printf("...",
+
printf("...", printf("...",
abc[0],def[0],def[0], abc[0],def[0],DEF[0],
+
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[1],def[1],def[1], abc[1],def[1],DEF[1],
abc[2],def[2],def[2], abc[2],def[2],DEF[2],
+
abc[2],def[2],def[2], abc[2],def[2],DEF[2],
abc[3],def[3],def[3], abc[3],def[3],DEF[3],
+
abc[3],def[3],def[3], abc[3],def[3],DEF[3],
</pre></code>
+
</pre>
   
To do this, use ctrl-v and motion to select the ''def''s, then apply the substitute: :B s/def/DEF/
+
To do this, use ctrl-v and motion to select the ''def''s, then apply the substitute:<br>
  +
<tt>:B s/def/DEF/</tt>
   
 
==Comments==
 
==Comments==
   
  +
----
 
 
[[Category:Plugin]]
 
[[Category:Plugin]]

Revision as of 09:06, 11 December 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 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, available at http://mysite.verizon.net/astronaut/vim/index.html#VIS

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

vim vis.vba.gz
:so %
:q

Once installed:

ctrl-v (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/

Comments