" 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 l: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 l:select = '''[V'']' elseif a:type ==# 'block' let l:select = "`[\`]" else let l: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 l:paste = 'p' if s:register !=# '"' let l:paste = '"'.s:register.l:paste endif silent execute 'normal! '.l:select.l:paste " Restore contents of the unnamed register and the previous values of the " 'clipboard' and 'selection' options let @@ = l:save['unnamed'] let &clipboard = l:save['clipboard'] let &selection = l:save['selection'] endfunction " Helper function for normal mode map function! replace_operator#Map(register) abort let s:register = a:register set operatorfunc=replace_operator#Operatorfunc return 'g@' endfunction