Search & Replace

Vim Text Editor Quick Reference

3 min read

Published Jul 6 2025


11
0
0
0

LinuxText Editor

Searching for text

KeyDescription
/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]

  1. [range] - this is optional, list of options defined below.
  2. pattern - this can be text or a regex pattern.
  3. replacement - this is the text you want to add replace with.
  4. [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/g

This 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

1

Line 1

5

Line 5

'a

Line marked with mark a

/pattern/

Next line matching pattern

?pattern?

Previous line matching pattern

+n

n rows below current/range line

-n

n rows above current/range line



These can be combined to make ranges using commas , or semi colons ;:


Syntax

Meaning

1,5

Lines 1 through 5

.,$

From current line to end of the file

'/,/\}

From match of / to match of }

'a,'b

From mark a to mark b

%

Shortcut for the entire file (1,$)

.,.+3

Current line and the next 3 lines

/start/,/end/

All lines between (and including) those that match those patterns

:5;+2

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

KeyDescription
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

  1. [range] - is an optional range.
  2. pattern - is what pattern you want to match when selecting where you want to run a command.
  3. 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/^#/d

Delete all lines starting with #.

:g/^\s*$/d

Delete 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/^#/d

Delete all lines NOT starting with #.

:v/dog/s/cat/duck/

Replace cat with duck only on lines that don’t contain dog.


Products from our shop

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact