A step by step approach to vim


Aug, 2020

A step by step approach to vim

Learning vim is not hard. But like any other learning process, to reach proficiency you need to master the fundamentals first. For vim that means about a couple of weeks of your time. Get through those two rough weeks and you can quickly get to a higher level of productivity.

Here's what worked for me:

Step 0: Improve your typing skills

I never questioned my typing skills until I came across TypingClub.

You'll go through a series of typing exercises and short videos that teach the techniques proven most effective. This helped me get more comfortable with the keyboard, improve my work posture and ultimately increase my productivity. Even if you don't stick with vim, this will definitely improve your work as a programmer.

This is not a step to tick the box and move on though. I still go back to TypingClub whenever I feel I'm a bit rusty... Or just for the fun of it. Vim work is keyboard exclusive, so the more comfortable you are typing, the faster you'll master vim's philosophy.

Step 1: Vimtutor

Perhaps the most valuable 30min of vim's learning path. It will teach you how to move around a file, edit it, quit it, create new files, etc.

  • Go to your terminal and run vimtutor
  • Follow the tutorial and use the practical exercises

Step 2: Disable your arrow keys in normal mode

Vim uses hjkl keys as the arrow keys, meaning h moves the cursor left, j moves the cursor down, k moves the cursor up and l moves the cursor right. The reason for it goes back to when Bill Joy was creating the vi text editor (in 1976) with an ADM-3A terminal that had the arrows located on the hjkl keys.

In practical terms, it does make sense to keep your hands in a home position instead of having them move all over the place. That's probably not where your muscle memory is so you'll have to practice it until you get used to it.

You will inevitably try to use the arrow keys - that's normal - but you shouldn't. Good news though: you can disable them in vim. This will force you to stay in your home keys and use hjkl. I'll explain later on how to do this.

Make it a fun learning process. There are games out there that help you gain that muscle memory. I played around with vim adventures. It will teach you this and other vim motions.

Step 3: Practice motions and commands

Now that you learned hjkl, avoid using them too much. You have motions that will help you jump, select, change and delete entire sets of words, lines, or other patterns. The power of vim is exactly in the editing of existing code with just a few keystrokes.

Vim language is very semantic so it's not like you need to memorize all the commands. Once you know the main ones well, you'll get thousands of useful variations.

Have a look at the list below:

w - move the cursor forward one word
2w - move the cursor forward two words
b - move the cursor back one word
2b - move the cursor back two words
ci( - change inside the parenthesis (can be used for other surrounding characters)
di( - delete inside parenthesis
cw - change word
dw - delete word
d2w - delete two words
dt; - delete till ;
f, - find the closest comma
ctrl + d - move down
ctrl + u - move up

Get an online cheat sheet or, better, just build your own. When you learn a new command, update your cheat sheet and practice those for a while.

Whenever I feel that something could be done more efficiently I search for it. Let's say I am somewhere at the top of the file and I want to go to the last line of the file. I don't want to press j all the way down hundreds of code lines. I could use ctrl + d that's faster… But I found that just pressing capital G goes directly to the last line. Press gg again and I am back up. Press 10g and I am on line 10. The same is valid for plugins or bindings that you can create for yourself.

Watch other people online. I found the upcase videos extremely helpful and I keep getting back to them whenever I feel my progress is stale.

Step 4: Once you go vim, you never go back

When things get rough we all have the tendency to back to our comfort zone and in this case, that means getting back to your good old IDE. DON'T. You will never gain the muscle memory you need if you keep switching editors. Plus, it will get messy, you'll start mixing vim key bindings with your IDE's bindings. Just don't.

Have patience and believe in the process. You will inevitably lose a bit of productivity in the beginning but you will definitely get back that investment in the near future.

Unpopular opinion: I started learning vim at work. Some people will tell you not to but I believe that an efficient learning process implies practicing vim using your real-life code and making use of your working hours. Make sure that your team is aware that you are learning something new and that it may take a bit of your time.

Step 5: Find a tutor

It helps a lot to work with a colleague that is experienced in vim, especially if this person is sitting next to you.

If you know a vim user at work, pair with him/her - you will learn A LOT. If you see a command that you did not follow ask what were the keys used. Then switch: be the driver and ask your tutor for recommendations while you are writing code.

Step 6: Edit your config files as you go

Though it might not look like it in the beginning, vim can do whatever your old editor did and more. But for that, you will have to add configurations. This is where the .vimrc file comes in.

In .vimrc you can do things like creating your own commands, disabling keys, adding themes, installing Plugins.

Beware of the Plugins though. Only add them as you go and as you fit necessary, make sure you know exactly what they are for.

You are now ready to disable your arrow keys. Open your .vimrc file and type:

" block arrow keys in normal mode
noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>

Don't forget to add your .vimrc to your git manager. This will come in handy if you work with different machines or when you change jobs. Whenever I edit my config file at work, I push it to a dotfiles repository on GitHub and when I get home I just need to pull that file to my home machine and that's it. Same environments, no need to set up everything from scratch.

You can have a look at my dotfiles here.