vim Find and Replace Substitute Command


This command:


substitutes each occurrence of foo with bar on the current line.

fool around with a foodie


barl around with a bardie

If you leave off the last /g, it will only replace the first occurence on the line. For example,


On the previous line would become

barl around with a foodie

This command:


performs the same substitution in lines 5 through 10.

This command


performs the same substitution from line 5 to the end of the file.

This command:


performs the same substitution on the whole buffer.

If you are in visual mode and hit the colon, the symbol '<,'> will appear. You can then do this


and have the substitution occur within your visual mode selection.

This command:


is equivalent to the command above but asks for confirmation on each occurence thanks to the /c flag (for "confirmation").

See :help :s and :help :s_flags.

See also this section on command-line ranges.