14.06.2014 Views

vim 101 hacks

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Buyer: xiujuan lu (luxiujuan@gmail.com)<br />

Transaction ID: 85W16630AD8515549<br />

Vim <strong>101</strong> Hacks<br />

www.thegeekstuff.com<br />

:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]<br />

The following are three possible substitution flags.<br />

• [c] Confirm each substitution.<br />

• [g] Replace all occurrences in the line.<br />

• [i] Ignore case for the pattern.<br />

Example 1: Substitute all occurrences of a text with another text in<br />

the whole file<br />

This is a very common usage of the text substitution feature inside Vim. When<br />

you want a specific text to be replaced with another text everywhere in the<br />

entire file then you can use the following sequence.<br />

:%s/old-text/new-text/g<br />

• %s – specifies all lines. Specifying the range as ‘%’ means do<br />

substitution in the entire file.<br />

• g – specifies all occurrences in the line. If this ‘g’ flag is not used then<br />

only first occurrence in the line only will be substituted.<br />

Example 2: Substitution of a text with another text within a single<br />

line<br />

Lack of range specification means do substitution on the current line only.<br />

With the ‘i’ flag, you specify case insensitive searching.<br />

:s/helo/Hello/gi<br />

130

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!