Vim Tips Wiki
(Uploaded by JohnBot from a locally edited file)
No edit summary
Tag: sourceedit
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=81
 
|id=81
 
|previous=80
 
|previous=80
 
|next=82
 
|next=82
|created=June 21, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
|author=Anon
+
|author=
|version=5.7
+
|version=6.0
 
|rating=99/48
 
|rating=99/48
  +
|category1=
  +
|category2=
 
}}
 
}}
  +
The <code>s</code> (substitute) command can be used to replace specified characters, and the <code>S</code> command can be used to replace specified lines.
I was just editing a file that contained the same leading string on many lines.
 
   
 
For example, the following has the same leading string on several lines:
 
<pre>
 
<pre>
foo_bar_baz1=a
+
foo_bar_baz1 = a1
  +
foo_bar_baz2 = a2
foo_bar_baz1=abc674
 
  +
foo_bar_baz3 = a3
foo_bar_baz1=qrs
 
  +
foo_bar_baz4 = a4
foo_bar_baz1=m1
 
  +
foo_bar_baz5 = a5
foo_bar_baz1=bz90
 
  +
foo_bar_baz6 = a6
foo_bar_baz1=bc
 
 
</pre>
 
</pre>
   
  +
Put the cursor on the <code>f</code> at the start of the first line and type the following: <code>7s</code> then <code>fixed</code> then press Esc. That changes <code>foo_bar_baz1</code> to <code>fixed_baz1</code>. Move the cursor to the start of the next line and press <code>.</code> to repeat the command.
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.
 
   
  +
A single character can be replaced with the <code>r</code> command. For example, with the cursor on "f", type <code>rt</code> to replace the "f" with "t". That only works with characters that can be entered with a single key press, and <code>s</code> can be used to for a replacement with a digraph. For example, put the cursor on the "x" in text "12x10<sup>6</sup>" then press <code>s</code> followed by Ctrl-K <code>*X</code> to change the text to "12×10<sup>6</sup>".
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.
 
  +
  +
If you need to substitute three lines of text, type <code>3S</code>. Vim deletes the three lines and enters insert mode, waiting for the subsitution text.
   
 
==Comments==
 
==Comments==
Line 29: Line 33:
   
 
----
 
----
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.
   
 
----
 
----

Latest revision as of 04:18, 8 August 2015

Tip 81 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0


The s (substitute) command can be used to replace specified characters, and the S command can be used to replace specified lines.

For example, the following has the same leading string on several lines:

foo_bar_baz1 = a1
foo_bar_baz2 = a2
foo_bar_baz3 = a3
foo_bar_baz4 = a4
foo_bar_baz5 = a5
foo_bar_baz6 = a6

Put the cursor on the f at the start of the first line and type the following: 7s then fixed then press Esc. That changes foo_bar_baz1 to fixed_baz1. Move the cursor to the start of the next line and press . to repeat the command.

A single character can be replaced with the r command. For example, with the cursor on "f", type rt to replace the "f" with "t". That only works with characters that can be entered with a single key press, and s can be used to for a replacement with a digraph. For example, put the cursor on the "x" in text "12x106" then press s followed by Ctrl-K *X to change the text to "12×106".

If you need to substitute three lines of text, type 3S. Vim deletes the three lines and enters 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.