Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2014 16:38
Show Gist options
  • Save orlp/8c25ed4abb36372bc6fe to your computer and use it in GitHub Desktop.
Save orlp/8c25ed4abb36372bc6fe to your computer and use it in GitHub Desktop.
" quick replace occurences
let g:should_inject_replace_occurences = 0
function! MoveToNext()
if g:should_inject_replace_occurences
call feedkeys("n")
call repeat#set("\<Plug>ReplaceOccurences")
let g:should_inject_replace_occurences = 0
augroup auto_move_to_next
autocmd! InsertLeave * :call MoveToNext()
augroup END
nmap <silent> <Plug>ReplaceOccurences :call ReplaceOccurence()<CR>
nmap <silent> <Leader>r :let @/ = '\C\<'.expand('<cword>').'\>'<CR>
\:set hlsearch<CR>:let g:should_inject_replace_occurences=1<CR>cgn
vmap <silent> <Leader>r :<C-U>
\let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
\gvy:let @/ = substitute(
\escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR>:set hlsearch<CR>:let g:should_inject_replace_occurences=1<CR>
\gV:call setreg('"', old_reg, old_regtype)<CR>cgn
function! ReplaceOccurence()
" check if we are on top of an occurence
let l:winview = winsaveview()
let l:save_reg = getreg('"')
let l:save_regmode = getregtype('"')
let [l:lnum_cur, l:col_cur] = getpos(".")[1:2]
normal! ygn
let [l:lnum1, l:col1] = getpos("'[")[1:2]
let [l:lnum2, l:col2] = getpos("']")[1:2]
call setreg('"', l:save_reg, l:save_regmode)
call winrestview(winview)
" if we are on top of an occurence, replace it
if l:lnum_cur >= l:lnum1 && l:lnum_cur <= l:lnum2 && l:col_cur >= l:col1 && l:col_cur <= l:col2
exe "normal! cgn\<c-a>\<esc>"
call feedkeys("n")
call repeat#set("\<Plug>ReplaceOccurences")
Copy link

lukelbd commented Jul 11, 2018

Thanks a lot for writing this -- I love it! Saw the reddit thread where you mentioned it -- you're absolutely right that this method is superior to :s substitution. Much smoother.

Copy link

kuator commented Jan 28, 2019

Hello, cool script. I wanted to ask what the call repeat#set("\<Plug>ReplaceOccurences") line does. Vim prints error saying that the function is not found. What should I do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment