How to map key sequence to command in Vim

Vim and the thousands of plugins written for it come with commands that can be used to enable, disable, toggle or invoke an action. Typically, these commands are textual and verbose to type in. For example, the command to toggle the Gundo window is :GundoToggle

Vim is highly flexible, in that it gives you the freedom to map these commands to any key sequence you prefer. For example, I decide to I want to toggle Gundo by typing the Leader key, followed by gu. The leader key is mapped to the backslash () key.

To map this key sequence to the Gundo toggle command, add this line to your vimrc:

nnoremap <Leader>gu :GundoToggle<CR>

Note how the Vim mode for the mapping is specified (normal mode), how the Leader key is specified and how the carriage return has to be explicitly specified.

Any command can be mapped in this way to any key sequence you desire in Vim.

Tried with: Vim 7.3

Advertisements

One thought on “How to map key sequence to command in Vim

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s