function! s:PadMacro(type) abort
if a:type ==# 'char'
normal! `[v`]d
elseif a:type ==# 'line'
normal! '[V']d
elseif a:type ==# 'v'
normal! `<v`>d
else
return
endif
let string = '\Pad{' . @" . '}'
silent execute "normal! i\<c-r>=string\r"
endfunction
nnoremap <silent> ;P :set operatorfunc=<sid>PadMacro<cr>g@
vnoremap <silent> ;P :<c-u>call <sid>PadMacro(visualmode())<cr>
If you put the previous code in your vimrc, it should do what you want.
To use it, hit ;P{motion}
and the PadMacro()
function should enclose the characters covered by {motion}
with \Pad{
and }
.
Usage examples:
;Pl
to enclose the character under the cursor (because l
is a motion which covers the character under the cursor)
;Piw
to enclose the word under the cursor (because iw
is a text object which covers all the characters inside the word under the cursor )
;Pis
to enclose the sentence in which the cursor is (because is
is a text object which covers all the characters inside a sentence)
;P$
to enclose the characters between your cursor and the end of the line
;P
to enclose the characters visually selected
Everything done by the PadMacro()
function is considered as a single edition which means it's dot repeatable.
In other words, if you have several similar portions of text to enclose with \Pad{}
there's no need to hit the mapping every time, only the first time (after that you can hit .
to repeat your last edition).
For a complete list of text-objects on which you can apply your enclosure, see:
:help text-objects
.