Saturday, December 3, 2011

Some useful vim key mappings

First the code with some descriptive comments:
" fast movement
" I don't have to move the fingers
" from the regular (one line) movement position
nnoremap <C-j> <C-d>
nnoremap <C-k> <C-u>

" move to beginning/end of the line
" easy to reach and I don't have to press shift
nnoremap 0 ^
nnoremap - $

" moving through tabs
" gr is easier to type than gT
nnoremap gr gT 

" get rid of the messages and highlighting
nnoremap   :silent nohlsearch:echo
And now, if you are not convinced yet, I have the full motivation behind these key mappings.
If you move one line up and down with k and j it seems natural to me to move faster with Ctrl+k and Ctrl+j, because you don't have to move the right hand fingers, so I have the following two mappings.
nnoremap <C-j> <C-d>
nnoremap <C-k> <C-u>
It's nice to move to the beginning or the end of the line with the corresponding regular expressions symbols ^ and $, but these symbols are difficult to type because they are in the center of the number row and you also need to press the shift key. So I choose 0 and - because 0 was already used to go to the first column of the line, but this is not so useful when writing code (because usually I want to go to the first non blank character of the line), so it could be replaced. And - because of the spatial relation: if 0 is used to go to the beginning of the line then the end (which is to the right) can be reached with the key to the right, - (this is used to go up by default, but I din't used it once).
nnoremap 0 ^
nnoremap - $
For moving through the tabs I use gt (go to next tab), it's easy to type. But gT (go to previous tab) it's not so easy. So I use the same spatial connection between the direction on the screen and keys position on the keyboard: if I go to the right with gr then I should go left with gr (r is to the left of t on the keyboard).
nnoremap gr gT 

No comments:

Post a Comment