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
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@
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With