Although I’ve always enjoyed using Sublime Text as my text editor, I decided to give using Vim a shot a few weeks ago. I wanted to see why some developers prefer it and wanted to experiment with spending more time in iTerm and less time switching between applications. Learning tmux at the same time, I’ve enjoyed getting started with Vim and thought I’d share some tips that might help someone interested in giving Vim a try.
Tips for learning Vim
Go through vimtutor
The easiest way to get started with vim is to use vimtutor. You can learn the
basics in a half an hour or so, and getting started is as easy as typing
in your shell. Use vimtutor before customizing your .vimrc so that you
understand the default configurations, which you’ll need to know if you ever use
someone else’s machine or are editing remote files. Having a solid understanding
of Vim operators, motions, and text objects will give you a good foundation for
using Vim effectively.
Use Google and Stack Overflow
Not sure how to do something in Vim? Google it or look it up on Stack Overflow. The Vim Tips wiki is also a good resource. If you find yourself thinking that there must be a way to do something in Vim, there probably is. I’ve added a few useful configurations to my own .vimrc from tips I’ve found online.
Use macros and custom key bindings
When writing code, most developers try to keep their code DRY (Don’t Repeat Yourself). The same idea should apply to how you manipulate code in your editor. If you have to perform the same set of actions to a block of code 10 times long, try using a macro instead of repeating the same longer set of keystrokes 10 times.
Start recording a macro by typing
q<any letter>. Vim saves your macro
to a register identified by a letter as you type in commands. To finish your
macro just hit
q. Then use your macro by typing
@<letter of the register you used>.
Of course, Vim is even more extensible when you edit your settings, remap keys, and create your own commands. Try editing your .vimrc and take a look at the dotfiles and Vim bundles other developers have shared online.
A fun way to reinforce some Vim basics is to install Vimium, a nifty Chrome extension. Vimium lets you use Vim key bindings across the web, making it even easier to leave your hands on the keyboard. It’s disabled by default on some sites that have their own rich set of keyboard shortcuts, and it’s highly configurable so that you can use a combination of a site’s own shortcuts and the ones Vimium adds.
If you have some tips for learning Vim, please share!