-.- --. .-. --..

Getting Started With Using Vim

19 Aug 2013

This is an introduction on some settings that made it really easy for me to use Vim on a daily basis. This is not an introductory post and assumes that you’ve tried to use Vim (and probably failed).

I’ve been using Vim for about two and a half years now and I use it for my daily coding and reading needs. However, I was not really fluent in using it for more than two years. I felt…clumsy using it. However, I forced myself to use it since I was fed up with IDEs at that point, which I used when I was coding PHP and I didn’t want to use one for Ruby. Fresh start, et al.

The best way to start using Vim today is to remap some keys (and disable some). The basic structure of a (re-)mapping is map {key} {macro}. The macro can be either a set of commands or another key.

I often see many Vim users use arrow keys for navigation in the “Normal” mode. This is wrong. However, habit is a bad thing and it is hard to give up muscle memory and retrain your brain. This setting will make it really easy and help you un-learn this. Drop these lines in your ~/.vimrc:

nnoremap <Left> :echo "won't happen! Use the h key"<cr>
nnoremap <Right> :echo "won't happen! Use the l key"<cr>
nnoremap <Up> :echo "won't happen! Use the k key"<cr>
nnoremap <Down> :echo "won't happen! Use the j key"<cr>

That’s not it. The muscle memory haunts again when you’re in insert mode. These will teach you better:

inoremap <Left> <nop>
inoremap <Right> <nop>
inoremap <Up> <nop>
inoremap <Down> <nop>

Wait! We didn’t use the echo thing like before. Why? try it and see what happens.

nnoremap is short for “Normal mode, no recursive map” and inoremap is for – Yep – “Insert mode, no recursive map”. Change <nop> in the insert directives above to :echo "won't happen!, restart Vim and in your insert mode, hit an arrow key. Boom!

The Escape

Trying to hit Esc is a pain. Vim comes with a default alternative mapping for the key: Ctrl+[. Oh! btw, don’t go <nop>-ing the Esc key. There is a better solution:

nnoremap <C-e> <C-[>
inoremap <C-e> <C-[>
vnoremap <C-e> <C-[>

I remapped Ctrl+e to act as the escape key. Easy to type and gets you off the Esc key habit pretty quick.

The Leader

The leader key in Vim is a sort of a meta-key. Like Ctrl on a normal text- editor that let is run commands/shortcuts. On a default vim installation, the leader key is mapped to backslash \. However, the popular choice for this key is the comma ,. Some basic mappings to get you started:

let mapleader=","

nnoremap <leader>W :%s/\s\+$//<cr>

In the above command, we’ve assigned a small macro that let’s you strip whitespace in your file. Whenever you hit ,W in normal mode, it will run the command :%s/\s\+$//<cr>

Detour: Search and replace

In Vim, search and replace can be done in Normal mode by typing:

:s/regexp to be used to find text/replacement string/
:%s/regexp to be used to find text/replacement string/

The second form replaces all the found instance whereas the first instance will replace only once.

These three settings should make your Vim 90% more usable.

Okay, 60% more usable.

Fine. 50% more usable.