Terminus
Vim Find And Replace

Vim Find And Replace

The short answer

In Vim, to search and replace the first occurrence of a string on the current line (i.e. the line the cursor is on), you can use the substitute command ([.inline-code]:s[.inline-code]) as follows:

:s/<pattern>/<replacement>

Where:

  • [.inline-code]pattern[.inline-code] is the string you want to replace.
  • [.inline-code]replacement[.inline-code] is the string you want to replace the [.inline-code]pattern[.inline-code] with.

For example, this command will replace the first occurrence of the string "foo" with the string "bar":

:s/foo/bar

You can also read our other article on how to search for patterns in Vim.

[#replace-a-pattern-on-a-line] Replacing all the occurrences of a pattern [#replace-a-pattern-on-a-line]

To replace all the occurrences of a pattern on the current line, you can append the [.inline-code]g[.inline-code] flag to the substitute command as follows:

:s/<pattern>/<replacement>/g

[#replace-a-pattern-in-a-file] Replacing all the occurrences of a pattern in the entire file [#replace-a-pattern-in-a-file]

To replace all the occurrences of a pattern in the entire file, you can prepend the substitute command with a percentage sign ([.inline-code]%[.inline-code]) as follows:

:%s/<patter>/<replacement>

[#replace-multiple-patterns]Replacing the occurrences of multiple patterns [#replace-multiple-patterns]

To find and replace the occurrences of multiple patterns at once, you can use the following command:

:[%]s/<pattern>[\|<pattern>...]/<replacement>/[g]

Where:

  • [.inline-code] <pattern>[\| <pattern>...][.inline-code] is a list of patterns separated by [.inline-code]\|[.inline-code].

For example, this command will replace all the occurrences of the strings "email" and "login" with the string "credentials" in the entire file:

:%s/email\|login/credentials

[#make-substitute-case-insensitive] Making the substitute command case insensitive [#make-substitute-case-insensitive]

To replace the occurrences of a pattern regardless of its case, you can append the [.inline-code]i[.inline-code] flag to the substitute command as follows:

:[%]s/<pattern>/<replacement>/[g]i

Replacing the occurrences of special characters

[#replace-tabs-with-spaces] Replacing tabs with spaces [#replace-tabs-with-spaces]

Using the pattern matching approach above, we can use vim substitute to replace tabs with spaces:

:[%]s/\t/ /[g]

[#replace-double-and-single-quotes]Replacing double quotes with single quotes [#replace-double-and-single-quotes]

Similarly, to replace double quotes with single quotes on the entire file, developers can run this command:

:[%]s/"/'/[g]

Limiting the pattern substitution to specific lines

In Vim, there are several ways to limit the substitution of patterns to a specific number of lines.

[#replace-in-a-line-range] Substituting between a line range [#replace-in-a-line-range]

To replace a pattern between specific lines, you can use the substitute command as follows:

:<start>,<end>s/<pattern>/<replacement>/g

Where [.inline-code]start[.inline-code] and [.inline-code]end[.inline-code] are the line numbers the substitution should happen between.

For example, this command applies the substitution between the lines 2 and 7:

:2,7s/hello/world

[#easily-recall-syntax-with-ai]Easily retrieve this syntax using Warp's Agent Mode: [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this syntax using the Warp Agent Mode feature:

Entering[.inline-code]What is the Vim command to substitute a pattern in a line range?[.inline-code] in the Agent Mode question input will prompt a human-readable step by step guide including code snippets.

[#replace-n-lines-from-pointer] Substituting N lines from the current line [#replace-n-lines-from-pointer]

To replace a pattern starting from the current line to a specific line number, you can use the substitute command as follows:

:.,+<lines>s/<pattern>/<replacement>/g

Where:

  • [.inline-code].[.inline-code] represents the current line.
  • [.inline-code]lines[.inline-code] represents the amount of lines.

For example, this command applies the substitution from the current line on to the next 7 lines:

:.,+7s/hello/world

[#replace-from-pointer-to-end]Substituting from the current line to the end of the file [#replace-from-pointer-to-end]

To replace a pattern starting from the current line to the end of the file, you can use the substitute command as follows:

:.,$s/<pattern>/<replacement>/g

For example, this command applies the substitution from the current line to the end of the file:

:.,$s/hello/world

[#replace-in-a-visual-block]Substituting in a visual block selection [#replace-in-a-visual-block]

To replace a pattern with a visual block selection, you can use the substitute command as follows:

:'<,'>s/<pattern>/<replacement>/g

Where:

  • [.inline-code]'<[.inline-code] represents the start of the visual block.
  • [.inline-code]'>[.inline-code] represents the end of the visual block.

[#deleting-a-pattern] Deleting the occurrences of a pattern [#deleting-a-pattern]

To delete all the occurrences of a pattern, you can leave the replacement string blank as follows:

:[%]s/<pattern>//[g]