Vim. Everybody’s heard of it, most don’t even know how to get out of it. What good is a powerful tool if it’s so intimidating that even nerds don’t bother to actually use it? This post is a guide from a beginner’s point of view to the infamous text editor Vim. It’s aim is to teach you just enough of Vim so that you too can finally begin to understand how to use Vim.
When I opened Vim for the first time, I was lost. In reality, however, Vim is not that hard to learn … it’s just different. So when nobody ever showed you how to use it, it’s not surprising that you find Vim strange and hard to learn. Let me be that person for you. Once someone provided you an insight into the workings of Vim, you will find that many of Vim’s commands are actually quite easy to learn. Let’s say you want to delete the word selected with your cursor. Press
d for delete, then
w for word. You want to delete the text between two quotation marks, e.g.,
example text in
"example text"? Press
d for delete, then
i for in-between (or inside), then
" for the pair of delimiting characters. The same way you can delete the text between parentheses with the combo
di) or the text between two brackets with
di]. You see, using Vim is not that hard. Simply tell yourself what you want to accomplish (e.g., “delete this word”) and in many cases you’ll intuitively stumble upon the command for that task. For that reason there’s no need to be intimidated by the number of keyboard shortcuts Vim has to offer. With that in mind, you will undertake your first steps in no time. But let’s not get ahead of ourselves and start at the beginning.
Moving around in the text document
When Vim (or rather, its predecessor Vi) was first developed in 1976, computer mice weren’t as common as they are today. The keyboard of Vi’s developer didn’t even have arrow keys. For this historic reason, the cursor is moved with the keys
l. The “outer” two keys
l are left and right. The “inner” two keys
k are down and up. Although today’s keyboard do have arrow keys, you should nonetheless stick to
l, since these keys are on your keyboard’s so-called home row (your hand’s natural resting position on the keyboard). Your goal should be to always keep your hands at the keyboard and also to move your hands as little as possible. Moving your hand back and forth between your keyboard and mouse is comparatively slow. For this reason you want to avoid using the mouse and its scroll-wheel. The same is true for moving your hand between the home row and the arrow keys. Let your index finger rest on the
j key (you’ll feel the little bump on that key). Your index finger will be responsible for pressing the
j keys. You middle finger will be responsible for pressing the
k key. Your ring finger will be responsible for pressing the
l key. Using
l may be weird at first, but you’ll quickly get used to them.
These four keys can be all you ever need to move around in your text document, but the wonderful thing about Vim is that there always is a better way to achieve the same thing faster (or with less keystrokes). Once you grow comfortable to using
l, you may want to accelerate your movements and jump over entire words, using
w to skip to the first character of the next word or
e to skip to the end, i.e., the last character. You can always strive towards becoming as efficient as possible by requiring as little motion as possible and as few keystrokes as possible. There probably are always some ways to be quicker and shave off some more keystrokes, but for now focus on using these four arrow replacements keys as well as
e to jump a word forward and
b to jump a word backward. You don’t have to learn all possible keyboard combinations before you can begin using Vim. Instead, start with baby steps and one of the million different ways that gets the job done, two or three new commands at a time, and once you get used to them, google how to get the job done faster.
A good tip is to disable the arrow keys completely so that you break the habit of using them and to get used to this strange new way of navigating. In your home directory, open or create a file called
.vimrc (assuming you are using Linux or macOS) and paste these lines into the file:
" Disable arrow keys in normal mode ... nnoremap <Left> :echoe "Use h"<CR> nnoremap <Right> :echoe "Use l"<CR> nnoremap <Up> :echoe "Use k"<CR> nnoremap <Down> :echoe "Use j"<CR> " ... and in insert mode inoremap <Left> <ESC>:echoe "Use h"<CR> inoremap <Right> <ESC>:echoe "Use l"<CR> inoremap <Up> <ESC>:echoe "Use k"<CR> inoremap <Down> <ESC>:echoe "Use j"<CR>
Modes in Vim
Writing code and writing English prose (for example, a blog post) are two very different forms of writing. For starters, when you edit code you don’t usually type one continuous stream of prose. Writing code requires much more navigation and jumping around. This could mean looking at a function definition and different places where that function is used. It makes sense to use different kinds of text editors for writing different kinds of text. To be better suited for writing code, there are different modes in Vim. There’s one mode for inserting/writing new text. There’s another mode for selecting large chunks of existing text to copy, delete or move blocks of text. There’s a mode for fine granular changes like fixing a typo, i.e., replacing just a single character instead of the entire word. The mode you will spend most of your time in is called
NORMAL mode. After making a change, you always and right away switch back to the default mode, i.e., the
NORMAL mode. In this
NORMAL mode, the keys
l move the cursor instead of writing the literal characters h, j, k, or l. If you wanted to actually write the letters h, j, k, or l (or any other letter), you could switch to
INSERT mode. You can switch to the
INSERT mode by pressing
i when in
NORMAL mode. In order to leave a mode and return to the
NORMAL mode, simply press the Escape key. So to actually make your first edit using Vim, first ensure that you are in
NORMAL mode (press the Escape key if not), then move the cursor to where you want it to be using the keys
b. Next, press
i to enter
INSERT mode. Now you can make all the edits you want. When you’re done editing, press Escape to go back to
How to exit Vim
Maybe you already know programming languages such as Python where you can type the
quit() function into the terminal to exit the interactive mode. In Vim it is similar, except that you first need to switch to the mode in which you may enter such commands. You enter this command mode by typing the colon character
:. (This activates just another mode, just like the
NORMAL mode or the mode for inserting or selecting text.) In this mode, you can type your desired command. That is, you can leave Vim by typing
:q. However, similar to programs like Microsoft Word, the program may ask you what it should do with unsaved changes in case you modified the document. Should the program save your changes or should it discard them? That’s something you need to tell Vim. Vim doesn’t call it “saving” but “writing”. So if you made changes to the document and want to exit, you can’t just quit, but you also need to save the changes in case you wish so. In such cases, you exit by typing
:wq. If you just want to get out of Vim without saving your changes, but cannot exit using
:q because there are unsaved changes, you need to use
:q! to exit Vim.
Practise Using Vim
By now, you already know enough to start learning Vim by playing a browser game called Vim Adventures. Have some fun with the game and take your first steps in learning how to navigate Vim.
You could also go ahead and use Vim right now to disable the arrow keys. To do so, first copy the text provided above. Now open the mentioned file by typing the following command into your terminal. In case the file does not exist yet, that command also creates the file.
You can paste text inside Vim by pressing
p by itself only pastes what you last copied within Vim. That is, it does not paste what’s in your operating system’s clipboard (i.e., what you copied from, for example, a website). If you want to paste what’s in your system clipboard, you need to press the keys
p in that order.1 Now the text to disable the arrow keys should have been pasted into the file
~/.vimrc. Switch to
NORMAL mode by pressing the Escape key if you are in a different mode. You can now save the file and exit by typing
If you still feel a little unsure and need some more guidance, take a look at these two videos.
I also recommend the YouTube channel ThePrimeagen and its six-part video series:
Watch the entire playlist, not just the one embedded video. After that, there’s no getting around investing the time and practising using Vim. Have the confidence. You now know enough to stand on your own feet. Edit some test files in Vim. If your usual code editor offers Vim emulation, make use of it. No amount of watching videos can save you from getting your hands dirty yourself.
After some days of getting comfortable with Vim, or whenever you feel ready, check out these fantastic talks by Leeren Chang.
There’s obviously a lot more to learn about Vim, but hopefully this blog post could interest you in learning Vim and take away some fears.
A great help in learning Vim was to also learn proper touch typing. It may seem harder to simultaneously learn two things rather than getting one thing down first and then moving on to the next thing, but in fact learning Vim is so much easier when you don’t have to constantly ask yourself, “Where’s the
c again?” or “Where’s the
Let me know whether you liked this introduction. Be well.
"tells Vim we want to do something relating to the clipboard. There are multiple clipboards in Vim (in order to store multiple texts). We need to specify which clipboard we want to use. The
*character represents the operating system’s clipboard. Once we specified which clipboard to use, we can press the
pkey to paste the text. ↩︎