How to Find and Replace All in Vim With Substitute Command
Tutorial on how to use substitute command, which may by one of the most useful commands in Vim.
Following my last tutorial on Vim, “5 Most Essential Vim Commands for Beginners” if you haven’t read make sure to check it out. In this article, I wanted to share one of the most useful commands you may find trivial to use in any text editor and that is a substitute command. If you have used Sublime Text, chances are you have used Replace toolbar, which find and replace the desired string or all occurrences of the string in the current file, well, that’s just what substitute does. At any point in your coding career, you will find your self in a need to change a large selection of strings. Doing it manually is one solution, doing it pragmatically is another better solution. And Vim offers an extremely fast and efficient way of doing it with the substitute command.
Syntax
Substitute command is invoked from the command mode, by calling :s
, which
stands for s[ubstitute]. But before any executions, it’s better to start with
reading the Vim’s documentation for the better get knowing.
:help :substitute
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
For each line in [range] replace a match of {pattern} with {string}.
For the {pattern} see pattern.
{string} can be a literal string, or something special; see
sub-replace-special.
When [range] and [count] are omitted, replace in the current line only. When
[count] is given, replace in [count] lines, starting with the last line in
[range].
When [range] is omitted start in the current line. E939
[count] must be a positive number. Also, see cmdline-ranges.
See :s_flags for [flags].
The delimiter doesn't need to be /, see pattern-delimiter.
So basically, the most basic usage would be:
:s/search/replace
Examples
Find and replace the first occurrence in the current line
When the range parameter is not provided to the function, it will find and replace only the first occurrence in the current line:
:s/search/replace
Find and replace all occurrences
By adding the percentage sign in front of the s we are giving the max range, so the function will look up through the whole file and replace them:
:%s/search/replace
Find and replace all occurrences in the range of lines
We can also limit the function lookup only to some lines [start, end], which will find and replace all the occurrences only inside those lines:
:1,10s/search/replace
Change only whole words exactly matching
To avoid removing wrong selections, we can select only the whole words using regex:
:%s/\<foo\>/bar/
Flags
Flags offer some caution and control in executing the substitute command, for example:
Global (g)
This flag will look throughout the whole files, assuming the default setting for the ‘gdefault’ and ’edcompatible’ option is off.
:%s/search/replace/g
Confirmation (c)
By adding c flag the command will ask for confirmation for every occurrence before changing.
:s/search/replace/c
Insensitive (i)
This will set up case insensitive lookup order.
:s/search/replace/i
Sensitive (I)
Meanwhile capital I will set up case insensitive lookup order.
:s/search/replace/I
Conslusion
This is the most basic usage of the substitute command, for the more advance usage I recommend reading the documentation and Vim Fandom article.