- Vim is a modal editor with four primary modes
- Normal: default mode used for movement and commands
- Insert: typical text editing mode
- Visual: selection mode for selecting characters, lines, and blocks of text
- Command-line: for entering editor commands (e.g.
:qor:help)
- Vim strives to be purely keyboard-driven
- Most commands have some handy mnemonic to help you memorize it
- e.g.
dis delete,uis undo,pis put,iis insert
- e.g.
- Many commands have multiple forms that do something similar
- e.g.
iinserts before cursor,Iinserts at the beginning of the line - e.g.
dis the delete command,Ddeletes to end-of-line,dddeletes an entire line
- e.g.
- Orthogonal design
- Vim has two main types of commands: movement and editing
- You can combine editing commands with movement commands to make editing more powerful
- e.g.
wmoves the cursor by a word,ddeletes something, sodwdeletes a word
- e.g.
- Commands can be repeated by prefixing them with a number
- e.g.
jmoves the cursor down one line, so5jmoves the cursor down 5 lines - e.g.
yyyanks (copies) a line, so10yycopies the next 10 lines
- e.g.
- Editing-movements can be combined with repetition as well
- e.g.
d3wdeletes 3 words
- e.g.
- Cursor movement
jdownkuphleftlright
- Word movement
wgo to begining of next word (punctuation separates)Wlikew, but only space separatesego to end of current word (punctuation separates)Elikee, but only space separatesbgo back to the beginning of previous word (punctuation separates)Blikeb, but only space separates
- Line movement
$go to last character on line (mnemonic: same as regex)^go to first character on line (mnemonic: same as regex)0go to beginning of line10|go to column 10
- Searching movement
/foogo to the next instance of "foo" in the document?foolike/foobut in reversenrepeat the last search in its natural directionNrepeat the last search in reversefxfind the next location of x on the current line and go to itFxlikefxbut in reversetxfind til the next location of x on the current line and go to it (puts cursor just before x)Txliketxbut in reverse;repeat the last line search in its natural direction,repeat the last line search in reverse%G[,{
- Marks
mxcreates a mark named x at the cursor's current location`xmoves the cursor to the location defined by mark x``moves the cursor back to its last location (e.g. after a search or paging)
- Document movement
Ctrl-dpage downCtrl-upage upgggo to top of fileGgo to bottom of file10Ggo to line 10
- View movement
Hmove cursor to top of view (high point)Mmove cursor to middle of viewLmove cursor to bottom of view (low point)zzcenter view on the cursor's current line
- Mode switching: transitioning from normal mode to insert mode
istart inserting left of cursorIstart inserting at the beginning of the lineastart inserting right of cursor (mnemonic: after or append)Astart inserting at the end of the lineostart inserting on a new line below currentOstart inserting on a new line above currentRstart replacing (like insert, but overwrites)EscCtrl-[return to normal mode
- Editing
uundoCtrl-rredoycopy (yank) text, requires movementyycopy entire linepput (paste) after cursorPput before cursorddelete text, requires movementDdelete to end of linedddelete entire linecchange text (change is a delete that also starts inserting), requires movementCchange to end of lineccchange entire linessubstitute textrxreplace character under cursor with xxdelete single character under cursor.repeat last editing command~toggle casing of selected characters>><<indent, outdent line (mnemonic: bitwise shift, like C)
- Visual mode
- While in visual mode, most normal-mode commands will apply to whatever you have selected (e.g.
yyanks selection,ddeletes it) vswitch to character-based visual modeVswitch to line-based visual modeCtrl-vswitch to block-based visual mode- While in block-based visual mode, after selecting text,
IandAwill switch you to multiline insert/append mode where you can edit multiple lines together
- While in block-based visual mode, after selecting text,
EscCtrl-[return to normal mode
- While in visual mode, most normal-mode commands will apply to whatever you have selected (e.g.
- Commands and movements can be repeated
3yyyank 3 lines starting at the cursor10>>indent 10 lines starting at the cursord2wand2dwboth delete 2 words4pput 4 times10kmove cursor up 10 lines
:in normal mode, switch to command-line mode to enter editor commands- A few editor commands
qquitq!quit without savingwqwrite file and quitwq!write file and quit, even if read-onlywwrite filew!write file, even if read-only%s/foo/bar/greplace all instances of "foo" with "bar" in the document
- Macros
- Macros allow you to record commands and play them back. This is helpful when editing structured patterns of data or performing a repetitive operation on similar text.
qxstart recording macro into the macro named xqif recording, stop recording@xreplay the commands stored in x
- Vim can address anything that has matching characters (e.g. quotes, parentheses, brackets, braces)
- e.g.
di"deletes the inner text between two quotes, same withdi',di(,di{, etc. - This works with other commands, too, e.g.
candy
- e.g.