Vim beginners guide and cheat sheet
July 14, 2021 / 12 min read / 2,500 , 1 , 0
Last updated: September 30, 2022
The command line text editor vim
is popular in the world of programming,
and for good reason. It can be found on nearly any Unix (and often Windows)
system, making it ideal for loading into remote machines and making quick edits
to files. It is also very powerful in the right hands. If you watch an
expert vim
user code with vim
, the cursor is bouncing all over
as they quickly write or edit their files, all without ever touching a mouse...
I am not one of those people. I picked up vim
out of necessity while
working on remote hosts without access to a GUI text editor (I'm partial
to VS Code). But since I needed to use vim
, I wanted to get better at it, so I
took a
vim Udemy course,
and I've been using what I've learned to get a little more efficient in vim
.
If you're a vim
beginner, this post will teach you the commands I found
most useful while learning vim
. And if you're a casual vim
user
like me, this post can serve as a cheat sheet reminder for all the vim
commands you and I will continue to forget.
The essentials
These are the set of commands that you need to enter a file, make changes, and get out (with or without saving).
-
Enter into a file for editing: Type:
vim some_file.txt
orvi some_file.txt
to enter a file withvim
. -
Exit a file without saving: First press
ESC
to enterNormal Mode
. Then type:q!
+ENTER
.q
here means "quit" while!
means "force", andvim
won't exit without it if you've made any changes. -
Save a file and exit: First press
ESC
to enterNormal Mode
. Then type:wq
+ENTER
.w
means "write" here so you can also just use:w
+ENTER
withoutq
to save the file without exiting. -
Enter
Insert Mode
: This is the mode where you can add text to the file. While inNormal Mode
, pressi
(for "insert"). Recall that you can pressESC
whenever you want to exitInsert Mode
to go back toNormal Mode
.
Navigation
These commands will get you around the file with ease. Notice where I use
capital
vs lowercase
letters, as I might not always point it out.
Case matters in vim
. For all of the following commands in this section,
unless otherwise specified, I'll assume you are in Normal Mode
(recall you can get there by hitting ESC
).
-
Move up, down, left, and right: There are two main ways to move in these directions. You can use the
up
,down
,left
, andright
arrow keys in any mode. If you are inInsert Mode
, these are the only keys for navigating. However, fromNormal Mode
, you can navigate in these directions without leaving typing position:h
isleft
,j
isup
,k
isdown
, andl
isright
. Notice the left-most key of these 4 keys (h
) isleft
, the right-most key (l
) isright
, and the middle two are (j
)up
followed by (k
)down
. -
Go to the start of the file: Press
g
+g
to go to the start of the file. -
Go to the end of the file: Press capital
G
(ieSHIFT-G
) to go to the end of the file. -
Go to a specific line in the file: Type
:LIN_NUM
+ENTER
to go to that line number. Eg.:25
+ENTER
to go to line 25. -
Go to the start of the current line: Type
0
(zero) to go to the start of the current line. You can also press^
(carrot)(ieSHIFT-6
) to go to the first non-blank character of the line. You might recall that^
is the start character forregex
expressions. -
Go to the end of the current line: Type
$
(dollar sign)(ieSHIFT-4
) to go to the end of the current line. You might recall$
is the end character forregex
expressions. -
Move forward one word: Type
w
to move your cursor to the next word. I find this to be a quick way of moving through a line. -
Search for a word: type
/
+ SEARCH +ENTER
to search for a word. E.g./foo
+ENTER
will search for all instances of "foo" in the text. Note if thehlsearch
setting is set, found searches will start highlighting as you type. To go to the next found search item, pressn
, or to go to the previous search item, press capitalN
. You can remove the previous search's highlight by typing:noh
(for no highlight), or if you can't remember this (and I just looked:noh
up so obviously I can't), just search for something new that doesn't exist. Eg./asdfasdfas
+ENTER
.
Entering Insert Mode
with style
There are a host of ways to enter Insert Mode
to start writing
new text. Here are the most useful ones. I will assume you are already in Normal mode
(ESC
),
for all these commands.
-
Enter
Insert Mode
in place: Pressi
to enterInsert Mode
one character before the character your cursor is on. I already mentioned this one in section one. -
Enter
Insert Mode
at line beginning: Press capitalI
to enterInsert Mode
at the beginning of the line containing your cursor. -
Enter
Insert Mode
with append: Pressa
to enterInsert Mode
one character after the character your cursor is on. -
Enter
Insert Mode
at line end: Press capitalA
to enterInsert Mode
at the end of the line containing your cursor. -
Enter
Insert Mode
below your current line: Presso
to create a new line below the line containing your cursor and enterInsert Mode
there. Note this will not break the line in half if your cursor is mid-line. -
Enter
Insert Mode
above your current line: Press capitalO
to create a new line above the line containing your cursor and enterInsert Mode
there.
Undo, Redo
Sometimes you make a mistake and need to un-make that mistake.
You could :q!
to exit without saving, but let's see if we can find a
less drastic solution. These commands should be performed from
Normal Mode
(ESC
).
-
Undo: Press
u
to undo. UppercaseU
undoes all the latest changes on one line (which I don't ever use). -
Redo: Press
Ctrl-r
to redo.
Deleting things (and cut)
There are multiple ways to delete different-sized chunks of code in vim.
Let's take a look at some of the more common ones. d
stands for delete in vim
so
commands focus around this key. Note that vim
always saves the last deleted
thing to the default register
(more on registers
later). So in a sense,
there is no delete
, just cut
with short-term memory. All these commands
should be performed from Normal Mode
(ESC
).
-
Delete/cut a letter: Press
d
+l
to delete a letter and save it to the defaultregister
. -
Delete/cut a word: Press
d
+w
to delete a word and save it to the defaultregister
. -
Delete/cut a line: Press
d
+d
to delete a line and save it to the defaultregister
.
Also, note that while in Insert Mode
you can use BACKSPACE
and DEL
, as usual,
to delete characters before or after your cursor respectively. This form
of deletion does not save to the default register
.
Copy/paste
We've already seen how to cut
(the same as deleting). Here we see
how to copy
items to the register and also to paste
items from the register.
Copying in vim
is called yank
ing, so those commands use y
.
All these commands should be performed from Normal Mode
(ESC
).
-
Copy/yank a letter: Press
y
+l
to copy a letter to the defaultregister
. -
Copy/yank a word: Press
y
+w
to copy a word to the defaultregister
. -
Copy/yank a line: Press
y
+y
to copy a line to the defaultregister
. -
Paste after the cursor: Press
p
to paste whatever is in the defaultregister
immediately after the character the cursor is hovering over. -
Paste before the cursor: Press capital
P
to paste whatever is in the defaultregister
immediately before the character the cursor is hovering over.
What is this register
I keep mentioning? Briefly, a register
can store
copied/cut text. A register can be "named". For example, to copy to the b
named register
you could type "b
followed by your yank
. And then to
use that yank
ed text, type "b
+ p
. The default register
is where
text is stored and pasted from if you don't use a named register.
Only the latest copy/cut text is stored in a register
. Subsequent
stores override the previous store. If registers
interests you,
give them a quick google search for more details.
Also, note these commands are only for copy/paste from within vim. Copying and pasting
from the system clipboard is trickier. While it is possible with key
commands, usually I've had success just using a good old fashion mouse
right-click
+ click dropdown item
for this purpose, so just do that.
Replace Mode
Replace Mode
is like Insert Mode
, except all typed characters overwrite
whatever the cursor is hovering. These commands should begin in Normal Mode
(ESC
).
-
Replace a single character: Place cursor over the character to be replaced and press
r
+ NEW CHARACTER (ier
+f
) to replace the hovered character with "f". Vim will immediately re-enterNormal Mode
after replacing this character. -
Replace multiple characters: Place cursor over the character you want to start replacing at and press capital
R
. Vim will enterReplace Mode
at that character and all subsequent characters typed will replace the following characters on that line. For instance with the cursor over the "d" from "dog", I could typeRcat
to replace "dog" with "cat". To exit replace mode you can hitESC
.
Doing things in multiples
Vim has a cool sentence structure like syntax that you can use to perform
actions. We saw this earlier with commands like d
+ w
for "delete a word".
Adding a number at the start of these sentence-like commands will perform that
command that number of times. For instance to "delete 3 words" I could type
3
+ d
+ w
and the hovered word plus the next two will be deleted.
To copy (yank) 2 lines, I can type 2
+ y
+ y
. And to undo the last 4
actions I could type 4
+ u
. Neat, eh?
Bonus: Vim settings
I think vim
is much nicer when it is configured the way you like it. Here's
a list of settings I like to use when running vim. Put these settings
(plus or minus the ones you like) into a file at ~/.vimrc
. In the file,
"
at the start of a line means it is a comment.
" use syntax highlighting
syntax on
" Show line number
set number
" Show the cursor position
set ruler
" Show incomplete commands
set showcmd
" Highlight searched words
set hlsearch
" Incremental search
set incsearch
" Search ignore case unless capitalized letters used in search
set ignorecase
set smartcase
" Don't line-break mid-word
set lbr
" Set auto-indent (auto indent when the previous line was indented for coding)
set autoindent
" Set smart indent (smartly indent when code indicates indent should occur)
set smartindent
" Replace tab with spaces
set expandtab
" Set tab spacing to 4
set tabstop=4
set shiftwidth=4
set softtabstop=4
Conclusions
These were the vim
commands (and settings) I found most useful in breaking
past the bare minimum of what it takes to code in vim
.
As I said earlier, I am by no means a vim
expert. I've only recently been
using vim
commands more complicated than the ones I listed in "vim essentials".
But now that I've learned some of these neat commands, I have to say,
vim is not so scary anymore and is even pretty pleasant to code in.
Vim is very powerful and there is a lot you can do with it. I'm sure vim
experts would disagree with some of my choices for a curated list of commands.
A couple of other vim
topics I left out that you should look into if you want
to get "good" with vim
are:
- Using the
vim
manual to look all these commands and ideas up yourself - Macros (save a series of
vim
commands to 1 or 2 keystrokes) vim
replace (replace words/phrases found withvim
search)Visual Mode
(make more complex edits in grid-like patterns)- Many others I can't think of right now
If you are interested in a vim
deep dive, I highly recommend the
Vim masterclass Udemy course
where you can learn these commands and many others and how to
"think like a vim
user" to really speed up your vim
coding.