" Replace the operated text with the contents of a register function! replace_operator#Operatorfunc(type) abort " Save the current value of the unnamed register and the current value of " the 'clipboard' and 'selection' options into a dictionary for restoring " after this is all done let save = { \ 'unnamed': @@, \ 'clipboard': &clipboard, \ 'selection': &selection \ } " Don't involve any system clipboard for the duration of this function set clipboard-=unnamed set clipboard-=unnamedplus " Ensure that we include end-of-line and final characters in selections set selection=inclusive " Build normal mode keystrokes to select the operated text in visual mode if a:type ==# 'line' let select = "'[V']" elseif a:type ==# 'block' let select = "`[\`]" else let select = '`[v`]' endif " Build normal mode keystrokes to paste from the selected register; only add " a register prefix if it's not the default unnamed register, because Vim " before 7.4 gets ""p wrong in visual mode let paste = 'p' if s:register !=# '"' let paste = '"'.s:register.paste endif silent execute 'normal! '.select.paste " Restore contents of the unnamed register and the previous values of the " 'clipboard' and 'selection' options let @@ = save['unnamed'] let &clipboard = save['clipboard'] let &selection = save['selection'] endfunction " Helper function for normal mode map function! replace_operator#(register) abort let s:register = a:register set operatorfunc=replace_operator#Operatorfunc return 'g@' endfunction