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 (:s) as follows:
:s/<pattern>/<replacement>Where:
- pattern is the string you want to replace.
- replacement is the string you want to replace the pattern with.
For example, this command will replace the first occurrence of the string "foo" with the string "bar":
:s/foo/barYou can also read our other article on how to search for patterns in Vim.
Replacing all the occurrences of a pattern
To replace all the occurrences of a pattern on the current line, you can append the g flag to the substitute command as follows:
:s/<pattern>/<replacement>/gReplacing all the occurrences of a pattern in the entire file
To replace all the occurrences of a pattern in the entire file, you can prepend the substitute command with a percentage sign (%) as follows:
:%s/<patter>/<replacement>Replacing the occurrences of 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:
- <pattern>\| <pattern>...] is a list of patterns separated by \|.
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/credentialsMaking the substitute command case insensitive
To replace the occurrences of a pattern regardless of its case, you can append the i flag to the substitute command as follows:
:[%]s/<pattern>/<replacement>/[g]iReplacing the occurrences of special characters
Replacing tabs with spaces
Using the pattern matching approach above, we can use vim substitute to replace tabs with spaces:
:[%]s/\t/ /[g]Replacing double quotes with 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.
Substituting between a line range
To replace a pattern between specific lines, you can use the substitute command as follows:
:<start>,<end>s/<pattern>/<replacement>/gWhere start and end 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/worldEasily retrieve this syntax using Warp's Agent Mode:
If you’re using Warp as your terminal, you can easily retrieve this syntax using the Warp Agent Mode feature:

EnteringWhat is the Vim command to substitute a pattern in a line range? in the Agent Mode question input will prompt a human-readable step by step guide including code snippets.
Substituting N lines from the current line
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>/gWhere:
- . represents the current line.
- lines represents the amount of lines.
For example, this command applies the substitution from the current line on to the next 7 lines:
:.,+7s/hello/worldSubstituting from the current line to the end of the file
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>/gFor example, this command applies the substitution from the current line to the end of the file:
:.,$s/hello/worldSubstituting in a visual block selection
To replace a pattern with a visual block selection, you can use the substitute command as follows:
:'<,'>s/<pattern>/<replacement>/gWhere:
- '< represents the start of the visual block.
- '> represents the end of the visual block.
Deleting the occurrences of a pattern
To delete all the occurrences of a pattern, you can leave the replacement string blank as follows:
:[%]s/<pattern>//[g]Related articles
Go To End of File in Vim / Vi
Select and delete to the end of a file in Vim
Select all in Vim / Vi
Select all and copy, paste, or delete the contents
Copy & Paste in Vim / Vi
Copy (Yank), Paste (Put) and Cut (Delete) in Vim
Searching in Vim
Search forward, backward, case insensitive, and more
Show & Hide Line Numbers in Vim / Vi
Toggle absolute and relative line numbers
Undo & Redo in Vim / Vi
Keyboard shortcuts and summary of how Vim tracks changes
Vim Go To End/Start of Line
Go to the end or beginning of a line in Vim
Go To Line In Vim
Variety of approaches to go to lines
Go To Top of File in Vim
Learn how to jump the top of the file in Vim, then navigate, search, highlight, and delete.
Vim / Vi Page Up and Down Controls
Quick reference for Vim's page up and down controls
How to delete lines in Vim / Vi
The best answer depends on which mode you use