key_mapping 

Send to Kindle
home » snippets » vim » key_mapping



Notes

From Listing Mappings

Char Mode
Space Normal, visual, select and operator-pending
n Normal
v Visual or Select
s Select
x Visual
o Operator-pending
! Insert and Command-line
i Insert
l :lmap mappings for Insert, Command-line and Lang-Arg
c Command-line

Just before the {rhs}, a special character can appear

Char Effect
* indicates that it is not remappable
& indicates that only script-local mappings are remappable
@ indicates a buffer local mapping

To map a backslash or use a backslash literally in the {rhs}, the special sequence <Bslash> can be used.  This avoids the need to double backslashes when using nested mappings.

How To

Fix timeouts if something messed it up

:set timeout timeoutlen=1000 ttimeoutlen=250

What's the way to specify that key in a mapping?

Type <C-K>KEY at the : prompt. It'll substitute the right thing. For instance, if you type <C-K>CONTROL-SPACE, it'll display as

Command line mappings

The <C-\>e sequence replaces the entire command-line with the value returned by the invoked function.

Operator pending mode

Operator-pending mappings can be used to define a movement command that can be used with any operator. Simple example: ":omap { w"makes "y{"work like "yw" and "d{"like"dw".

To ignore the starting cursor position and select different text, you can have the omap start Visual mode to select the text to be operated upon. Example that operates on a function name in the current line:

onoremap <silent> F :<C-U>normal! 0f(hviw<CR>

The CTRL-U (<C-U>) is used to remove the range that Vim may insert.  The Normal mode commands find the first '(' character and select the first word before it. That usually is the function name.

To enter a mapping for Normal and Visual mode, but not Operator-pending mode, first define it for all three modes, then unmap it for Operator-pending mode:

:map    xx something-difficult
:ounmap xx

Likewise for a mapping for Visual and Operator-pending mode or Normal and Operator-pending mode.

Use of <Plug>

If you are developing a Vim plugin or script and you want to provide the user with the flexibility of assigning his own key map to invoke a function provided by your script, then you can prefix the map with <Plug>.

For example, let us say a plugin has a function s:VimScriptFn() and the user has to create a map to assign a key to invoke this function. The plugin can provide the following map to simplify this:

noremap <unique> <Plug>ScriptFunc :call <SID>VimScriptFn()<CR>

Note that in the above map command, instead of the typical key sequence for the {lhs} of the map, the <Plug>ScriptFunc text is used. The <Plug> generates a unique key sequence that a user cannot enter from a keyboard. The above map is visible outside of the script where it is defined.

With the above command, the user can assign _p to invoke the script function as shown below:

:nmap _p <Plug>ScriptFunc