operator-pending mode.
Suppose that you want to define <F7> so that the command d<F7> deletes a C
program block (text enclosed in curly braces, {}). Similarly y<F7> would yank
the program block into the unnamed register. Therefore, what you need to do
is to define <F7> to select the current program block. You can do this with
the following command: >
:omap <F7> a{
This causes <F7> to perform a select block "a{" in operator-pending mode, just
like you typed it. This mapping is useful if typing a { on your keyboard is a
bit difficult.
LISTING MAPPINGS
To see the currently defined mappings, use ":map" without arguments. Or one
of the variants that include the mode in which they work. The output could
look like this:
_g :call MyGrep(1)<CR> ~
v <F2> :s/^/> /<CR>:noh<CR>`` ~
n <F2> :.,$s/^/> /<CR>:noh<CR>`` ~
<xHome> <Home>
<xEnd> <End>
The first column of the list shows in which mode the mapping is effective.
This is "n" for Normal mode, "i" for Insert mode, etc. A blank is used for a
mapping defined with ":map", thus effective in both Normal and Visual mode.
One useful purpose of listing the mapping is to check if special keys in <>
form have been recognized (this only works when color is supported). For
example, when <Esc> is displayed in color, it stands for the escape character.
When it has the same color as the other text, it is five characters.
REMAPPING
The result of a mapping is inspected for other mappings in it. For example,
the mappings for <F2> above could be shortened to: >
:map <F2> G<F3>
:imap <F2> <Esc><F3>
:map <F3> oDate: <Esc>:read !date<CR>kJ
For Normal mode <F2> is mapped to go to the last line, and then behave like
<F3> was pressed. In Insert mode <F2> stops Insert mode with <Esc> and then
also uses <F3>. Then <F3> is mapped to do the actual work.
Suppose you hardly ever use Ex mode, and want to use the "Q" command to format
text (this was so in old versions of Vim). This mapping will do it: >
:map Q gq
But, in rare cases you need to use Ex mode anyway. Let's map "gQ" to Q, so
that you can still go to Ex mode: >
:map gQ Q
What happens now is that when you type "gQ" it is mapped to "Q". So far so
good. But then "Q" is mapped to "gq", thus typing "gQ" results in "gq", and
you don't get to Ex mode at all.
To avoid keys to be mapped again, use the ":noremap" command: >
:noremap gQ Q
Now Vim knows that the "Q" is not to be inspected for mappings that apply to
it. There is a similar command for every mode:
:noremap Normal, Visual and Operator-pending
:vnoremap Visual
:nnoremap Normal
:onoremap Operator-pending
:noremap! Insert and Command-line
:inoremap Insert
:cnoremap Command-line
RECURSIVE MAPPING
When a mapping triggers itself, it will run forever. This can be used to
repeat an action an unlimited number of times.
For example, you have a list of files that contain a version number in the
first line. You edit these files with `vim *.txt`. You are now editing the
first file. Define this mapping: >
:map ,, :s/5.1/5.2/<CR>:wnext<CR>,,
Now you type ",,". This triggers the mapping. It replaces "5.1" with "5.2"
in the first line. Then it does a ":wnext" to write the file and edit the
next one. The mapping ends in ",,". This triggers the same mapping again,
thus doing the substitution, etc.
This continues until there is an error. In this case it could be a file
where the substitute command doesn't find a match for "5.1". You can then
make a change to insert "5.1" and continue by typing ",," again. Or the
":wnext" fails, because you are in the last file in the list.
When a mapping runs into an error halfway, the rest of the mapping is
discarded. CTRL-C interrupts the mapping (CTRL-Break on MS-Windows).
DELETE A MAPPING
To remove a mapping use the ":unmap" command. Again, the mode the unmapping
applies to depends on the command used:
:unmap Normal, Visual and Operator-pending
:vunmap Visual
:nunmap Normal