Vim Tips Wiki
m (Substitution of characters and lines in VIM is made far easier with the s and S commands moved to Substitute characters and lines easily: Page moved by JohnBot to improve title)
(Change <tt> to <code>, perhaps also minor tweak.)
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=81
 
|id=81
  +
|previous=80
|title=Substitution of characters and lines in VIM is made far easier with the s and S commands
 
  +
|next=82
|created=June 21, 2001 14:41
+
|created=2001
 
|complexity=basic
 
|complexity=basic
|author=Substitue characters and lines
+
|author=
 
|version=5.7
 
|version=5.7
 
|rating=99/48
 
|rating=99/48
  +
|category1=
|text=
 
  +
|category2=
Substitute Characters
 
 
}}
 
I was editing a file that contained the same leading string on many lines:
  +
<pre>
 
foo_bar_baz1=a
 
foo_bar_baz1=abc674
 
foo_bar_baz1=qrs
 
foo_bar_baz1=m1
 
foo_bar_baz1=bz90
 
foo_bar_baz1=bc
  +
</pre>
   
 
Needing to only substitute a portion of the string, I referred to a Vim reference card and discovered a command answering my need exactly. The <code>s</code> command is used to subsitute a certain number of characters. In my example file above, if I only needed to subsititute the characters foo_bar, I set the cursor on the first character where I'd like the subsitution to begin and type <code>7s</code>. Vim drops the characters foo_bar and goes to insert mode, waiting for the substitution text.
-----------------------------------
 
   
 
If you need to subsitute three lines of text, simply type <code>3S</code>. Vim drops the three lines and goes into insert mode, waiting for the subsitution text.
I was just editing a file that contained the same leading string on many lines.
 
 
 
 
example:
 
 
 
 
foo_bar_baz1=a
 
 
foo_bar_baz1=abc674
 
 
foo_bar_baz1=qrs
 
 
foo_bar_baz1=m1
 
 
foo_bar_baz1=bz90
 
 
foo_bar_baz1=bc
 
 
...
 
 
 
 
Needing to only substitute a portion of the string, I referred to a VIM reference card and discovered a command answering my need exactly. The s command is used to subsitute a certain number of characters. In my example file above, if I only needed to subsititute the characters foo_bar, I set the cursor on the first character where I'd like the subsitution to begin and type 7s. VIM drops the characters foo_bar and goes to insert mode, waiting for the substitution text.
 
 
 
 
Substitute Lines
 
 
-----------------------
 
 
After years of using vi and VIM and always deleting multiple lines in order to replace them, I just discovered the S command. If you need to subsitute three lines of text, simply type 3S. VIM drops the three lines and goes into insert mode, waiting for the subsitution text.
 
}}
 
   
== Comments ==
+
==Comments==
 
Of course, if you're lazy like me and you don't want to count that there are 7 characters that you want to replace, you could use the c command and use a motion to specify how much to kill. For example, in the previous example, I'd type c2t_ to kill foo_bar and to be left in insert mode.
 
Of course, if you're lazy like me and you don't want to count that there are 7 characters that you want to replace, you could use the c command and use a motion to specify how much to kill. For example, in the previous example, I'd type c2t_ to kill foo_bar and to be left in insert mode.
   
leifw at bigfoot dot com
 
, July 11, 2001 14:12
 
 
----
 
----
You could have selected the "foo_bar" characters with Ctrl+V (Visual Block), pressed "c" to change the block, made your change to the first line, press ESC and, voil�, all the lines you selected will be changed the same way.
+
You could select the "foo_bar" characters with Ctrl+V (Visual Block &ndash; Ctrl+Q on Windows), press "c" to change the block, make your change to the first line, and press Esc. All the lines you selected will be changed the same way.
   
bdr at users.sourceforge.net
 
, August 29, 2001 8:11
 
 
----
 
----
<!-- parsed by vimtips.py in 0.664127 seconds-->
 

Revision as of 05:12, 13 July 2012

Tip 81 Printable Monobook Previous Next

created 2001 · complexity basic · version 5.7


I was editing a file that contained the same leading string on many lines:

foo_bar_baz1=a
foo_bar_baz1=abc674
foo_bar_baz1=qrs
foo_bar_baz1=m1
foo_bar_baz1=bz90
foo_bar_baz1=bc

Needing to only substitute a portion of the string, I referred to a Vim reference card and discovered a command answering my need exactly. The s command is used to subsitute a certain number of characters. In my example file above, if I only needed to subsititute the characters foo_bar, I set the cursor on the first character where I'd like the subsitution to begin and type 7s. Vim drops the characters foo_bar and goes to insert mode, waiting for the substitution text.

If you need to subsitute three lines of text, simply type 3S. Vim drops the three lines and goes into insert mode, waiting for the subsitution text.

Comments

Of course, if you're lazy like me and you don't want to count that there are 7 characters that you want to replace, you could use the c command and use a motion to specify how much to kill. For example, in the previous example, I'd type c2t_ to kill foo_bar and to be left in insert mode.


You could select the "foo_bar" characters with Ctrl+V (Visual Block – Ctrl+Q on Windows), press "c" to change the block, make your change to the first line, and press Esc. All the lines you selected will be changed the same way.