Vim Tips Wiki
Tags: Visual edit apiedit
No edit summary
Tag: sourceedit
 
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=81
 
|id=81
Line 7: Line 6:
 
|complexity=basic
 
|complexity=basic
 
|author=
 
|author=
|version=5.7
+
|version=6.0
 
|rating=99/48
 
|rating=99/48
 
|category1=
 
|category1=
 
|category2=
 
|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 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 <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.
 
  +
  +
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>".
   
If you need to subsitute three lines of the text file, simply type <code>3S</code>. 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==

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.