Thursday, 3 May 2012

Common VIM Mappings to make life easier

Swami Nikhilaananda said:
"When you transition from a known to an unknown, the similarities allow the transition to become smoother"

If you have not checked out the previous post about Vi(m) editor, now is the time to read it.

Many times, we are used to common keyboard short-cuts like Ctrl + C to copy, Ctrl + v to paste, and so on. However, when we move to Vim, it is tedious to remember all the new shortcuts like y to yank (copy) and p to put (paste).

To avoid that, I created a bunch of remappings to enable me to perform common tasks quickly using the same set of keyboard shortcuts that we are so used to.

You can do it too. Just open your ~/.vimrc file. If it is not there, create it. Then, paste the contents below as is into the file. Save it and restart vim. These shortcuts will start working (in escape mode)

set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

set mouse=a
se nu

:map <C-a> GVgg
:map <C-n> :enew
:map <C-o> :e . <Enter>
:map <C-s> :w <Enter>
:map <C-c> y
:map <C-v> p
:map <C-x> d
:map <C-z> u
:map <C-y> R
:map <C-t> :tabnew <Enter>
:map <C-i> >>
:map <C-w> :close <Enter>
:map <C-f> /
:map <F3> n
:map <C-h> :%s/
:map <C-q> :q <Enter>
:map <S-t> vat
:map <S-T> vit
:map <S-{> vi{
:map <S-(> vi(
:map <S-[> vi[

Here, C stands for Control, says Ctrl + a, GVgg is the Vim equivalent of select all. Thus, we are just mapping them. S stands for Shift. will select all text within the current tag inclusive of tag. exclusive of tag. S-{, S-(, S-[ will select text within the corresponding brace.

Hope you find these shortcuts useful. Let me know if you want any thing that's missing here.

You may also like to read
Comment and Uncomment Multiple Lines in Vim
Vim Editor - Some cool features (Relatively Advanced)

1 comment:

Unknown said...

One more thing that usually isn't on by default is highlighting.

:set hls

You can also cause your searches to be case insensitive

:set ignorecase

Suppose you are searching for something and you don't want the search to wrap back to the beginning of the file,

:set nowrapscan

Suppose you created a new file type, and wanted vim to give it the colour treatment of an laready existing type like a .cpp or .v,

au BufRead,BufNewFile *. set filetype=c

au BufRead,BufNewFile *.sv,*.svh set filetype=verilog

Causes all files with the .sv or .svh extension to be given the colour treatment of verilog files.

You can also set the colour scheme you want using:

:color <> in escape mode.

<> could be torte, morning, ron etc..