This article describes how to find and replace text in Vim/Vi.
Vim is the most popular command line text editor. It comes pre-installed on macOS and most Linux distributions. Finding and replacing text in Vim is very easy.
Basic Find and Replace
In Vim, you can use the :substitute
(:s
) command to find and replace text.
To run commands in Vim, you must be in normal mode, which is the default mode when you start the editor. To return to normal mode from any other mode, simply press the “Esc” key.
The general form of the replace command is as follows.
|
|
This command searches for {pattern}
in each line of [range]
and replaces it with {string}
. is a positive integer multiplied by the command [count]
.
If [range]
and [count]
are not given, only the pattern
found in the current line is replaced. The current line is the line where the cursor is located.
For example, to search for the first match of the string foo
in the current line and replace it with bar
, you would use the following command.
|
|
To replace all occurrences of the search pattern in the current line, add the g
flag.
|
|
To search and replace all matching patterns in the entire file, use the percentage character %
as range
. This character indicates the range from the first line of the file to the last line.
|
|
If the {string}
part is omitted, it will be treated as an empty string and the matching pattern will be deleted. The following command removes all matches for the string “foo” in the current line.
|
|
In addition to the slash character (/
), you can use any other non-alphanumeric single-byte character as a separator. This option is useful when you include the " / " character in the search pattern or when replacing strings.
|
|
To confirm each replacement, use the c
flag.
|
|
|
|
Press y
to replace the match, or press l
to replace the match and exit. Press n
to skip, q
or Esc
to exit. The a
option replaces the match and all remaining matches. To scroll down the screen, use CTRL+Y
and to scroll up, use CTRL+E
.
You can also use regular expressions as search mode. The following command replaces all lines starting with “foo” with “Vim is the best”.
|
|
The ^
symbol matches the beginning of the line, and . *
matches any number of characters.
Case-sensitive
By default, the search operation is case-sensitive; a search for FOO
will not match Foo
.
To ignore the case of the search pattern, use the i
flag.
|
|
Another way to force case ignoring is to append \c
to the search pattern. For example, /Linux\c
performs a case ignored search.
If you have changed the default case setting and want to perform a case-sensitive search, use the I
flag.
|
|
An uppercase \C
after the pattern will also force a case-matching search.
Search range
When no range is specified, the alternative command runs only on the current line.
The range can be one line or a range between two lines. Line specifiers are separated by the ,
or ;
characters. A range can be specified using absolute line numbers or special symbols.
For example, to replace all occurrences of foo' with
bar’ in all lines from line 3 to line 10, you would use the following command.
|
|
The range includes all content, which means that the range includes the first and last row.
The dot .
character indicates the current line, and $
- the dollar sign indicates the last line. Replace foo
in all lines from the current line to the last line.
|
|
You can also use the " +" or “-” symbol to set the line descriptor followed by the number added to or subtracted from the previous line number. If the number after the symbol is omitted, the default is 1.
For example, to replace each “foo” with “bar” starting from the current line and the next four lines, enter the following command.
|
|
Replace whole words
The replace command finds the pattern as a string, not as an entire word. For example, if you are searching for “gnu”, the search results match “gnu” embedded in a larger word (such as “cygnus” or " magnum") in a larger word.
To search for an entire word, type \<
to mark the beginning of the word, enter a search pattern, and type \>
to mark the end of the word.
For example, to search for the word “foo”, you would use \<foo\>
.
|
|
Alternate History
Vim keeps track of all the commands you have run in the current session. To browse the history to find previous alternative commands, type :s
and use the up/down arrow keys to find previous alternative actions. To run a command, simply press Enter
. You can also edit the command before executing the action.
Example
Comment lines (add #
in front of the line) from 5 to 20.
|
|
Uncomment lines 5 through 20 to restore the previous changes.
|
|
Replace all instances of “Apple”, “Orange” and “Mango” with “Fruit.
|
|
Delete trailing spaces at the end of each line.
|
|
Conclusion
Search and replace is a powerful feature of Vim that allows you to change text quickly.