Skip to content

Instantly share code, notes, and snippets.

@Mekajiki
Last active August 29, 2015 14:01
Show Gist options
  • Save Mekajiki/54374a00781236eedff5 to your computer and use it in GitHub Desktop.
Save Mekajiki/54374a00781236eedff5 to your computer and use it in GitHub Desktop.
vim function to replace selection with current register
function! ReplaceWithRegister(type, ...)
let sel_save = &selection
let &selection = "inclusive"
let reg_a = @a
let reg_z = @z
let @z = @@
if a:0 " Invoked from Visual mode, use '< and '> marks.
silent exe 'normal! `<' . a:type . '`>"ad"zP'
elseif a:type == 'line'
silent exe "normal! '[V']\"ad\"zP"
elseif a:type == 'block'
silent exe "normal! `[\<C-V>`]\"ad`[\"zP"
else
silent exe 'normal! `["ad`]"Ax"zP'
endif
let &selection = sel_save
let @@ = @a
let @a = reg_a
let @z = reg_z
endfunction
" map to ;
" typing ;w replace a word with current register
nmap <silent> ; :set opfunc=ReplaceWithRegister<CR>g@
vmap <silent> ; :<C-U>call ReplaceWithRegister(visualmode(), 1)<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment