Learning Vim in a Week

Note: I turned this blog post into a talk for Boston Vim, check it out here: Learning Vim in a Week - Boston Vim.


I’d been using Sublime for a long time and recently switched over to Vim. It look me about a full week to learn enough to use it daily for work. Here is what I did to do it!

###Before you start, you should know…

  • All that stuff Sublime or TextMate can do. So can Vim. Plus more.
  • You’ll be able to keep your entire workflow in a single terminal window, without taking your hands off the keyboard.
  • .vimrc. This is the file you modify to customize Vim, you’ll become very familiar with it.
  • Don’t start learning at work, it’s frustrating at first and you won’t be able to get much done. Weekends are good.

Getting started…

These are roughly the steps I followed to learn Vim.

  • Grab a coffee, open a terminal, and run the command vimtutor. Go through it a couple times (I did it sporadically over a couple days).
  • There’s also a gamified version of vimtutor, Vim Adventures.
  • Steal someone else’s .vimrc and .vimrc.bundles. These files let you add plugins and customize Vim. Thoughtbot’s is good. Here’s mine. You’ll eventually have your own, but it helps to start with someone elses.
  • Remap your Caps Lock to ESC. You will use ESC in Vim constantly. Having it closer to your home row helps.
  • Speed up your key repeat rate. This is how quickly a key press repeats when you hold down a key. It helps you scroll through code much faster. On OSX you can do this KeyRemap4MacBook.
  • Watch screencasts. Thoughtbot has a ton of great ones if you subscribe to prime. The one I found most helpful was “Vim for Rails Developers.”

Switching Files and Searching:

The biggest barrier for me using Vim full time was quickly navigating a project. There are bundles you can use to make this really easy.

CTRLP

This does the same thing as Sublime’s Ctrl P. Fuzzy search by file name. Must have.

vim ctrl p

NERDTree

Gives you a sidebar that you can quickly navigate files with. I’ve mapped NERDTree to <F10> so I can quickly open and close it. I’ve also mapped <F9> to bring me to my currently open file in NERDTree (super useful).

" Toggle nerdtree with F10
map <F10> :NERDTreeToggle<CR>

" Current file in nerdtree
map <F9> :NERDTreeFind<CR>
nerdtree

Super fast search. Also speeds up indexing when using CtrlP.

ag

Copy & Paste:

I struggled for a while with copying and pasting code from outside Vim. It’s just a little different than other text editors and this makes the transition a little rough.

I recommend reading this article on copy/paste.

Also, if you have Vim’s auto indent and auto commenting turned on (which you probably do and want). When you paste, code may automatically be reformatted or commented out. This is annoying.

Vim has a “paste mode” that you can toggle on and off. I mapped it to <F2> so that when I do need to paste from outside of Vim, I can tap a key to do it.

"key to insert mode with paste using F2 key
map <F2> :set paste<CR>i
" Leave paste mode on exit
au InsertLeave * set nopaste

Finally, Commit to it

Once I had the basics down, I hid Sublime from my dock and started using Vim for real work. Anytime I found I was slow with something, I’d look up a fast way to do it, learn it and continue on.

One thing I’ve learned is, there is always a faster way to do something in Vim.

More reading