Tuesday, February 7, 2012

Using key mapping in vi

vi or vim provide a very useful technique whereby user can assign arbitrary functions to control key strokes. For eg, here is one such mapping:

map ^k I/*A*/

When a hits ctrl+k, the line on which the cursor currently is, is commented. To explain the command:
map ^k - assigns the functionality to ctrl+k character
I/*A*/ - concatenation of smaller operations which are
- Move to Esc mode,
I - Insert in the beginning of the line
/* - characters to be inserted
- Move to Esc mode
A - Append at the end of the line
*/ - characters to be appended
- move to Escape mode

The Esc key can be typed by first typing Ctrl+V followed by Esc key.

By adding this mapping to .vimrc file, this mapping can be saved across vim invocations.

No comments: