Skip to content

Instantly share code, notes, and snippets.

@ggandor
Last active March 16, 2021 14:58
Show Gist options
  • Save ggandor/93a956c32191516a8846930ffe28389e to your computer and use it in GitHub Desktop.
Save ggandor/93a956c32191516a8846930ffe28389e to your computer and use it in GitHub Desktop.
Vim replace operator on steroids
" Replace operator that takes an additional motion
" Usage: yr{motion}{char}
" Try it together with targets.vim text objects, the possibilities are endless!
" TODO: Don't ask for input each time when dot-repeated. (Or is that a feature?)
fun! s:Replace(type)
let sel_save = &selection
let &selection = "inclusive"
let reg_save = @@
let c = getchar()
if c =~# "\<esc>" || c =~# "\<c-c>"
return
endif
if type(c) == v:t_number
let c = nr2char(c)
endif
if a:type ==# 'line'
silent exe "normal! `[V`]"
elseif a:type ==# 'char'
silent exe "normal! `[v`]"
endif
exe "normal! r".c
let &selection = sel_save
let @@ = reg_save
endfun
nnoremap <silent> yr :set opfunc=<SID>Replace<CR>g@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment