Saturday, March 30, 2013

Configuring .vimrc and .gvimrc for Mapping Keys

As we know that computers do what we ask to do it and that is called programming. Or I can say programming is like giving instruction to computers. Mapping keys in gvim/vim is same. As we know that command for selecting all text in file is :ggVG  right?
What if I am not able to remember this???  Somebody will say that you have to remember it there is not any other option. But gvim/vim says "I have solution of this problem."
Gvim/Vim has facility of mapping commands, definition of mapping I can say that telling computer (gvim/vim in our case)  now onward you should perform this operation irrespective of whatever you were doing previously.

Get started with mapping 

Let's take an example suppose you want to map command :ggVG for selection of whole text in file to sel. Simply write following command in your .gvimrc/.vimrc file
                                    map sel ggVG
or for temporary use write command :map sel ggVG and now for experiment press "sel" in normal mode. Your whole text will get selected.

Now let's map some keys with commands. You can use <keyname> to tell gvim/vim mapping about special character. The following command maps space key for selecting word on which currently our cursor is.
                                    map <space> viw
after this put your cursor on any word and type space bar and gvim/vim automatically selects the word. One another example is map <c-d> dd maps cntr + d key with command dd. So after writing this command every time pressing Control + d deletes current line in file.
The following map will give you command for shifting your line in your text.
                                    map - ddp
after typing this your current line will shift one line below by typing just - in normal mode. Same way you can create such mappings as per your requirements.

you can use this command for creating abbreviation as gvim/vim do exactly we say it to do. As in first example we are replacing  ggVG with sel. But if mapping contains latter "i" gvimvim shift to insert mode. So if you want to create abbreviation it can be done as follows.
                            map fun ifunction
In this case first latter i takes gvim/vim into insert mode and then write function. So after this if you are pressing fun in normal mod it will give you function fully written. Also gvim/vim has facility to abbreviation.

you can ma function keys as well example map <F2> command.             

Modal Mapping

You can be more specific when you want to apply your mapped keyword by using nmap , vmap and imap for mapping in normal mode, visual mode and insert mode respectively.

The reason for these different commands is that the behavior of one mapped keyword will be differnt according to different modes selected.
you can disable any previously defined mapping by the following command.
                 :nummap mappedkeywore
suppose you want to remove mapping on key Control + d simply write :nummap <c-d>

 View Currently maped keys

 you can see which key have been mapped to which command. Gvim/vim shows you complete list of this type of key. Just type the following command 
                     :verbose map 
this command gives you complete list of files that have been mapped for current file.


 PREV PAGE                                               INDEX                                                      NEXT PAGE

NOTE: The above commands should be written to your .gvimrc or .vimrc file so that you can use it whenever you open gvim/vim. This will improve your code editing speed.

1 comment: