lo tolmencre
lo tolmencre

Reputation: 3934

How to bind key to unexposed plugin function?

I am using Commentary. It defines the following keybindings:

command! -range -bar Commentary call s:go(<line1>,<line2>)
xnoremap <expr>   <Plug>Commentary     <SID>go()
nnoremap <expr>   <Plug>Commentary     <SID>go()
nnoremap <expr>   <Plug>CommentaryLine <SID>go() . '_'
onoremap <silent> <Plug>Commentary        :<C-U>call <SID>textobject(get(v:, 'operator', '') ==# 'c')<CR>
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
nmap <silent> <Plug>CommentaryUndo :echoerr "Change your <Plug>CommentaryUndo map to <Plug>Commentary<Plug>Commentary"<CR>

if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
  xmap gc  <Plug>Commentary
  nmap gc  <Plug>Commentary
  omap gc  <Plug>Commentary

  nmap gcc <Plug>CommentaryLine " <-------------------- I WANT TO REBIND THIS

  if maparg('c','n') ==# '' && !exists('v:operator')
    nmap cgc <Plug>ChangeCommentary
  endif
  nmap gcu <Plug>Commentary<Plug>Commentary
endif

To keep some of my muscle memory compatible between Vim and Emacs I want to map gcc to M-;, as that is my Emacs binding for comment toggle. But I can't figure out how to do that, as CommentaryLine is not exposed. Meaning I cannot call it with : from the "minibuffer" (name for that in Vim?).

How can such unexposed functions, that are only accessible to the user through predefined key bindings, be mapped?

Upvotes: 0

Views: 216

Answers (1)

romainl
romainl

Reputation: 196526

"Plug" mappings let plugin authors create as many mapping as they want for their plugin without interfering with the user's own mappings:

  • the plugin exposes a <Plug>Whatever that's not mapped to any key,
  • the user can map that plug mapping to whatever key or key sequence he wants.

In this case, the author creates a number of plug mappings (<Plug>CommentaryLine, <Plug>Commentary, etc.) and maps them to harmless key sequences (gc, gcc, etc. that don't do anything in Vim by default) after checking if they are not already mapped to something else.

But I can't figure out how to do that, as CommentaryLine is not exposed. Meaning I cannot call it with : from the "minibuffer" (name for that in Vim?).

Well, there's no CommentaryLine command or function to begin with so you will have a hard time finding it exposed anywhere or calling it from the command-line (that's the name of your "minibuffer").

How can such unexposed functions, that are only accessible to the user through predefined key bindings, be mapped?

Again, CommentaryLine is worse than unexposed; it's non-existing!

nmap gcc <Plug>CommentaryLine " <-------------------- I WANT TO REBIND THIS

Did you try the following?

nmap <key> <Plug>CommentaryLine

See :help <Plug>.

Upvotes: 3

Related Questions