Starting With Vim
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
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
[. ]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:
diwdelete the word under courser (mnemonic: delete in word)
ytwyank (or copy) till "w" (copy into buffer text from the cursor till the next w character)
dGdelete 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
The best way to learn about something in Vim is to use the help, e.g.
:help ]to learn more about motions starting with ]. Help in Vim is properly structured and highly detailed.
Learn from others by reading their dotfiles.
Learn more about the Vim motions language
On this very website there are other articles about Vim.