如何为VIM添加自定义动词?

use*_*789 5 vi vim

我想为vim定义一个新动词(比如'o'),它可以对任何现有的vim textobject进行操作.有关如何做到这一点的任何指示?

谢谢AB

Pet*_*ker 6

这些动词称为运算符(请参阅参考资料: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
Run Code Online (Sandbox Code Playgroud)

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

获得更多帮助

:h operator
:h :map-operator
:h 'opfunc'
:h g@
Run Code Online (Sandbox Code Playgroud)