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 />

Example 6. Substitute only whole words and not partial matches<br />

Let us assume that you want to change only the whole word ‘his’ to ‘her’ in<br />

the original text mentioned below. If you do the standard substitution, apart<br />

from changing his to her, it will also change This to Ther as shown below.<br />

Standard Substitution:<br />

Original Text: This is his idea<br />

:s/his/her/g<br />

Translated Text: Ther is her idea<br />

Whole Word Substitution:<br />

Original Text: This is his idea<br />

:s/\/her/<br />

Translated Text: This is her idea<br />

Note: You should enclose the word with < and > , which will force the<br />

substitution to search only for the full word and not any partial match.<br />

Note 2: novices sometimes use spaces instead of < and >, not realizing this<br />

won't match words at the beginnings and ends of lines or words with<br />

proximate punctuation.<br />

Example 7. Substitute either word1 or word2 with a new word using<br />

a regular expression<br />

In the following example, Vim will replace any occurrences of either good or<br />

nice with awesome.<br />

132

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

Saved successfully!

Ooh no, something went wrong!