Execute <Plug> commands in vim

Tarrasch picture Tarrasch · Aug 31, 2013 · Viewed 17.6k times · Source

I've recently found a vim plugin using something called <Plug>. For example there is a command <Plug>abc_def which I would like to execute.

I've tried everything like :<Plug>abc_def and similar. Nothing worked. And :help <Plug> gave no information.

However, I've been able to execute it by creating a mapping :map x <Plug>(unite_redraw). Then I can execute it by pressing x.

Now, is there any way to execute :<Plug>abc_def without creating a dummy mapping just to run it? The actual plugin I use is Unite.

Answer

glts picture glts · Aug 31, 2013

<Plug> mappings are meant to be mapped and called via the map. A <Plug> map is a device to expose a clean interface of plugin actions to the user.


Example: Instead of mapping some key to some plugin function in the plugin's hard code, such as "map zz to the action 'center cursor aesthetically'",

nnoremap <expr> zz 'zz'.float2nr(winheight(0)*0.1).'<C-E>'

it is better to expose only a named <Plug> mapping that the user can then remap, without having to copy and paste the "action".

nnoremap <expr> <Plug>NiceCenterCursor 'zz'.float2nr(winheight(0)*0.1).'<C-E>'
nmap zz <Plug>NiceCenterCursor

This is then easy to override, reuse, plug into by the user.


<Plug> mappings are active only in the modes they have been defined for. To execute a <Plug> mapping that is defined for normal mode, you can do as with any normal command: use :normal (without the exclamation mark).

:execute "normal \<Plug>NiceCenterCursor"

Since <Plug> actually represents a special magic key, we need to use :normal together with :execute and escape the <Plug>.

The <Plug> mechanism is described in depth at :h 41.11. See also this article about this topic by a Vim master.