Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute <Plug> commands in vim

Tags:

vim

vim-plugin

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.

like image 673
Tarrasch Avatar asked Aug 31 '13 09:08

Tarrasch


People also ask

Where are Vim plug plugins stored?

By default, Vim expects each plugin to be stored in the ~/. vim/pack/<my-plugins>/. When Vim starts, it will load all plugins stored in /. vim/pack/my-plugins/start.


1 Answers

<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 from the plugin:

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

that the user can then remap in their config, without having to copy and paste the "action":

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.

like image 106
glts Avatar answered Oct 21 '22 01:10

glts