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.

 1  :[range]s/{pattern}/{string}/[flags] [count] 

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.

 1  :s/foo/bar/ 

To replace all occurrences of the search pattern in the current line, add the g flag.

 1  :s/foo/bar/g 

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.

 1  :%s/foo/bar/g 

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.

 1  :s/foo//g 

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.

 1  :s|foo|bar| 

To confirm each replacement, use the c flag.

 1  :s/foo/bar/gc 
 1  replace with bar (y/n/a/q/l/^E/^Y)? 

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”.

 1  :%s/^foo.*/Vim is the best/gc 

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.

 1  :s/Foo/bar/gi 

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.

 1  :s/foo/bar/gi 

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.

 1  :3,10s/foo/bar/g 

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.  1  :.,$s/foo/bar/ 

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.

 1  :.,+4s/foo/bar/g 

## 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\>.

 1  :s/\/bar/ 

## 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.

 1  :5,20s/^/#/ 

Uncomment lines 5 through 20 to restore the previous changes.

 1  :5,20s/^#// 

Replace all instances of “Apple”, “Orange” and “Mango” with “Fruit.

 1  :%s/apple\|orange\|mango/fruit/g 

Delete trailing spaces at the end of each line.

 1  :%s/\s\+\$//e 

## Conclusion

Search and replace is a powerful feature of Vim that allows you to change text quickly.