Vim beginners guide and cheat sheet
July 14, 2021 / 12 min read / 1,293 , 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
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
vim Udemy course,
and I've been using what I've learned to get a little more efficient in
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
like me, this post can serve as a cheat sheet reminder for all the
commands you and I will continue to forget.
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:
vi some_file.txtto enter a file with
Exit a file without saving: First press
Normal Mode. Then type
qhere means "quit" while
!means "force", and
vimwon't exit without it if you've made any changes.
Save a file and exit: First press
Normal Mode. Then type
wmeans "write" here so you can also just use
qto save the file without exiting.
Insert Mode: This is the mode where you can add text to the file. While in
Normal Mode, press
i(for "insert"). Recall that you can press
ESCwhenever you want to exit
Insert Modeto go back to
These commands will get you around the file with ease. Notice where I use
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
(recall you can get there by hitting
Move up, down, left, and right: There are two main ways to move in these directions. You can use the
rightarrow keys in any mode. If you are in
Insert Mode, these are the only keys for navigating. However, from
Normal Mode, you can navigate in these directions without leaving typing position:
right. Notice the left-most key of these 4 keys (
left, the right-most key (
right, and the middle two are (
upfollowed by (
Go to the start of the file: Press
gto go to the start of the file.
Go to the end of the file: Press capital
SHIFT-G) to go to the end of the file.
Go to a specific line in the file: Type
ENTERto go to that line number. Eg.
ENTERto 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
SHIFT-6) to go to the first non-blank character of the line. You might recall that
^is the start character for
Go to the end of the current line: Type
SHIFT-4) to go to the end of the current line. You might recall
$is the end character for
Move forward one word: Type
wto 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 +
ENTERto search for a word. E.g.
ENTERwill search for all instances of "foo" in the text. Note if the
hlsearchsetting is set, found searches will start highlighting as you type. To go to the next found search item, press
n, or to go to the previous search item, press capital
N. 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
:nohup so obviously I can't), just search for something new that doesn't exist. Eg.
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 (
for all these commands.
Insert Modein place: Press
Insert Modeone character before the character your cursor is on. I already mentioned this one in section one.
Insert Modeat line beginning: Press capital
Insert Modeat the beginning of the line containing your cursor.
Insert Modewith append: Press
Insert Modeone character after the character your cursor is on.
Insert Modeat line end: Press capital
Insert Modeat the end of the line containing your cursor.
Insert Modebelow your current line: Press
oto create a new line below the line containing your cursor and enter
Insert Modethere. Note this will not break the line in half if your cursor is mid-line.
Insert Modeabove your current line: Press capital
Oto create a new line above the line containing your cursor and enter
Sometimes you make a mistake and need to un-make that mistake.
: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 (
uto undo. Uppercase
Uundoes all the latest changes on one line (which I don't ever use).
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
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
cut with short-term memory. All these commands
should be performed from
Normal Mode (
Delete/cut a letter: Press
lto delete a letter and save it to the default
Delete/cut a word: Press
wto delete a word and save it to the default
Delete/cut a line: Press
dto delete a line and save it to the default
Also, note that while in
Insert Mode you can use
DEL, as usual,
to delete characters before or after your cursor respectively. This form
of deletion does not save to the default
We've already seen how to
cut (the same as deleting). Here we see
copy items to the register and also to
paste items from the register.
vim is called
yanking, so those commands use
All these commands should be performed from
Normal Mode (
Copy/yank a letter: Press
lto copy a letter to the default
Copy/yank a word: Press
wto copy a word to the default
Copy/yank a line: Press
yto copy a line to the default
Paste after the cursor: Press
pto paste whatever is in the default
registerimmediately after the character the cursor is hovering over.
Paste before the cursor: Press capital
Pto paste whatever is in the default
registerimmediately 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
register you could type
"b followed by your
yank. And then to
yanked text, type
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
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
click dropdown item for this purpose, so just do that.
Replace Mode is like
Insert Mode, except all typed characters overwrite
whatever the cursor is hovering. These commands should begin in
Replace a single character: Place cursor over the character to be replaced and press
r+ NEW CHARACTER (ie
f) to replace the hovered character with "f". Vim will immediately re-enter
Normal Modeafter replacing this character.
Replace multiple characters: Place cursor over the character you want to start replacing at and press capital
R. Vim will enter
Replace Modeat 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 type
Rcatto replace "dog" with "cat". To exit replace mode you can hit
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
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
w and the hovered word plus the next two will be deleted.
To copy (yank) 2 lines, I can type
y. And to undo the last 4
actions I could type
u. Neat, eh?
Bonus: Vim settings
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
These were the
vim commands (and settings) I found most useful in breaking
past the bare minimum of what it takes to code in
As I said earlier, I am by no means a
vim expert. I've only recently been
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
- Using the
vimmanual to look all these commands and ideas up yourself
- Macros (save a series of
vimcommands to 1 or 2 keystrokes)
vimreplace (replace words/phrases found with
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