VI Cheat Sheet

A complete reference of VI editor commands with examples and usage instructions. Find the command just using the search bar.

57 commands found
Filter by category:

h

Navigation

Move cursor left

Syntax:

h

Examples:

h Move one character to the left
5h Move five characters to the left

Notes:

Basic cursor movement command

j

Navigation

Move cursor down

Syntax:

j

Examples:

j Move one line down
10j Move ten lines down

Notes:

Basic cursor movement command

k

Navigation

Move cursor up

Syntax:

k

Examples:

k Move one line up
3k Move three lines up

Notes:

Basic cursor movement command

l

Navigation

Move cursor right

Syntax:

l

Examples:

l Move one character to the right
7l Move seven characters to the right

Notes:

Basic cursor movement command

w

Navigation

Move to beginning of next word

Syntax:

w

Examples:

w Move to next word
3w Move forward 3 words

Notes:

Stops at punctuation

b

Navigation

Move to beginning of previous word

Syntax:

b

Examples:

b Move to previous word
2b Move back 2 words

Notes:

Stops at punctuation

e

Navigation

Move to end of current word

Syntax:

e

Examples:

e Move to end of word
4e Move to end of 4th word ahead

Notes:

Useful for selecting to end of word

0

Navigation

Move to beginning of line

Syntax:

0

Examples:

0 Move to start of current line

Notes:

Goes to column 0

$

Navigation

Move to end of line

Syntax:

$

Examples:

$ Move to end of current line
3$ Move to end of line 3 lines down

Notes:

Useful for line editing

gg

Navigation

Go to first line of file

Syntax:

gg

Examples:

gg Go to beginning of file
5gg Go to line 5

Notes:

Quick way to navigate to top

G

Navigation

Go to last line of file

Syntax:

G

Examples:

G Go to end of file
10G Go to line 10

Notes:

Quick way to navigate to bottom

Ctrl+f

Navigation

Page down

Syntax:

Ctrl+f

Examples:

Ctrl+f Move one page down

Notes:

Full page scroll

Ctrl+b

Navigation

Page up

Syntax:

Ctrl+b

Examples:

Ctrl+b Move one page up

Notes:

Full page scroll

i

Editing

Insert before cursor

Syntax:

i

Examples:

i Enter insert mode before cursor

Notes:

Most common way to start editing

a

Editing

Insert after cursor

Syntax:

a

Examples:

a Enter insert mode after cursor

Notes:

Useful for appending

o

Editing

Open new line below

Syntax:

o

Examples:

o Create new line below and enter insert mode

Notes:

Creates line and enters insert mode

O

Editing

Open new line above

Syntax:

O

Examples:

O Create new line above and enter insert mode

Notes:

Creates line and enters insert mode

x

Editing

Delete character under cursor

Syntax:

x

Examples:

x Delete current character
5x Delete 5 characters

Notes:

Like pressing Delete key

X

Editing

Delete character before cursor

Syntax:

X

Examples:

X Delete character to the left
3X Delete 3 characters to the left

Notes:

Like pressing Backspace key

dd

Editing

Delete entire line

Syntax:

dd

Examples:

dd Delete current line
3dd Delete 3 lines starting from current

Notes:

Deleted lines are stored in buffer

dw

Editing

Delete word

Syntax:

dw

Examples:

dw Delete from cursor to end of word
2dw Delete 2 words

Notes:

Deletes to end of current word

yy

Editing

Copy (yank) entire line

Syntax:

yy

Examples:

yy Copy current line
5yy Copy 5 lines starting from current

Notes:

Copied lines stored in buffer

yw

Editing

Copy word

Syntax:

yw

Examples:

yw Copy current word
3yw Copy 3 words

Notes:

Copies from cursor to end of word

p

Editing

Paste after cursor

Syntax:

p

Examples:

p Paste after current position
3p Paste 3 times

Notes:

Pastes from buffer

P

Editing

Paste before cursor

Syntax:

P

Examples:

P Paste before current position

Notes:

Pastes from buffer

r

Editing

Replace single character

Syntax:

r[char]

Examples:

ra Replace current character with 'a'
r5 Replace current character with '5'

Notes:

Replaces only one character

R

Editing

Replace mode

Syntax:

R

Examples:

R Enter replace mode

Notes:

Overwrites characters until Esc is pressed

u

Editing

Undo last change

Syntax:

u

Examples:

u Undo last edit
5u Undo last 5 changes

Notes:

Can undo multiple actions

Ctrl+r

Editing

Redo last undone change

Syntax:

Ctrl+r

Examples:

Ctrl+r Redo last undone change

Notes:

Opposite of undo

/pattern

Search

Search forward for pattern

Syntax:

/[pattern]

Examples:

/hello Search forward for 'hello'
/^start Search for lines starting with 'start'

Notes:

Use regular expressions

?pattern

Search

Search backward for pattern

Syntax:

?[pattern]

Examples:

?hello Search backward for 'hello'
?end$ Search for lines ending with 'end'

Notes:

Searches in reverse direction

n

Search

Find next occurrence

Syntax:

n

Examples:

n Go to next search result

Notes:

Repeats last search

N

Search

Find previous occurrence

Syntax:

N

Examples:

N Go to previous search result

Notes:

Repeats last search in reverse

:s/old/new/

Search

Substitute (replace) text

Syntax:

:s/[old]/[new]/[flags]

Examples:

:s/old/new/ Replace first 'old' with 'new' on current line
:s/old/new/g Replace all 'old' with 'new' on current line
:%s/old/new/g Replace all 'old' with 'new' in entire file
:1,10s/old/new/g Replace in lines 1-10

Notes:

Powerful find and replace feature

:w

File

Save file

Syntax:

:w [filename]

Examples:

:w Save current file
:w newfile.txt Save as newfile.txt
:w! Force save (override readonly)

Notes:

Write changes to disk

:q

File

Quit VI

Syntax:

:q

Examples:

:q Quit if no changes
:q! Quit without saving

Notes:

Exit the editor

:wq

File

Save and quit

Syntax:

:wq

Examples:

:wq Save file and quit
:wq! Force save and quit

Notes:

Common way to exit after editing

:x

File

Save and quit (only if changed)

Syntax:

:x

Examples:

:x Save and quit if file was modified

Notes:

More efficient than :wq for unchanged files

ZZ

File

Save and quit (normal mode)

Syntax:

ZZ

Examples:

ZZ Save and quit from normal mode

Notes:

Quick alternative to :wq

:e

File

Edit file

Syntax:

:e [filename]

Examples:

:e newfile.txt Open newfile.txt for editing
:e! Reload current file, discarding changes

Notes:

Open different file

:r

File

Read file into current buffer

Syntax:

:r [filename]

Examples:

:r file.txt Insert contents of file.txt at cursor
:r !ls Insert output of 'ls' command

Notes:

Inserts file contents at cursor position

v

Visual

Start visual mode (character-wise)

Syntax:

v

Examples:

v Start selecting characters

Notes:

Select text character by character

V

Visual

Start visual line mode

Syntax:

V

Examples:

V Start selecting lines

Notes:

Select text line by line

Ctrl+v

Visual

Start visual block mode

Syntax:

Ctrl+v

Examples:

Ctrl+v Start selecting rectangular blocks

Notes:

Select rectangular areas of text

d (in visual mode)

Visual

Delete selected text

Syntax:

d

Examples:

v[select]d Select text then delete it

Notes:

Works with any visual selection

y (in visual mode)

Visual

Copy selected text

Syntax:

y

Examples:

v[select]y Select text then copy it

Notes:

Works with any visual selection

ciw

Text Objects

Change inner word

Syntax:

ciw

Examples:

ciw Delete current word and enter insert mode

Notes:

Deletes word regardless of cursor position in word

di"

Text Objects

Delete text inside quotes

Syntax:

di[delimiter]

Examples:

di" Delete text inside double quotes
di' Delete text inside single quotes
di( Delete text inside parentheses

Notes:

Works with various delimiters

da"

Text Objects

Delete around quotes (including quotes)

Syntax:

da[delimiter]

Examples:

da" Delete text and double quotes
da) Delete text and parentheses
da{ Delete text and curly braces

Notes:

Includes the delimiters in the action

.

Advanced

Repeat last command

Syntax:

.

Examples:

. Repeat the last editing command

Notes:

Very powerful for repetitive tasks

>>

Advanced

Indent line

Syntax:

>>

Examples:

>> Indent current line
3>> Indent next 3 lines

Notes:

Useful for code formatting

<<

Advanced

Unindent line

Syntax:

<<

Examples:

<< Unindent current line
2<< Unindent next 2 lines

Notes:

Removes indentation

J

Advanced

Join lines

Syntax:

J

Examples:

J Join current line with next line
3J Join next 3 lines

Notes:

Removes line breaks

~

Advanced

Switch case of character

Syntax:

~

Examples:

~ Change case of character under cursor
5~ Change case of next 5 characters

Notes:

Toggles between upper and lowercase

Ctrl+n

Advanced

Auto-complete (in insert mode)

Syntax:

Ctrl+n

Examples:

Ctrl+n Show completion suggestions

Notes:

Completes words from current file

Ctrl+p

Advanced

Auto-complete previous (in insert mode)

Syntax:

Ctrl+p

Examples:

Ctrl+p Show previous completion suggestions

Notes:

Cycles backwards through completions

:set

Advanced

Set editor options

Syntax:

:set [option]

Examples:

:set number Show line numbers
:set nonumber Hide line numbers
:set autoindent Enable automatic indentation
:set tabstop=4 Set tab width to 4 spaces

Notes:

Customizes editor behavior

VI Editor Tips & Modes

VI Modes

  • Normal Mode: Default mode for navigation and commands
  • Insert Mode: For typing text (enter with i, a, o)
  • Visual Mode: For selecting text (v, V, Ctrl+v)
  • Command Mode: For file operations (start with :)
  • Press Esc to return to Normal mode

Essential Workflow

  1. vi filename - Open file
  2. i - Enter insert mode to edit
  3. Type your content
  4. Esc - Return to normal mode
  5. :w - Save file
  6. :q - Quit (or :wq to save and quit)

Quick Navigation

  • h j k l - Left, down, up, right
  • w b - Next/previous word
  • 0 $ - Beginning/end of line
  • gg G - Top/bottom of file

Common Mistakes

  • Forgetting to press Esc before commands
  • Using :q! when you want to save changes
  • Not understanding which mode you're in
  • Trying to use mouse for navigation