Get out
Esc back to normal mode (press it first, whenever you're lost):q quit:q! quit and throw away changes:w save:wq save and quitZZ save and quit:w !sudo tee % save a file you opened without sudoModes
i insert before the cursora insert after the cursorI insert at the start of the lineA insert at the end of the lineo new line below, and insertO new line above, and insertv visual: select charactersV visual: select linesCtrl-v visual: select a columnEsc back to normal modeMove
h j k l left, down, up, rightw next wordb previous word0 start of the line^ first non-blank character$ end of the linegg first line of the fileG last line of the file42G line 42 (or :42)Ctrl-d half a page downCtrl-u half a page up% the matching bracketEdit
x delete a characterdd delete (cut) a line5dd delete 5 linesdw delete to the next wordD delete to the end of the lineyy copy a line5yy copy 5 linesp paste afterP paste beforeu undoCtrl-r redo. repeat the last changer replace one charactercw change a wordcc change the whole line>> indent the line<< unindent the lineJ join the next line onto this oneThese combine: an action, then a movement. d$ deletes to the end of the line, yG copies to the end of the file, and ci" changes the text inside quotes.
Search
/text search forward?text search backwardn next matchN previous match* search for the word under the cursor:noh clear the highlightingReplace
:s/old/new/ first match on this line:s/old/new/g all matches on this line:%s/old/new/g all matches in the file:%s/old/new/gc the same, and ask each time:10,20s/old/new/g in lines 10 to 20Add \c to ignore case, as in :%s/old\c/new/g. Patterns use Vim’s own regex syntax, where a \v at the start makes them closer to what you know from other tools.
Files and windows
:e file open a file:r file insert a file here:sp file split horizontally:vsp file split verticallyCtrl-w w next window:tabe file open in a new tabgt next tab:ls open files:bn next fileHandy for config files
:set number line numbers:set paste paste without auto-indent:set nopaste back to normal:set list show tabs and line ends:set ff=unix convert Windows line endings (then :w):%s/\s\+$// remove trailing spaces:!nginx -t run a command, without leaving:syntax on syntax colorsTo keep settings, put them in ~/.vimrc, without the colon:
set numbersyntax onset expandtab shiftwidth=4 tabstop=4YAML breaks on tabs, so expandtab matters there.
When things look wrong
E45: 'readonly' option is set: you don’t have permission, or opened it read-only. Save with:w !sudo tee %, then reload with:e!.E325: ATTENTIONon opening: there’s a swap file from a crash, or someone else is editing it. Read the message before you choose. Delete the.swpfile when you’re done.- The screen froze: you pressed
Ctrl-s. PressCtrl-q. - Typing runs commands: you’re in normal mode. Press
i. Recording @qat the bottom: you pressedq. Pressqagain.- The server only has
vi: nearly all of this still works. On Debian and Ubuntu,vim.tinyis what you have until youapt install vim.