Professional Documents
Culture Documents
dmsul / vim_crash_course.md
Last active last month • Report abuse
Star
vim_crash_course.md
NOTE: Specific examples given for options, flags, commands variations, etc., are not
comprehensive.
NORMAL MODE
Vim has 2 main "modes", that chance the behavior of all your keys. The default mode
of Vim is Normal Mode and is mostly used for moving the cursor and navigating the
current file.
Some important (or longer) commands begin with ":" and you will see the text you
enter next at the bottom left of the screen.
:q[uit] - quit (the current window of) Vim. ("Window" here is internal to Vim, not if
you have multiple OS-level windows of Vim open at once.)
:q! - force quit (if the current buffer has been changed since the last save)
:e[dit] {filename} - read file {filename} into a new buffer.
Vim doesn't "open" files like MS Word does. Instead, it reads the contents of a file
into RAM and then you edit the "buffer" in RAM. Other programs may access and
change the underlying file you originally opened (Vim will notice this and issue a
warning). :w[rite] {filename} - write the current buffer to {filename}. If no
filename passed (i.e., :w ) and the buffer already has an associated filename (the
one used with :e {filename} , that associated filename will be used.
A simple :w is like MS Word "save" and :w {new_filename} is "save a copy as
{new_filename} but keep the current buffer.
:save {filename} - Save as (and change current buffer to {filename})
:w! - force write (e.g., even if the underlying file has been been changed by another
program and this write will overwrite those changes)
1 of 9 5/7/21, 20:09
Vim Crash Course https://gist.github.com/dmsul/8bb08c686b70d5a68da0...
j - down
k - up
h - left
l - right
% - If cursor inside parens (or similar), move to opening paren. If cursor on paren,
move to partner paren. If not between parens, do nothing. (Can do weird stuff with
unmatched parens.)
w - forward one word (alphanumeric or _; or consective chars that are not those)
e - end of current word (or next word if currently at end of word)
b - to beginning of word (or previous word if currently at beginning)
} - Move forward a paragraph. { - Move back a paragraph.
gg - go to beginning of file
[#]gg - go to line [#] (Example 20gg goes to line 20)
G - go to end of file
[#]| - go to column [#] on current line, e.g. 10| goes to col 10.
2 of 9 5/7/21, 20:09
Vim Crash Course https://gist.github.com/dmsul/8bb08c686b70d5a68da0...
Note: Most motion commands can take a number before them to execute that
number of times.
4j - move cursor down 4 times
10w - move forward 10 "words"
VISUAL "MODE"
v[m] - Go into visual (highlight) mode to select characters. Use usual motion
commands to add to selection as cursor moves.
V[m] - Visual mode, but grab whole lines at a time, not characters.
c-v[m] - Visual mode, but select vertically (by columns) instead of horizontally
ggvG - Highlight whole file, i.e., go to beginning of file ( gg ), enter visual mode ( v ),
go to end of file ( G ).
Once a selection has been made, you can use an edit command (see below) on that
selection and it will (usually) behave as you'd expect.
INSERT MODE
3 of 9 5/7/21, 20:09
Vim Crash Course https://gist.github.com/dmsul/8bb08c686b70d5a68da0...
Most commands are executed in Normal Mode. If you actually want to add to the text
(type a "j" instead of moving down one line) you need to enter Insert Mode. There are
several ways to do this.
I - Enter insert mode at the beginning of the current line's text (same as doing ^
then i )
A - Enter insert mode After the end of th current line (like $ then a )
o - Add a new line after the current line and enter insert mode (like doing
A<Enter> )
O - Add a new line before the current line and enter insert mode
c[m] - Change text from here to [m]. "Change" deletes the text and immediately
enters insert mode.
cc - Change this whole line.
C - Changes from here to end of line.
r - replace the current character with the next character typed (only one character
allowed)
R - Replace characters until <Esc> (like replace/overwrite mode in Word)
More generally, the composability of editing commands and motion is what makes
Vim particularly powerful. If you know 5 editing commands and 5 motion commands,
you actually know 25 specific and easy to remember commands.
4 of 9 5/7/21, 20:09
Vim Crash Course https://gist.github.com/dmsul/8bb08c686b70d5a68da0...
dd - delete this entire line 4dd - delete this line and next 3 (4 total)
D - delete from here to end of line
NOTE: deleting text puts that text into the default register, so if you yy , move to
another line, then dd , you'll lose whatever whatever you yanked with yy . There is a
plugin to override this behavior (I think by Tim Pope).
Change case
5 of 9 5/7/21, 20:09
Vim Crash Course https://gist.github.com/dmsul/8bb08c686b70d5a68da0...
Indentation
J - Take the next line and move it to the end of this line.
gq[m] - Format the text between here and [m] (usually just breaks overly long
lines to smaller lines, by default 80 characters).
gqq - Format the current line
u - undo
c-r - redo
. - repeat last edit (including insert, replace, indent, etc.) Note: EXTREMELY
useful.
A;;<Esc>j. - Enter insert mode at end of line ( A ), type two semi-colons ( ;; ), exit
Insert Mode ( <Esc> ), move down one line ( j ), and add two semi-colons to the end
of this new line ( . )
>>... - Indent this line >> , then indent it another three times ( ... )
Substitution
:<range>s/<re>/<str>/<flags> - substitute first instance of <re> in each line in
<range> with <str> . <flags> change default behavior.
<range> :
6 of 9 5/7/21, 20:09
Vim Crash Course https://gist.github.com/dmsul/8bb08c686b70d5a68da0...
. - current line
<flags> :
c - confirm (will highlight next instance of <re> and ask you to press "y"
to execute change
i - case insensitive
Specific sub: Use \zs and \ze to demark a sub-RegEx within the matched RegEx
that should be substituted. Example:
This changes all instances of "Year 2007 is over" to "Year 2008 is over", but not all
"2007" to "2008".
daw - Delete "a" "word". Deletes the current word and space(s) after it.
diw - Delete "inner" "word". Deletes current word but not trailing space(s).
dap - Delete a paragraph. A "paragraph" to Vim is a group of consecutive lines of
text between an empty line(s).
yaw - Yanks a word.
yiw - Yanks inner word.
ya( - Yank a parenthetical. If cursor between (), yank everything between those
parens and the parens themselves. Else do Nothing.
yi( - Yank inner parenthetical. Like ya( , but excluding the parens.
ya) - ya(
ya[ - Same as ya( but for brackets [] .
ya" - Same
7 of 9 5/7/21, 20:09
Vim Crash Course https://gist.github.com/dmsul/8bb08c686b70d5a68da0...
This takes what you've already typed (either word or line) and uses the text in the
rest of any open buffers to predict what will come next. If there's only one possibility,
it will be filled in. If there are several possibilities, it will give you the choices.
Vim autocomplete is pretty nice, but there are better, automated predictive text
solutions (specifcally, the package YouCompleteMe). However, I've found they're a
bit too demanding for most laptops and only useful on desktop computers.
Macros
Marks
8 of 9 5/7/21, 20:09
Vim Crash Course https://gist.github.com/dmsul/8bb08c686b70d5a68da0...
Lower case are local, upper case are global (across files). Jumping to a mark is a
standard motion command for deleting, yanking, etc.
ma - sets mark on current cursor location (line and column), WLOG, called "a"
'a - jump to line of "a" (first non-blank character of line)
`a - jump to position of "a" (line and col)
:marks - lists all current marks
:delmarks <args> - delete specific marks
:delmarks! - delete all lowercase in buffer
]' , [' - jump to next (previous) line with a lowercase mark
]` , [` - jump to next (previous) lowercase mark
Spellcheck
9 of 9 5/7/21, 20:09