如何将自定义谓词添加到 VIM



我想为 vim 定义一个新的动词(比如说"o"),它可以对任何现有的 vim 文本对象进行操作。关于我如何做到这一点的任何指示?

谢谢血型

这些动词称为运算符(见:h operator)。如果要构建自己的运算符,则必须使用'operatorfunc'设置,然后执行g@。vim 文档最好地解释了如何做到这一点,请参阅 ( :h :map-operator ) 以下是 vim 文档中的示例:

nmap <silent> <F4> :set opfunc=CountSpaces<CR>g@
vmap <silent> <F4> :<C-U>call CountSpaces(visualmode(), 1)<CR>
function! CountSpaces(type, ...)
  let sel_save = &selection
  let &selection = "inclusive"
  let reg_save = @@
  if a:0  " Invoked from Visual mode, use '< and '> marks.
    silent exe "normal! `<" . a:type . "`>y"
  elseif a:type == 'line'
    silent exe "normal! '[V']y"
  elseif a:type == 'block'
    silent exe "normal! `[<C-V>`]y"
  else
    silent exe "normal! `[v`]y"
  endif
  echomsg strlen(substitute(@@, '[^ ]', '', 'g'))
  let &selection = sel_save
  let @@ = reg_save
endfunction

如果你想要另一个例子,请参阅Tim Pope的评论插件。

如需更多帮助

:h operator
:h :map-operator
:h 'opfunc'
:h g@

最新更新