Vim Cheat Sheet
Vim editor commands — modes, motions, editing, search/replace, and the survival basics for editing files on remote shells.
Overview
Vim is the editor you'll hit on almost every Linux box, often the only one available on a stripped-down shell. Knowing the basics—how to move, edit, save, and quit—is essential. Vim is modal: Normal mode for navigation/commands, Insert mode for typing, Visual mode for selection.
Authorized use only. Edit files only on systems you own or have permission to access.
Survival (quit/save)
Enter Insert mode (start typing)
iBack to Normal mode
EscSave (write)
:wQuit
:qSave and quit
:wq or ZZQuit without saving (discard changes)
:q!Movement (Normal mode)
Left, down, up, right
h j k lForward / back one word
w / bStart / end of line
0 / $Top / bottom of file
gg / GJump to line 42
:42Half page up / down
Ctrl-u / Ctrl-dEditing
Delete character under cursor
xDelete (cut) current line
ddYank (copy) current line
yyPaste after / before cursor
p / PUndo / redo
u / Ctrl-rChange word (delete + insert)
cwAppend at line end / open new line below
A / oSearch & replace
Search forward (n = next, N = previous)
/patternSearch backward
?patternReplace all occurrences in the file
:%s/old/new/gReplace all with confirm prompt
:%s/old/new/gcClear search highlight
:nohVisual mode & blocks
Character-wise selection
vLine-wise selection
VBlock (column) selection
Ctrl-vIndent / unindent line
>> / <<Useful one-liners
Open a file at a specific line
vim +42 file.txtRun a shell command from inside vim
:!idRead command output into the buffer
:r !ls -laSave a file you opened without write perms (sudo trick)
:w !sudo tee %Spawn a shell (handy if vim is a sudo GTFOBin)
:!/bin/shTips
- If you're stuck, press Esc then type :q! to bail out without saving.
- :w !sudo tee % saves a file you forgot to open with sudo.
- Combine counts with motions: 3dd deletes 3 lines, 5j moves down 5.
- vim appears on GTFOBins — sudo vim -c ':!/bin/sh' is a classic privesc.