首页 > 解决方案 > 如何将密钥绑定到未公开的插件功能?

问题描述

我正在使用评论。它定义了以下键绑定:

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

为了让我的一些肌肉记忆在 Vim 和 Emacs 之间保持兼容,我想映射gccM-;,因为那是我的 Emacs 绑定用于评论切换。但我不知道该怎么做,因为CommentaryLine没有暴露。这意味着我不能:从“迷你缓冲区”(Vim 中的名称?)中调用它。

如何映射这些只能通过预定义的键绑定访问的未公开功能?

标签: vimpluginskey-bindings

解决方案


“Plug”映射让插件作者可以为他们的插件创建尽可能多的映射,而不会干扰用户自己的映射:

  • 该插件公开了一个<Plug>Whatever未映射到任何键的
  • 用户可以将该插头映射映射到他想要的任何键或键序列。

在这种情况下,作者创建了许多插件映射(<Plug>CommentaryLine,<Plug>Commentary等),并在检查它们是否尚未映射到之后将它们映射到无害的键序列(gc,gcc等,默认情况下在 Vim 中不做任何事情)别的东西。

但我不知道该怎么做,因为CommentaryLine没有暴露。这意味着我不能:从“迷你缓冲区”(Vim 中的名称?)中调用它。

好吧,没有CommentaryLine命令或函数开始,所以你很难找到它暴露在任何地方或从命令行调用它(这是你的“迷你缓冲区”的名称)。

如何映射这些只能通过预定义的键绑定访问的未公开功能?

再次,CommentaryLine比未曝光更糟糕;它不存在!

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

您是否尝试过以下操作?

nmap <key> <Plug>CommentaryLine

:help <Plug>


推荐阅读