Search & Replace
Vim Text Editor Quick Reference
3 min read
Published Jul 6 2025, updated Aug 17 2026
Guide Sections
Guide Comments
Searching for text
| Key | Description |
|---|---|
/text | Search forwards for whatever text is specified, case sensitive, eg. /fish will find the text fish |
/text\c | Search forwards for whatever text is specified, case insensitive, eg. /fish\c will find the text fish or Fish or FISH etc. |
?text | Search backwards for whatever text is specified, case sensitive, eg. ?fish will find the text fish |
?text\c | Search backwards for whatever text is specified, case insensitive, eg. ?fish\c will find the text fish or Fish or FISH etc. |
/regex | Search forwards with a regex pattern, eg. /\d\{3} will search for a 3 digit number forwards |
?regex | Search backwards with a regex pattern, eg. ?\d\{3} will search for a 3 digit number backwards |
n | Next matching pattern |
N | Previous matching pattern |
* | Search for the current word forwards |
# | Search for the current word backwards |
Substitute
Substitute can be used to replace text in lines, syntax is :[range]s/pattern/replacement/[flags]
[range]- this is optional, list of options defined below.pattern- this can be text or a regex pattern.replacement- this is the text you want to add replace with.[flags]- this is optional and determines how it will act, list of options defined below.
Examples:
:s/car/bike/This will replace the first occurrence of car it finds in the file with the word bike.
:%s/car/bike/gThis will replace all occurrences of car with the word bike in the entire file. (% = all lines, g = global per line).
Ranges
Basic range elements:
Range | Meaning |
| Curren line |
| Last line in the file |
| Line 1 |
| Line 5 |
| Line marked with mark |
| Next line matching |
| Previous line matching |
|
|
|
|
These can be combined to make ranges using commas , or semi colons ;:
Syntax | Meaning |
| Lines 1 through 5 |
| From current line to end of the file |
| From match of |
| From mark |
| Shortcut for the entire file ( |
| Current line and the next 3 lines |
| All lines between (and including) those that match those patterns |
| Line 5 and the next 2 lines (so lines 5–7) |
The difference between using , or ; :
,- Both line numbers are evaluated relative to line 1.;- The second number is evaluated relative to the first.
Flags
| Key | Description |
|---|---|
g | Replace all occurances |
i | Ignore case |
I | Don't ignore case |
c | Confirm each substitution |
Global
Global allows you to select which lines you want to run a command on. So you can combine it with replace to only replace text in a selection of lines, or combine it with delete to only delete within certain lines etc.
The syntax is :[range]g/pattern/command
[range]- is an optional range.pattern- is what pattern you want to match when selecting where you want to run a command.command- is the command you would like to run.
Examples:
:g/dog/s/cat/duck/On all lines containing dog, run the substitution s/cat/duck/.
:g/^#/dDelete all lines starting with #.
:g/^\s*$/dDelete all blank lines.
Inverse global
Inverse global uses :v and is the opposite of the global option, so will run a command on any lines that don't match the selection.
The syntax and options are the same as global but using v instead of g.
Examples:
:v/^#/dDelete all lines NOT starting with #.
:v/dog/s/cat/duck/Replace cat with duck only on lines that don’t contain dog.