- 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.
:q
or:help
)
- Vim strives to be purely keyboard-driven
- Most commands have some handy mnemonic to help you memorize it
- e.g.
d
is delete,u
is undo,p
is put,i
is insert
- e.g.
- Many commands have multiple forms that do something similar
- e.g.
i
inserts before cursor,I
inserts at the beginning of the line - e.g.
d
is the delete command,D
deletes to end-of-line,dd
deletes 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.
w
moves the cursor by a word,d
deletes something, sodw
deletes a word
- e.g.
- Commands can be repeated by prefixing them with a number
- e.g.
j
moves the cursor down one line, so5j
moves the cursor down 5 lines - e.g.
yy
yanks (copies) a line, so10yy
copies the next 10 lines
- e.g.
- Editing-movements can be combined with repetition as well
- e.g.
d3w
deletes 3 words
- e.g.
- Cursor movement
j
downk
uph
leftl
right
- Word movement
w
go to begining of next word (punctuation separates)W
likew
, but only space separatese
go to end of current word (punctuation separates)E
likee
, but only space separatesb
go back to the beginning of previous word (punctuation separates)B
likeb
, 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)0
go to beginning of line10|
go to column 10
- Searching movement
/foo
go to the next instance of "foo" in the document?foo
like/foo
but in reversen
repeat the last search in its natural directionN
repeat the last search in reversefx
find the next location of x on the current line and go to itFx
likefx
but in reversetx
find til the next location of x on the current line and go to it (puts cursor just before x)Tx
liketx
but in reverse;
repeat the last line search in its natural direction,
repeat the last line search in reverse%
G[
,{
- Marks
mx
creates a mark named x at the cursor's current location`x
moves 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-d
page downCtrl-u
page upgg
go to top of fileG
go to bottom of file10G
go to line 10
- View movement
H
move cursor to top of view (high point)M
move cursor to middle of viewL
move cursor to bottom of view (low point)zz
center view on the cursor's current line
- Mode switching: transitioning from normal mode to insert mode
i
start inserting left of cursorI
start inserting at the beginning of the linea
start inserting right of cursor (mnemonic: after or append)A
start inserting at the end of the lineo
start inserting on a new line below currentO
start inserting on a new line above currentR
start replacing (like insert, but overwrites)Esc
Ctrl-[
return to normal mode
- Editing
u
undoCtrl-r
redoy
copy (yank) text, requires movementyy
copy entire linep
put (paste) after cursorP
put before cursord
delete text, requires movementD
delete to end of linedd
delete entire linec
change text (change is a delete that also starts inserting), requires movementC
change to end of linecc
change entire lines
substitute textrx
replace character under cursor with xx
delete 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.
y
yanks selection,d
deletes it) v
switch to character-based visual modeV
switch to line-based visual modeCtrl-v
switch to block-based visual mode- While in block-based visual mode, after selecting text,
I
andA
will switch you to multiline insert/append mode where you can edit multiple lines together
- While in block-based visual mode, after selecting text,
Esc
Ctrl-[
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
3yy
yank 3 lines starting at the cursor10>>
indent 10 lines starting at the cursord2w
and2dw
both delete 2 words4p
put 4 times10k
move cursor up 10 lines
:
in normal mode, switch to command-line mode to enter editor commands- A few editor commands
q
quitq!
quit without savingwq
write file and quitwq!
write file and quit, even if read-onlyw
write filew!
write file, even if read-only%s/foo/bar/g
replace 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.
qx
start recording macro into the macro named xq
if recording, stop recording@x
replay 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.
c
andy
- e.g.