Skip to content

Instantly share code, notes, and snippets.

@romainl
Last active August 4, 2023 07:46
Show Gist options
  • Save romainl/b00ccf58d40f522186528012fd8cd13d to your computer and use it in GitHub Desktop.
Save romainl/b00ccf58d40f522186528012fd8cd13d to your computer and use it in GitHub Desktop.
Substitute operator
function! Substitute(type, ...)
let cur = getpos("''")
call cursor(cur[1], cur[2])
let cword = expand('<cword>')
execute "'[,']s/" . cword . "/" . input(cword . '/')
call cursor(cur[1], cur[2])
endfunction
nmap <silent> <key> m':set opfunc=Substitute<CR>g@
" Usage:
" <key>ipfoo<CR> Substitute every occurrence of the word under
" the cursor with 'foo' in the current paragraph
" <key>Gfoo<CR> Same, from here to the end of the buffer
" <key>?bar<CR>foo<CR> Same, from previous occurrence of 'bar'
" to current line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment