Skip to main content

Problem:

Map is one of the lesser known and most useful commands.

You may find when you're editing that you are repeatedly using a command sequence or maybe a very complex sequence.  The map command allows you to define a macro for the editor's command mode.  In other words, you can issue it while you type; as soon as you hit the mapped key, the keystroke is replayed

Resolution:

The command format is

map

to display currently mapped keys

map mykey sequence   

to map mykey with a sequence of commands

unmap key

to remove the map.

As map is used in command mode, you need to know the key not used in command-mode and are available for your use.  These are:

G K q V v ^A ^K ^O ^T ^W ^X _ * \\ =

Example 1

Initiate vi and type the following:

Micro World Focus 2007

Enter command mode by pressing colon (:) and type the following

map g dwwP

dw

delete word (delete actually copies to a buffer like Yank)

w

forward by one word

P

Paste  

Use the map command with no argumants to confirm your mapping:

:map

up      ^[[A    k

down    ^[[B    j

left    ^[[D    h

right   ^[[C    l

home    ^[[H    H

g       g       dwwP

[Hit return to continue]

Move the cursor so it under the W and press 'g'

Move the cursor so it under the W and press 'g'

The words 'World' and 'Focus' should now be transposed.

Micro Focus World 2007

Map commands can be saved in your .exrc  file.  This file is stored in your home directory and is read by vi and ex on startup.  

vi will also read a file called .exrc in the current directory allowing you to set specific mappings and settings for a project.  

.exrc can be created by vi.  

Old KB# 3709