Introduction

In every document editing software search and replace is a desirable function or feature, that is also true in text editor software. If you are programing, editing lists, or CVS files, you will need almost for sure, some search and replace tool, I use vim for almost all text editing work I need to do. Search and replace on vim is pretty similar to what you do we you use sed to find and replace words in text files.

Search and replace in vim

Search and replace on the current line only

To act only in the current line enter the simplest form of the command

:/s/search-this/replace-with-this

Now vim will look in the current line for the string “search-this” and will replace it (if found) with “replace-with-this”

Search and replace in a range of lines

If you need to search for a string in a range of lines and not only in the current line, you may specify that range by issuing this command.

:5,20s/search-this/replace-with-this

Now, the command will have effect from line 5 to line 20. But if you want the command to work from line 5 to the end of the document use this:

:5,$s/search-this/replace-with-this

As you can see the symbol “$” identify the last line of the document and the current line is identified by a simple dot “.”, so If you enter this command:

:.,15s/search-this/replace-with-this

The search and replace will take place from the current line (should be minor than 15) to the 15th line.

If you need to work on the entire document, you have two options, the long one is:

:1,$s/search-this/replace-with-this

And the shortcut is:

:%s/search-this/replace-with-this

Being % some kind of alias for “1,$”.

If you need to go some lines above and below from the current line, use the “+” symbol and the number of lines you want to go back and forth.

:-3,+2s/search-this/replace-with-this

You can combine all we have learned in this way.

:-10,$s/search-this/replace-with-this

That will search and replace 10 lines above the current one, and then until the end of the document.

:.,+12s/search-this/replace-with/this

Will search and replace from the current line until 12 lines below the current line.

Global and confirmation modifiers

All above commands will only have effect on the first instance of a match in any given line, if you want to act all over the line, you will have to use the g modifier, and if you want to confirm any change before it happens, use the c modifier.

So to work over an entire document use this command:

:%s/foo/bar/gc

That command will work over the entire document and will search for the word “foo” and will ask you if you want it replaced by the word “bar”.

More vim’s search and replace tips and tricks

If you use this command:

:%s/foo/bar/gc

It will replace. foo foo2 foobar 123foo321_ for bar bar2 barbar 123bar321_. So If you only want to replace the exact word, I mean foo by bar, and not any word containing foo, use this command instead

:%s/\<foo\>/bar/gc

And if you want to make it case insensitive

:%s/\<foo\>/bar/gci

And if you want case sensitive (default)

:%s/\<foo\>/bar/gcI

Only searching

Of course you do not need to perform search and replace together you may be interested in only searching, this of course possible, just strip the replace part.

:/foo

Will search for foo, once found if you want to look for the next occurrence, just type:

:/

Wildcards and special searches

Vim is really powerful, let’s see a little example of how powerful it is.

  • A caret ^ match the beginning of a line
  • A dollar sign $ match the end of a line
  • \s match white spaces
  • \S match non-white spaces
  • \d match any digit
  • \D match any non-digit
  • \u match uppercase
  • \l match lowercase

So, if you want to find every line starting with t in your file, you may use this:

:/^t

And if you want to find the lines ending with t, well use this:

:/t$

This is useful, if you are trying to locate some special characters, i.e. if you are editing a html file, you do not want lines starting with “>” or ending with “<”, so you can look for them.

:/^>

And

:/<$

Replacing after you have found the word

Now that you have found the word you were looking for you can replace all occurrences of the them with:

%s//bar/gc

This replace any match of your last search by “bar”

Few last examples

As you may see the limit is your imagination, but here some more examples of using search and replace in vim to just delete matches instead of replacing them:

:2,10s/\<foo\>//

From line 2 to 10 deletes foo and the rest of the line, check that it is foo, and not foo2.

:2,10s/\<foo\>.\{n}//

From line 2 to 10 deletes foo and the rest “n” characters, where n is an integer.

:2,10s/\<foo\>\zs.*//

From line 2 to 10 deletes all words after foo

:2,10s/.*\<bar\>//

From line 2 to 10 deletes the word bar and all text before it.

:2,10s/.*\ze\<bar\>//

From line 2 to 10 deletes all words before the word bar.

Please add your own tips in the comments.