-
Notifications
You must be signed in to change notification settings - Fork 326
Useful vim commands
Brian "Beej Jorgensen" Hall edited this page May 22, 2019
·
4 revisions
This is a collection of command examples that are commonly used by instructors here at Lambda School.
This is not a complete reference or a tutorial. It is just the commands that we commonly use in order to edit quickly. See the links for more in-depth information.
-
[ESC]
means to hit the escape key. -
[RET]
means to hit the return key. -
CTRL-x
means hold down control andx
.
- OpenVim Tutorial
- Vim Adventures
- Vim Reference Card (utexas)
- Vim Reference Card (rtorr)
- Vim Quick Reference Card (vimqrc)
-
I
: insert at the beginning of the line -
A
: append starting at end of line -
D
: delete to end of line -
C
: change from here to end of line -
rX
: replace the current character with character X -
R
: start replacing characters from the cursor on -
~
: change the case of a character -
dd
: delete (cut) the current line -
dw
: delete the current word -
d$
: delete from cursor to end of line -
dtX
: delete all characters up to the next occurrence of X in the line -
dTX
: delete all characters down to the previous occurrence of X in the line -
ctX
: change the characters from the cursor to the next occurrence of X in the line
-
h
j
k
l
: move the cursor left, down, up, and right faster than reaching for the arrow keys -
$
: move to end of line -
^
: move to the first non-space character in the line -
0
: move to the beginning of the line -
tX
: move to the next X character in the line -
TX
: move to just after the previous X character in the line -
e
: move to the end of the next (or current) word -
b
: move to the beginning of the previous (or current) word -
w
: move to the beginning of the next (or current) word -
CTRL-f
: move forward a page (page down) -
CTRL-b
: move backward a page (page up) -
*
: move to the next occurrence of the word under the cursor -
#
: move to the previous occurrence of a word under the cursor -
%
: move to the open/close brace/bracket/paren paired with the one under the cursor -
/foo[RET]
: search for the next occurrence offoo
in the file -
?foo[RET]
: search for the previous occurrence offoo
in the file -
n
: repeat the last search -
N
: repeat the last search, but in the other direction
-
ma
: set markera
at the cursor location (can use any letter a-z) -
`a
: jump to markera
(can use any letter a-z)
-
v
: start a cursor-oriented visual block -
V
: start a line-oriented visual block -
CTRL-v
: start a rectangular visual block
Once a block is selected:
-
c
: change the block to different text (with rectangular blocks, this makes one cursor per line) -
d
: delete the block -
y
: yank (copy) the block -
:s/foo/bar/g[RET]
: replace all occurrences offoo
withbar
in the block (this will show up as:'<,'>
when you first start typing)
The following use regexes:
-
:s/foo/bar[RET]
: replace the first occurrence offoo
withbar
on the current line -
:s/foo/bar/g[RET]
: replace all occurrences offoo
withbar
on the current line -
:%s/foo/bar/g[RET]
: replace all occurrence offoo
withbar
on all lines in the file
-
dd
: delete (cut) the current line -
dw
: delete the current word -
d$
: delete from cursor to end of line -
dtX
: delete all characters up to the next occurrence of X in the line -
dTX
: delete all characters down to the previous occurrence of X in the line -
x
: delete the character under the cursor -
yy
: copy the current line -
yw
: copy the current word -
y$
: copy from cursor to end of line -
p
: paste after cursor position -
P
: paste before cursor position
GUI-based vims often support COMMAND-c
/CTRL-c
and COMMAND-v
/CTRL-v
for copying and pasting from the system buffer. (Note that the system copy/paste buffer is different from that used by the y
and p
commands.) If you're using the console-based vim, you might have to select the text to copy with the mouse if you want to copy it to the system copy/paste buffer.
-
20ix[ESC]
: insert 20x
characters -
15~
: change the case of the next 15 characters -
5k
: move the cursor up 5 lines -
12yy
: copy 12 lines
-
:q[RET]
: quit unless the file has been modified -
:q![RET]
: quit even if the file has been modified, discarding changes -
ZZ
: save the file (only if it has been modified) and quit -
:qa[RET]
: quit all open tabs -
:qa![RET]
: quit all open tabs even if the files have been modified, discarding changes
-
.
: repeat the last edit command, e.g.AHello[ESC]j.
will addHello
to the end of the current and next line -
xp
: transpose the character under the cursor with the next one; not really a special command--simplyx
to cut the current character under the cursor followed byp
to paste it after the cursor position -
!!ls[RET]
: run thels
command and insert its output into the document (can run any command) -
:sh[RET]
: spawn an interactive shell (hitCTRL-d
or typeexit[RET]
to get back to vim)
- WIP
-
:q[RET]
: close the current window or tab (if multiple windows or tabs are open)
- WIP
-
:q[RET]
: close the current window or tab (if multiple windows or tabs are open)
- WIP