Last active
March 16, 2021 14:58
-
-
Save ggandor/93a956c32191516a8846930ffe28389e to your computer and use it in GitHub Desktop.
Vim replace operator on steroids
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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