Find Replace in Vim or Vi
Find and Replace in Vim / Vi
This article describes how to find and replace text in Vim / Vi.
Vim is the most popular command-line text editor. It comes preinstalled on macOS and most Linux distributions. Finding and replacing text in Vim is quick and easy.
In Vim, you can find and replace text using the :substitute
(:s
) command.
To run commands in Vim, you must be in normal mode, the default mode when starting the editor. To go back to normal mode from any other mode, just press the ‘Esc’ key.
The general form of the substitute command is as follows:
:[range]s/{pattern}/{string}/[flags] [count]
The command searches each line in [range]
for a {pattern}
, and replaces it with a {string}
. [count]
is a positive integer that multiplies the command.
If no [range]
and [count]
are given, only the pattern found in the current line is replaced. The current line is the line where the cursor is placed.
For example, to search for the first occurrence of the string ‘foo’ in the current line and replace it with ‘bar’, you would use:
:s/foo/bar/
To replace all occurrences of the search pattern in the current line, add the g
flag:
:s/foo/bar/g
If you want to search and replace the pattern in the entire file, use the percentage character %
as a range. This character indicates a range from the first to the last line of the file:
:%s/foo/bar/g
If the {string}
part is omitted, it is considered as an empty string, and the matched pattern is deleted. The following command deletes all instances of the string ‘foo’ in the current line:
:s/foo//g
Instead of the slash character (/
), you can use any other non-alphanumeric single-byte character except as a delimiter. This option is useful when you have the ‘/’ character in the search pattern or the replacement string.
:s|foo|bar|
To confirm each substitution, use the c
flag:
:s/foo/bar/gc
replace with bar (y/n/a/q/l/^E/^Y)?
Press y
to replace the match or l
to replace the match and quit. Press n
to skip the match and q
or Esc
to quit substitution. The a
option substitutes the match and all remaining occurrences of the match. To scroll the screen down, use CTRL+Y
, and to scroll up, use CTRL+E
.
You can also use regular expressions as a search pattern. The command bellow replaces all lines starting with ‘foo’ with ‘Vim is the best’:
:%s/^foo.*/Vim is the best/gc
The ^
(caret) symbol matches the beginning of a line and .*
matches any number of any characters.
Case Sensitivity
By default, the search operation is case sensitive; searching for “FOO” will not match “Foo”.
To ignore case for the search pattern, use the i
flag:
:s/Foo/bar/gi
Another way to force ignore case is to append \c
after the search pattern. For example, /Linux\c
performs ignore case search.
If you changed the default case setting and you want to perform case sensitive search, use the I
flag:
:s/foo/bar/gi
Uppercase \C
after the pattern also forces case match search.
Search Range
When no range is specified the substitute command operates only in the current line.
The range can be either one line or a range between two lines. The line specifiers are separated with the ,
or ;
characters. The range can be specified using the absolute line number or special symbols.
For example, to substitute all occurrences of ‘foo’ to ‘bar’ in all lines starting from line 3 to line 10 you would use:
:3,10s/foo/bar/g
The range is inclusive, which means that the first and last lines are included in the range.
The dot .
character indicates the current line and $
- the dollar sign the last line. To substitute ‘foo’ in all lines starting from the current line to the last one:
:.,$s/foo/bar/
The line specifier can also be set using the ‘+’ or ‘-’ symbol,followed by a number that is added or subtracted from the preceding line number. If the number after the symbol is omitted, it defaults to 1.
For example to substitute each ‘foo’ with ‘bar’ starting from the current line and the four next lines, type:
:.,+4s/foo/bar/g
Substituting Whole Word
The substitute command looks for the pattern as a string, not a whole word. If, for example, you were searching for “gnu”, the search find matches where “gnu” is embedded in larger words, such as “cygnus” or “magnum”.
To search for a whole word, type \<
to mark the beginning of a word, enter the search pattern, type \>
to mark the end of a word:
For example, to search for the word “foo” you would use \<foo\>
:
:s/\<foo\>/bar/
Substitute History
Vim keeps track of all the commands you run in the current session. To browse the history for previous substitute commands, enter :s
and use the arrow up/down keys to find a previous substitute operation. To run the command, simply press Enter
. You can also edit the command before performing the operation.
Examples
Comment lines (add #
before the line) from 5 to 20:
:5,20s/^/#/
Uncomment lines from 5 to 20, revert the previous changes:
:5,20s/^#//
Replace all instances of ‘apple’, ‘orange’, and ‘mango’ with ‘fruit’:
:%s/apple\|orange\|mango/fruit/g
Remove trailing whitespace at the end of each line:
:%s/\s\+$//e
Conclusion
Searching and replacing is a powerful feature of Vim, which allows you to make changes to your text quickly.
Feel free to leave a comment if you have any questions.