How to get started with Vim

Gautham Dinesh
7 min readAug 8, 2020

A beginner’s guide to Vim (and why you should use it)

Over the years, growing as a programmer and learning new skills, I have continuously experimented with different text editors and IDEs. From the very annoying ones like Eclipse to simple yet handy ones like Sublime Text, and the recent favorite VSCode.

Yet with all their wonderful functions, extensions, and plugins I did not have an easy way of interfacing with them. I would have to take my hands off my keyboard and use my mouse to click a few things here and there, waste time scrolling through chunks of code, and perform repetitive moves in the codebase which was time-consuming.

Enter vim

My first encounter with vim was about 8 months ago when I was following a Youtube tutorial and a file was opened from the terminal using “vim” instead of the “nano” command that I had been used to. I followed along but when the person in the tutorial left the file back to the terminal, I was stuck. I had no idea how he saved the file and quit. If you want to see someone suffer, open up a file in vim and ask the person to quit.

Since that encounter, I had abandoned the idea of doing anything in vim, but fast forward to about 2 months ago and one of my friends starts preaching about the power of vim and how it is the best thing he has used. So I decided to do some research and WOW.

Initial Phase

When I first starting playing around with vim, it’s best described as learning grammar for the first time. You learn what each word means, how it related to a subject, the way it’s modified by a verb, and how you can compose phrases and sentences with them. But with just about an hour of pressing a bunch of keys, you start to understand how intuitive and powerful the tool you have at your disposal is. This is why even after 30 years since its inception many developers are using it and recommend it. And it all works in your terminal.

Baby Steps (Motions)

The first time you use vim, you would probably start typing hoping to see text appear but all you will hear are invalid keypress sounds. To enter text in vim, you need to use the insert command:

i : insert

Vim consists of commands that allow the user to manipulate the content of the file. When you press i it will drop you into insert mode which will allow you to start typing. You should see — INSERT — appear at the bottom of the window. To exit from insert mode you would use the Esc key.

All your navigation in vim can be done from the home row which reduces the need for you to move your fingers away and use the mouse or arrow keys. You might think that “but that’s only about 3 seconds of my time.” And you are right but when you are in the middle of writing a function or you are in the “flow” that 3 seconds of moving away and coming back can break your focus and the speed with which you are working.

To navigate in vim, you use:

h: left
j: bottom
k: top
l: right

This may seem very unusual at first but trust me once you get used to it, you will not miss the arrow keys or using the mouse to click around. The left and right are easy to remember since they are at their correct positions and for me, the hardest to figure out were the j and k but try to think of the j as the bottom arrow key since it has a curve at the bottom. But as you practice more it will become intuitive.

GIF for showing vim navigations using h,j,k,l
Navigations using h,j,k,l

If you have a file or a paragraph it would be dreadful to use just these 4 letters to navigate but Vim has some neat tricks for faster navigation:

b: moves the cursor to the beginning of the previous word
w: moves the cursor to the beginning of the next word
e: moves the cursor to the end of the next word
$: moves the cursor to the end of the line
0: moves cursor to the beginning of the line
{,}: move between white spaces

But there is even more to the magic of vim. If you enter a number before carrying out any command, then the following command will be executed the specified number of times.

For example, if I wanted to move up 10 lines, instead of pressing and holding j, I could type 10j and this would move the cursor down 10 lines. This ability to enter a number before a command can be used with any combination of letters to multiply the command.

GIF showing more navigations with vim
More navigations with vim

In building up our grammar, we can consider the motion commands to be the nouns. Following this are verbs, that allow us to perform an action on the noun.

Let’s get walking (Verbs)

Vim makes it very easy to perform actions on the noun that you select and the ones that I use most often are:

d: delete
c: change
y: yank
v: visual

As you can see already, the letters correspond very closely with the intended function. d can be used to delete. For example, dw will delete a word and c3w will allow you to change 3 words.

Yank simply means copy and a command like yy will allow you to copy an entire line into your vim clipboard and with p you can paste the selection.

v simply puts you in a visual mode where the text is visually highlighted by character and V allows you to select by line.

GIF showing Vim verbs
Vim verbs

We need to build up some speed (Modifiers)

Let’s go back to the first command we saw: i, this means insert but when preceded by a verb it means in. This allows us to choose what to modify, for example, the command ciw will allow you to change the content of a word similar to what cw would do but with an exception that it would apply to a word in any context.

To understand what this means, I have to introduce the dot operator: . This operator allows you to repeat previously entered commands. Let’s say I wanted to change 2 words in different positions to the same word, I could move my cursor to one of the words, type ciw and type the new word, say hello and then move my cursor to the other word and press . This would repeat the previous motion in the same way regardless of the context of the word.

GIF showing VIM modifiers
VIM modifiers

Some other modifiers you can use are:

t: to a letter and places cursor before it
f: find a letter and places cursor on it
a: around
/: find a string

Let’s get running

Now we can put all of this together to create some very nifty motions:

cit: change inside tags e.g html <> tags
yyp: duplicate a line
dd: deletes a line but also copies it to clipboard
ct{: changes from cursor to {
d$: deletes to the end of the line
GIF showing motions in vim
Motions

Partings words

It wouldn’t be fair if I ended without telling you how to exit the file. When you want to enter vim specific commands we use :. You should see this appear at the bottom of the screen.

:w : save the file
:wq : save and quit the file
:q! : quit without saving changes

What I mention in this post is just scratching the surface of vim and there is so much more to learn. Almost everything in vim is customizable and there are many plugins that make using vim much more fun and fast. In fact, I wrote most of this post in Vim.

I plan on doing a part two where I cover more advanced commands, remapping keys, and macros. Please let me know in the comments if you want to see anything specific.

If you want to explore more, these are some of the resources that I used when I was starting.

By entering the command vimtutor on the terminal, it will open up a tutorial that teaches you most of the basic concepts of vim.

Thanks for reading, please share if you liked this post and found it helpful.

--

--