Tools › Cheat sheets › Linux & Shell › Vim
📝

Vim cheat sheet

Vim is modal: Normal mode for commands, Insert mode for typing, Visual mode for selecting. Press Esc any time to return to Normal mode.

Click any command to copy it.

Get out (the most asked question)

CommandWhat it does
:qQuit (fails if there are unsaved changes).
:q!Destructive: Quit and discard unsaved changes.
:wSave.
:wqSave and quit.
ZZSave and quit (Normal mode).
:w !sudo tee %Save a file you opened without root permission.

Modes

CommandWhat it does
iInsert before the cursor.
aInsert after the cursor.
oOpen a new line below and insert.
OOpen a new line above.
vVisual mode: select characters.
VVisual mode: select lines.
Ctrl-vVisual block mode: select columns.
EscReturn to Normal mode.

Move

CommandWhat it does
h j k lLeft, down, up, right.
w / bNext / previous word.
0 / $Start / end of line.
gg / GTop / bottom of the file.
:42Go to line 42.
Ctrl-d / Ctrl-uScroll down / up half a page.
%Jump to the matching bracket.

Edit

CommandWhat it does
xDelete the character under the cursor.
ddDelete (cut) the line.
yyCopy the line.
p / PPaste after / before.
uUndo.
Ctrl-rRedo.
.Repeat the last change.
cwChange the word (delete it and enter insert mode).
DDelete to the end of the line.
>> / <<Indent / unindent the line.
ggVGSelect the whole file.

Search and replace

CommandWhat it does
/patternSearch forward.
n / NNext / previous match.
*Search for the word under the cursor.
:%s/old/new/gReplace all occurrences in the file.
:%s/old/new/gcReplace with confirmation for each.
:nohClear search highlighting.

Files and windows

CommandWhat it does
:e fileOpen another file.
:split / :vsplitSplit the window horizontally / vertically.
Ctrl-w wSwitch between windows.
:tabnew fileOpen a file in a new tab.
gtNext tab.
:set numberShow line numbers.
:set pastePaste text without auto-indent mangling.
:!commandRun a shell command.

Frequently asked questions

How do I exit Vim?

Press Esc, then type :q and Enter. To save and quit use :wq; to quit without saving use :q!.

Why does pasted text get indented strangely?

Auto-indent is re-indenting each line. Run :set paste before pasting, and :set nopaste afterwards.

More Linux & Shell cheat sheets

🐚 Bash🔎 grep🔍 find✂️ sed🧮 awk📦 tar🪟 tmux🔐 chmod permissionsAll Linux & Shell cheat sheets →
Copied to clipboard