Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do one add custom verbs to VIM?

Tags:

vim

vi

I would like to define a new verb to vim (say 'o') which can operate on any of the existing vim textobjects. Any pointers on how I can go about doing this?

Thanks AB

like image 706
user1595789 Avatar asked Aug 13 '12 15:08

user1595789


1 Answers

These verbs are called operators (see :h operator). If you want to build your own operator you must use the 'operatorfunc' setting then execute g@. The vim documentation explains it best on how to do this, please see (:h :map-operator) Here is the example from the vim documentation:

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

If you want another example please see take a look at Tim Pope's commentary plugin.

For more help

:h operator
:h :map-operator
:h 'opfunc'
:h g@
like image 173
Peter Rincker Avatar answered Oct 10 '22 14:10

Peter Rincker