Here’s a vi commands cheat sheet to help you navigate and edit files efficiently in the vi text editor. It covers essential commands for opening files, moving within a document, editing, searching, and saving.
Opening and Exiting
Open a file
vi filename
– Open a file in vi.
Save and Exit
:w
– Save the file.:q
– Quit (only if no changes have been made).:wq
orZZ
– Save and quit.:q!
– Quit without saving.
Navigation
Moving by Character
h
– Move left.l
– Move right.j
– Move down.k
– Move up.
Moving by Word
w
– Move to the next word.e
– Move to the end of the word.b
– Move to the beginning of the word.
Moving by Line
0
– Move to the beginning of the line.^
– Move to the first non-blank character of the line.$
– Move to the end of the line.
Moving by Paragraph
{
– Move to the beginning of the paragraph.}
– Move to the end of the paragraph.
Moving by Screen
Ctrl + d
– Move half-page down.Ctrl + u
– Move half-page up.Ctrl + f
– Move full page down.Ctrl + b
– Move full page up.
Moving by File
gg
– Go to the beginning of the file.G
– Go to the end of the file.nG
– Go to line n (e.g.,50G
to go to line 50).H
– Move to the top of the screen.M
– Move to the middle of the screen.L
– Move to the bottom of the screen.
Editing
Insert Mode (Enter Text)
i
– Insert before cursor.I
– Insert at the beginning of the line.a
– Append after cursor.A
– Append at the end of the line.o
– Open a new line below.O
– Open a new line above.
Delete Text
x
– Delete the character under the cursor.X
– Delete the character before the cursor.dw
– Delete the word.d$
– Delete from the cursor to the end of the line.dd
– Delete the whole line.dG
– Delete from cursor to end of file.dgg
– Delete from cursor to start of file.
Copy and Paste
yy
– Copy (yank) the entire line.yw
– Yank a word.y$
– Yank from cursor to end of line.p
– Paste after cursor.P
– Paste before cursor.
Undo and Redo
u
– Undo the last change.Ctrl + r
– Redo the last undo.
Replace Text
r<char>
– Replace the character under the cursor.R
– Enter replace mode (overwrite text).
Searching
Search Forward and Backward
/pattern
– Search forward forpattern
.?pattern
– Search backward forpattern
.n
– Repeat the search in the same direction.N
– Repeat the search in the opposite direction.
Search and Replace
:s/old/new/
– Replace first occurrence in current line.:s/old/new/g
– Replace all occurrences in current line.:%s/old/new/g
– Replace all occurrences in entire file.:%s/old/new/gc
– Replace all occurrences with confirmation.
Visual Mode
Selecting Text
v
– Start character-wise selection.V
– Start line-wise selection.Ctrl + v
– Start block-wise selection.
Miscellaneous
Indentation
>>
– Indent current line.<<
– Unindent current line.
Case Change
~
– Toggle case of character under the cursor.gU
– Change selection to uppercase.gu
– Change selection to lowercase.
Tips and Sources
Useful Tips
- Use
:set number
to display line numbers. - Use
:set ignorecase
to make searches case-insensitive. - Use
:set hlsearch
to highlight search results. - Use
:set autoindent
to enable automatic indentation. - Press
Esc
multiple times if you’re stuck in a mode.