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
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.
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:
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:
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:
And the shortcut is:
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.
You can combine all we have learned in this way.
That will search and replace 10 lines above the current one, and then until the end of the document.
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
So to work over an entire document use this command:
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:
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
And if you want to make it case insensitive
And if you want case sensitive (default)
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.
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:
And if you want to find the lines ending with t, well use this:
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.
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:
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:
From line 2 to 10 deletes foo and the rest of the line, check that it is foo, and not foo2.
From line 2 to 10 deletes foo and the rest “n” characters, where n is an integer.
From line 2 to 10 deletes all words after foo
From line 2 to 10 deletes the word bar and all text before it.
From line 2 to 10 deletes all words before the word bar.
Please add your own tips in the comments.