Find and Replace with Vim : Ubuntu (6.06.1 / 6.10)

By | 2006/12/24

I recently posted about the power of Vim and we also learned about some of Vim’s Sweet Moves. Today I wanted to talk about a few more advanced actions within Vim that can make your life easier. What are they? Find and Replace.

Something that we’ve all had to do at one time or another is use find and replace within a document. This is really easy within Vim.

To change all instances of one word to another within one line use:

:s/word/replacement/g

To change all instances of one word to another within the entire file use:

:%s/word/replacement/g

To ask for confirmation before replacing each instance of a word use:

:%s/word/replacement/gc

I use these options regularly, especially when I’m upgrading between releases (or steps to upgrading). It’s simple to find & replace all instances of dapper within your /etc/apt/sources.list to edgy using one simple command within Vim. Once those lines are updated you’re ready to upgrade to the next release–it’s as simple as that!

If you want to find out more about Vim (as if this wasn’t enough!) you can use the command:

:help

while within Vim and it’ll give you some more tips and options. You can also run the command:

vimtutor

to walk you through doing all of these suggestions in actual practice. It’s a really easy to follow, very informative tutor.

Also, for some more advanced uses of Vim including using it as a file explorer, using tabs and quite a bit more check out my buddy Aaron’s post Use Vim!

Leave a Reply

Your email address will not be published. Required fields are marked *