Vim Reference#
Modes#
- Normal: for moving around a file and making edits
- Insert: for inserting text
- Replace: for replacing text
- Visual (plain, line, or block) mode: for selecting blocks of text
- Command-line: for running a command
You change modes by pressing <ESC> (the escape key) to switch from any mode
back to normal mode. From normal mode, enter insert mode with i, replace mode
with R, visual mode with v, visual line mode with V, visual block mode
with <C-v> (Ctrl-V, sometimes also written ^V), and command-line mode with
:.
You use the <ESC> key a lot when using Vim: consider remapping Caps Lock to
Escape (macOS
instructions).
Command-line#
Command mode can be entered by typing : in normal mode. Your cursor will jump
to the command line at the bottom of the screen upon pressing :. This mode
has many functionalities, including opening, saving, and closing files, and
quitting Vim.
:qquit vim (close window):wsave file (write file):wqsave and quit:e {name of file}open file for editing:lsshow open buffers:help {topic}open help:help :wopens help for the:wcommand:help wopens help for thewmovement
Movement#
You should spend most of your time in normal mode, using movement commands to navigate the buffer. Movements in Vim are also called "nouns", because they refer to chunks of text.
- Basic movement:
hjkl(left, down, up, right) - Words:
w(next word),b(beginning of word),e(end of word) - Lines:
0(start of line),^(first non-whitespace),$(end of line) - Screen:
H(Top of screen),M(Middle of screen),L(Lower/bottom of screen) - Scroll:
Ctrl-u(up),Ctrl-d(down) - File:
gg(start of file),G(end of file) - Line numbers:
:{number}<CR>or{number}G(line {number}) - Misc:
%(corresponding item) - Find:
f{character},t{character},F{character},T{character}- find/to forward/backward {character} on the current line
,/;for navigating matches
- Search:
/{regex},n/Nfor navigating matches
Edits#
Everything that you used to do with the mouse, you now do with the keyboard using editing commands that compose with movement commands. Here's where Vim's interface starts to look like a programming language. Vim's editing commands are also called "verbs", because verbs act on nouns.
ienter insert mode- but for manipulating/deleting text, want to use something more than backspace
o/Oinsert line below / aboved{motion}delete {motion}- e.g.
dwis delete word,d$is delete to end of line,d0is delete to beginning of line
- e.g.
c{motion}change {motion}- e.g.
cwis change word - like
d{motion}followed byi
- e.g.
xdelete character (equal dodl)ssubstitute character (equal toxi)- visual mode + manipulation
- select text,
dto delete it orcto change it
- select text,
uto undo,<C-r>to redoyto copy / "yank" (some other commands likedalso copy)pto paste- Lots more to learn: e.g.
~flips the case of a character
Counts#
You can combine nouns and verbs with a count, which will perform a given action a number of times.
3wmove 3 words forward5jmove 5 lines down7dwdelete 7 words
Modifiers#
You can use modifiers to change the meaning of a noun. Some modifiers are i,
which means "inner" or "inside", and a, which means "around".
ci(change the contents inside the current pair of parenthesesci[change the contents inside the current pair of square bracketsda'delete a single-quoted string, including the surrounding single quotes