Vim Find and Replace

Last Updated 2022-12-29

Vim Substitute

Here’s a basic invocation of substitute in Vim to search and replace a string: 


  • replaces first [.inline-code]fizz[.inline-code] pattern found on current line with [.inline-code]buzz[.inline-code]

The full command syntax is:


If you’re looking for advice on just searching, not replacing, read about vim search here.

[#replace-patterns]Use Vim Substitute to Replace Patterns[#replace-patterns]


  • searches for the first exact matches on the current line of the words [.inline-code]fizz[.inline-code] or [.inline-code]buzz[.inline-code] and replaces it with [.inline-code]fizzbuzz[.inline-code]


  • omitting a replacement string will find the first occurrence of [.inline-code]fizz[.inline-code] and replace it with no string - effectively deleting the [.inline-code]fizz[.inline-code] occurrence from the file.

[#replace-tabs-with-spaces]Replace Tabs with Spaces in Vim[#replace-tabs-with-spaces]

Using the pattern matching approach above, we can use vim substitute to replace tabs with spaces: [.inline-code]:%s/\t/ /g[.inline-code].

[#replace-double-quotes-with-single-quotes]Replace Double Quotes with Single Quotes in Vim[#replace-double-quotes-with-single-quotes]

Similarly, to replace double quotes with single quotes on the entire file, developers can run this command: [.inline-code]:%s/”/’/g[.inline-code].

[#vim-substitute-flags]Vim Substitute Flags[#vim-substitute-flags]

[#replace-all][.inline-code]g[.inline-code] - global replace all[#replace-all]


  • note the global - [.inline-code]g[.inline-code] - flag being added
  • this will replace all occurrences found of [.inline-code]fizz[.inline-code] pattern found on the current line with [.inline-code]buzz[.inline-code]
  • Without this, the substitute command will run only for the first occurrence on the current line that you’re on.

[#confirmation][.inline-code]c[.inline-code] - confirmation[#confirmation]


  • note the confirmation - [.inline-code]c[.inline-code] - flag being added
  • replace all occurrences found of [.inline-code]fizz[.inline-code] pattern found on current line with [.inline-code]buzz[.inline-code] but confirms with you before making the replacement
  • Respond [.inline-code]y[.inline-code] to replace the match, l to replace the match and quit substitute, [.inline-code]n[.inline-code] to skip the current match, and [.inline-code]q[.inline-code] to quit.

[#ignore-casing][.inline-code]i[.inline-code] - ignore casing[#ignore-casing]


  • note the ignore casing - [.inline-code]i[.inline-code]  flag being added
  • replace all occurrences found of [.inline-code]FIZZ[.inline-code] pattern regardless of casing found on current line with [.inline-code]buzz[.inline-code]
  • searching for [.inline-code]HELLO[.inline-code] will provide the same results as if you searched for [.inline-code]heLlo[.inline-code]

[#substitute-with-ranges]Limiting the scope of Vim substitute with ranges[#substitute-with-ranges]

By providing a range option, developers are able to limit the substitution to specific lines or the entire file.


  • adding a % applies the substitution across the entire file


  • applies the substitution from line 2 to 7


  • applies the substitution from current line to the next 7 lines


  • applies the substitution from current line to the end of file

[#find-and-replace-in-selection]Vim find and replace in selection[#find-and-replace-in-selection]

Alternatively, you can enter visual mode and choose a block selection. After highlighting your intended section, type [.inline-code]:[.inline-code].


  • applies substitution from the beginning of the selected block to the end of the block
  • note: entering command line mode with : will pre-fill the command line with the range [.inline-code]:'<,'>[.inline-code]

[#using-slash-and-dot]Using slash and dot instead of substitute to find and replace[#using-slash-and-dot]

Slash and dot is a straightforward method that performs much like the find and replace function found on Google Docs, Microsoft Word, and other document-based applications. Here’s an example of how to use it.

Open any file in vim: [.inline-code]vim <file-name>[.inline-code]

Type [.inline-code]/[.inline-code] and search for the word: [.inline-code]/ <search word>[.inline-code]

  • note: this search performs a partial match

To find the next occurrence of the pattern, type [.inline-code]n[.inline-code] (for next)

To replace the word, first type: [.inline-code]cgn[.inline-code]

At this point, Vim will remove the word and you can type the replacement word: [.inline-code]<replacement word>[.inline-code]

When you’re done, hit [.inline-code]ESC[.inline-code]. If you want to replace the next occurrence of the search word, hit [.inline-code]n[.inline-code] and then hit [.inline-code].[.inline-code].

This will automatically replace the found occurrence with the same replacement word typed from before.

If you have many occurrences of the same word, you can see how this will get repetitive quickly; so generally you should use the substitute command instead.