viernes, 11 de marzo de 2011

My Vim configuration. Part 3 - Vim shortcuts

Continuing with my explanation of my Vim configuration (you can have a look at my view settings and my plugin settings), today is the day of the shortcuts :)

Everyone of us love the shortcuts our favorite IDE has. Vim is no less and you can define whatever shortcut you need :)

How to define a shortcut in Vim
It is very easy to define a shortcut in Vim, you only need to map the key combination you one to use with the actual command you want to execute.
To map a combination you can use map or noremap (and all the variants for each mode, like imap, inoremap, nmap, nnoremap, etc...). The difference between map and noremap is that the second one is not recursive. You have a very good explanation here.
You can see an example of a shortcut definition:

This shortcut closes Vim without saving the file and can be executed by pressing the keys 'c', 'l', 'o', 's' and 'e'

Leader
The previous example is more or less a "hard code" shortcut :) A better way to map commands is using the leader variable, so you start your shortcut by pressing your leader combination.
To define your leader combination you only need to do something like this:

In this case, my leader combination is just the ',' key :)

It is important to define your leader combination because a lot of the existing plugins for Vim use it for their own shortcuts (mappings).

My shortcuts
Ok, now that we have defined the leader, let's have a look to my actual shortcuts.
The first one I want to show you is how to change between the actual buffer and the alternative buffer (If you don't know anything about Vim buffers, take a look at this video, it is great!)

What I'm doing here is mapping ',' and '6' to ':b#' (:b# is what you have to type to switch buffers when in normal mode. means that, after the command, I want to press 'return', executing the command). I'm using nnoremap for this shortcut because I want to use it only in normal mode (the first n of nnoremap) and because if some of my plugins map any ':', 'b' or '#' key to any command I don't want to execute that shortcut (not recursive), I just want to execute ':b#'.

The next shortcuts are related to some plugins I use:

I just map the NERD tree to ',d' and a little bit of help every time I want to use ack (only in normal mode) :P

Then, I have two shortcut for removing the extra spaces (at the end of the line) and to change tabs with 2 spaces :)

I also have a shortcut that changes the ruby hashes (with a symbol as key) from 1.8 syntax to 1.9 syntax:

And last, but not least, I map the tab as a match bracket finder:

I probably change all of this shortcuts in the future, when I get more experience with Vim, but I hope it shows you how powerful (and customizable) Vim is :)

PS: I know that the gist are not visible on google reader. Sorry about that! I want to move my blog and fix that :)

No hay comentarios:

Publicar un comentario