Starting With Vim

March 25, 2019

Iā€™m not gonna lie. Starting Vim is hard.

It is partly because it is so much different from any other popular editor, and partly because itā€™s not very useful out of the box (though when configured properly it can easily replace ~put you favorite IDE here~).

So the newbies have to deal with lotā€™s of new concepts at the same time: how to use modes, how to navigate in the code, what configurations options should be used, and finally, and most importantly, how on earth I quit this thing?!

I remember all those problems very clearly, so here Iā€™d like to share some piece of advice that can soften this harsh period a little bit for the Vim beginners. Because, in the end, it is all worth it.

Start with some basic configuration

The problem of Vim is that itā€™s not very useful out of the box. You need to work on the configuration a little bit in order for it to become useful. Some people find pleasure in this process of always polishing their.vimrc (I certainly do), some donā€™t. But beginners sometimes are disappointed by how little Vim is offering out of the box. No syntax highlight, no code auto-completion, code formatting, etc.

There are several starter "packs" with all the blows and whistles that you can use right away. Personally though I donā€™t recommend this approach because of the downside ā€“ you never fully understand whatā€™s in there and how to change it.

Understanding and growing your .vimrc is a crucial part of understanding Vim.

So instead of starting with full-featured packs, Iā€™d recommend starting with some small but sane vimrc file. Small enough that you can read through it and understand what it does. Then start your journey on growing and modifying it to your own needs.

In order to learn what are the unlimited possibilities I highly recommend browsing through the other peopleā€™s configuration files. For example, you can browse the dotfiles (there are lotā€™s of tasties there, not only vimrc files), or you can check out mine.

Learn to love modes

When you first open Vim, you canā€™t type. If you try pressing the keys, something weird would happen. For example, you can accidentally move cursor to the next line, or most likely open some weird mode without any way to escape.

This is because at first you open a file in a NORMAL mode. This is the mode in which keys are used to execute commands, for example move to the next paragraph, format the file or delete the text till the and of a line. If you want to type you need the INSERT mode. There are multiple ways to get into it, but the most common is to just press i (insert) in the normal mode.

There are several other modes as well but the point here is that modes are there to make developerā€™s life easier, not to complicate things. As long as you get used to it of cause.

Donā€™t use arrows

Vim is very effective in terms of navigation. You can do anything without a mouse. But in order to get there you should limit yourself in some of the old habits and embrace the Vimā€™s way.

For example, in order to get to the beginning of the line, you can type \$, navigate to the beginning of the next word with w (mnemonic word). go to the end of file with G.

There are many other shortcuts, which you can find on any tutorial site. But the point is you never really start using those and enjoy the high speed of your typing and navigation until you ban yourself from using arrows.

Thankfully, you can literals do just that with the following configurations:

noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>

This snippet literally disables your arrows, forcing you to use hjkl instead. Those guys are much easier to use when you get used to it, because your right hand is always there.

Embrace the motions

The keys I use the most are not hjkl. Moving between code character by character is not very effective. The keys I used the most is w (word) and b which moves the cursor to the next/previous word. gg will put my cursor to the beginning of the file, G ā€“ to the end.

You can use f<char> or t<char> to move to the next char. For example, f; will put the cursor on the next ;, and t; will put just one character before it.

You can use ) to get to the next sentence and } to move the paragraph. And there is a whole bunch of motions starting with ] or [. ]s will move to the next misspelled word, ]z moves to the next open fold, and there are many more (plugins can even add up to those)

Also all those you can prepend with a number. Say 5j will move you 5 lines down, and 10) will skip 10 sentences.

There are many and many more text motions. They let you be highly effective while moving through the code.

Combine Motions And Actions

In normal mode you can use some actions to modify text without going into the insert mode.

For example, you can type x to delete a character under the cursor. You can prepend it with number: 5x will delete 5 character. And finally, you can combine those actions with motions:

  • diw delete the word under courser (mnemonic: delete inĀ word)
  • ytw yank (or copy) till "w" (copy into buffer text from the cursor till the next w character)
  • dG delete the text till the end of the file

You get the idea. You can combine actions with motions, which opens up the infinite opportunities.

Where To Go From Here

Vim For Developers

Vim For Developers

ā„ļø 35% off this Christmas season! ā„ļø

Learn Vim and upgrade your productivity to the next level by building the IDE of your dreams.

LEARN MORE