Vim Cheatsheet
Cursor Navigation
Vim has several commands for moving the cursor around the file. Here are the basic ones, which work in Normal mode:
h
: Move cursor leftj
: Move cursor downk
: Move cursor upl
: Move cursor right
These are some more advanced commands:
-
w
: Move to start of next word -
b
: Move to start of previous word -
e
: Move to end of word -
0
(zero): Move to start of line -
^
: Move to first non-blank character of line -
$
: Move to end of line -
shift g
: Move to end of file -
gg
: Move to start of file -
<number> shift g
: Move to line number. For example,10G
will move to line 10. -
shift h
: Move to top of screen -
shift m
: Move to middle of screen -
shift l
: Move to bottom of screen -
:<number>
: Move to line number. For example,:10
will move to line 10. -
%
: Move to matching parenthesis -
*
: Search for the word under the cursor -
#
: Search backwards for the word under the cursor -
n
: Move to next occurrence of search term -
N
: Move to previous occurrence of search term
Vim also supports searching with /
(search forwards) and ?
(search backwards). For instance, /text
will move the cursor to the next occurrence of “text”, and ?text
will move it to the previous occurrence. After a search, you can use n
and N
to navigate between occurrences.
Remember that these commands are meant for Normal mode. If you are in Insert mode, you would need to press Esc
first to return to Normal mode.
Scrolling
Here are some common scrolling commands you can use in the Vim text editor:
Ctrl+d
: scrolls down (toward the end of the file) half a screen.Ctrl+u
: scrolls up (toward the start of the file) half a screen.Ctrl+f
: scrolls forward down the page (equivalent to Page Down).Ctrl+b
: scrolls back up the page (equivalent to Page Up).Ctrl+e
: scrolls the window up by one line.Ctrl+y
: scrolls the window down by one line.zz
: centers the screen around the cursor.zt
: puts the line with the cursor at the top of the window.zb
: puts the line with the cursor at the bottom of the window.
It’s important to remember that in Vim, the cursor stays where it is when you scroll. It doesn’t move with the screen like it does in some other editors. This allows you to move the view to a part of the file while keeping your place.
Switching modes
Here’s a summary of the keys you can use to switch between different Vim modes:
-
Normal mode to other modes:
i
: switch to Insert modea
: switch to Insert mode, but start inserting after the current cursor positiono
: switch to Insert mode, opening a new line below the current oneshift o
: switch to Insert mode, opening a new line above the current oneI
: switch to Insert mode, but start inserting at the beginning of the lineA
: switch to Insert mode, but start inserting at the end of the linev
: switch to Visual modeV
: switch to Visual Line modeCtrl+v
: switch to Visual Block mode:
: switch to Command-line mode/
: switch to Command-line mode with a forward search?
: switch to Command-line mode with a backward search!
: switch to Command-line mode for executing shell commandsR
: switch to Replace modeQ
: switch to Ex mode
-
Other modes back to Normal mode:
Esc
: switch back to Normal mode from any other mode
-
Visual mode to Select mode:
Shift+v
: switch to Select modeCtrl+g
: switch to Select mode
Remember that while in Insert and Command-line mode, pressing Ctrl+[
or Ctrl+c
will also take you back to Normal mode. It’s often handy to use these combinations if your keyboard has the Esc
key in an inconvenient location.
Operations
Here are some common operations you can perform in Vim:
x
: Delete the character under the cursorshift x
: Delete the character before the cursord(motion)
: Delete from the cursor to wherever the motion takes youy(motion)
: Yank (copy) from the cursor to wherever the motion takes youc(motion)
: Delete from the cursor to wherever the motion takes you and start insert modegu(motion)
: Change the text from the cursor to wherever the motion takes you to lowercasegU(motion)
: Change the text from the cursor to wherever the motion takes you to uppercase
Motion modifiers
i
: Inside. For example,di"
will delete everything inside the next pair of quotes.a
: Around. For example,da"
will delete everything inside the next pair of quotes, including the quotes.t
: Till. For example,dt"
will delete everything up to (but not including) the next pair of quotes.f
: Find. For example,df"
will delete everything up to (and including) the next pair of quotes.shift f
: Find backwards. For example,dF"
will delete everything up to (and including) the previous pair of quotes.
These can be followed by cursor navigation commands to perform the operation on a larger piece of text. For example, d2w
will delete the next two words, and c3j
will delete the next three lines and start insert mode.