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

• n – Will not replace the current highlighted word. But it will<br />

automatically highlight the next word that matched the search<br />

pattern<br />

• a – Will substitute all of the remaining matches without further<br />

prompting.<br />

• l – This will replace only the current highlighted word and terminate<br />

the find and replace effort.<br />

Example 9. Prepending every line with its line number<br />

When the replacement string starts with ‘\=’, it should be evaluated as an<br />

expression. Using the ‘line’ function we can get the current line number.<br />

:%s/^/\=line(".") . ". "/g<br />

Note: This is different from “:set number” which does not write the line<br />

numbers into the file. When you use this substitution you are making these<br />

line numbers part of the actual content of the file.<br />

Example 10. Substituting a special character with an equivalent<br />

value.<br />

Substituting the ~ character with the value of the $HOME variable.<br />

Original Text: Current file path is ~/test/<br />

:%s!\~!\= expand($HOME)!g<br />

Translated Text: Current file path is<br />

/home/ramesh/test/<br />

Notes:<br />

• You can use the expand function to expand any available predefined<br />

or user defined variables.<br />

134

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

Saved successfully!

Ooh no, something went wrong!