Showing posts from 2014

Quick commenting multiple lines in VIM with a map

A nice, and reasonably easy feature to get a handle on in Vim is key mapping. You can map a key combination to another key combination. In order to comment a line with // at the beginning of the line we would type the following commands in vim:

  0  - move to beginning of the line
  i   - insert mode
  //  - the comment syntax
  <esc>- back to normal mode
  j - move down to the next line

so we can map that series of comands in to an easy to remember key combination like this:

:nmap cc 0i//<esc>j

This works great for one line, but what about where you want to comment out multiple lines you ask? It's a pretty reasonable request!

Well, if we simply right 2cc, the 2 will be appended to the 0, which will be equivalent to typing 20i//j, which is not what we want. Try it!

The solution is to use a concept called register, which will define the command as a block, prevent the appending we say previously. It will look like the following

:nmap cc @='0i//j<C-V><esc>…