14.06.2014 Views

vim 101 hacks

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Transaction ID: 85W16630AD8515549<br />

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

www.thegeekstuff.com<br />

Original Text: Linux is good. Life is nice.<br />

:%s/\(good\|nice\)/awesome/g<br />

Translated Text: Linux is awesome. Life is awesome.<br />

You can also do full-word substitution by specifying regular expression. The<br />

following example does the substitution of hey or hi with hai. Please note<br />

that this does not do any substitution within larger words such as ‘they’,<br />

‘this’.<br />

:%s/\/hai/g<br />

• \< – word boundary.<br />

• \| – “logical or” (in this case hey or hi)<br />

Example 8. Interactive Find and Replace in Vim Editor<br />

You can perform an interactive search and replace using the ‘c’ flag of the<br />

substitute command, which specifies to ask for confirmation to do<br />

substitution or not, as explained below. In this example, Vim editor will do a<br />

global search for the word ‘awesome’ and replace it with ‘wonderful’.<br />

However, it will do the replacement only based on your input as explained<br />

below.<br />

:%s/awesome/wonderful/gc<br />

replace with wonderful (y/n/a/q/l/^E/^Y)?<br />

• y – Will replace the current highlighted word. After replacing it will<br />

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

133

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

Saved successfully!

Ooh no, something went wrong!