7. Mapping Keys

Mapping keys is one of the most useful and productive operations that vim performs. Mapping keys is just a method to map a complex command to a particular keystroke or sequence of keystrokes. If you find yourself routinely performing the same commands to edit a document there is a good possibility you could create a map to make your editing easier.

For instance, if you write html, xml, or sgml documents in a text editor you know it can become quite tedious typing out all those tags all the time. Markup language tags may also include attributes which you also have to type in or cut and paste from other locations. This can slow your work down quite a bit. There are gui programs which sort of solve this but many of them can't always give you the control you need and you end up resorting to hand editing the file anyway. The simple solution in vim is to create a map.

There are several different types of maps you can create but the two we will concentrate on are Normal mode and Insert mode maps. Normal mode maps begin with the command map and look something like this:

map keyboard_key command_to_execute

Insert mode maps begin with the command map! and look something like this:

map! keyboard_key command_to_execute

Caution

Avoid re-mapping the <F1> key as this is always mapped to vim's help.

The complexity of the map is pretty much limited to your imagination. Here is a simple map you might use to open a letter:

map <F2> iTo whom it may concern, <CR><CR><TAB>

This is a Normal mode map set to use the <F2> key. It won't work when you're in insert mode. When you press the <F2> key, vim executes the insert command (because it's still in Normal mode), it then types all the letters as it sees them (because now it's in Insert mode) until it hits <CR>. This is how you type a Carriage Return in maps. So vim executes a carriage return. It then executes the next carriage return and then indents that line with one tab. At this point the map is finished. If you would like to test this map go ahead and copy the map into vim by typing : and then pasting the map into the status bar. Press Return. Now try pressing the <F2> key and see what it does. One thing to notice is the map exits while in insert mode. This allows you to start typing immediately.

Tip

If you forgot what maps you have or just want to see what maps you have defined you can simply type: :map or :map! and hit enter. The defined maps will show up in the "status bar."

Tip

To make maps permanent add them to your .vimrc file.

Earlier I talked about how maps can make working with markup languages easier. Here are several example Insert mode maps which can be used to create headings in html:

map! ,h1 <H1></H1><ESC>2ba
map! ,h2 <H2></H2><ESC>2ba
map! ,h3 <H3></H3><ESC>2ba

We will focus on the first map. This map is executed while you are typing in insert mode. After you type ,h1 vim types <H1></H1> for you. It then sees the escape command which just like any other time you're in insert mode takes you back to Normal mode. Vim then sees the command to move back 2 words (2b) which puts the cursor on the ">" of the first <H1> tag. Vim then sees the a command so it moves one space to the right and enters insert mode. At this point the map is finished, your tags have been written, you are in insert mode and ready to type the heading. The beauty of maps like this is you never leave insert mode (at least from your perspective) and you type only 3 characters and get many more. Once you get used to your personal maps it's easy to forget about all the tags and concentrate more on your content.

If you would like to try these maps out simply copy and paste them into vim like the previous example.

There are several things you should notice about the previous examples. Insert mode maps are generally used to enter portions of text which are repetitive or time consuming. They work extremely well for inserting markup text and then positioning your cursor where you would logically type next. They take care of "closing" tags for you which can cut down on nesting errors and ill formed documents.

When creating maps, selection of your assigned keystrokes is pretty much up to you. I would recommend you avoid re-mapping standard commands as this makes it difficult for you to transition to someone else's machine unless you carry around a copy of your .vimrc on a floppy (which I've often thought about :) ). When you choose a sequence of commands for Insert mode choose a sequence which is unlikely to be typed. In the previous example I chose the "," because in almost every type of document it is always followed by a space. The reason for this is vim has to pause for a second whenever the first character of one of its maps is pressed. It does this to see if you are typing a map or just a sequence of characters that just happens to start with the mapped character. By choosing the comma, every time I type a comma and then the following space, vim knows almost immediately that I don't want a map.

For more complex map examples see the section Example maps and vimrc files.

For more information about mapping keys type:

:help key-mapping