Skip to content

Instantly share code, notes, and snippets.

@PeterRincker
Forked from orlp/gist:8c25ed4abb36372bc6fe
Last active April 18, 2022 20:20
Show Gist options
  • Select an option

  • Save PeterRincker/08edd3ea980e778d3986 to your computer and use it in GitHub Desktop.

Select an option

Save PeterRincker/08edd3ea980e778d3986 to your computer and use it in GitHub Desktop.

Revisions

  1. PeterRincker revised this gist Sep 10, 2018. 1 changed file with 18 additions and 3 deletions.
    21 changes: 18 additions & 3 deletions quick-replace.vim
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,11 @@
    " Repeat change with `.` (jumps to next match).
    " Can also visually select characters and use `C` set match and start a change

    if exists('g:loaded_quick_replace') || &cp
    finish
    endif
    let g:loaded_quick_replace = 1

    function! s:gn_next()
    augroup gn_next_repeat
    autocmd!
    @@ -31,7 +36,17 @@ function! s:match_visual()
    call setreg('"', reg, type)
    endfunction

    onoremap <expr> <Plug>(gn-next) <SID>gn_next()
    onoremap <expr> <SID>(gn-next) <SID>gn_next()
    onoremap <script> <Plug>(gn-next) <SID>(gn-next)
    nnoremap <script> <Plug>(quick-replace) :let @/ = '\V\<' . escape(expand('<cword>'), '\') . '\>'<cr>c<SID>(gn-next)
    nnoremap <script> <Plug>(quick-replace-g*) :let @/ = '\V' . escape(expand('<cword>'), '\')<cr>c<SID>(gn-next)
    xnoremap <script> <Plug>(quick-replace) :<c-u>call <SID>match_visual()<cr>c<SID>(gn-next)
    if !hasmapto("<Plug>(quick-replace)", 'n')
    nmap cm <Plug>(quick-replace)
    endif

    nmap <Plug>(quick-replace) :let @/ = '\V\<' . escape(expand('<cword>'), '\') . '\>'<cr>c<Plug>(gn-next)
    xmap <Plug>(quick-replace) :<c-u>call <SID>match_visual()<cr>c<Plug>(gn-next)
    if !hasmapto("<Plug>(quick-replace)", 'v')
    xmap C <Plug>(quick-replace)
    endif
  2. PeterRincker revised this gist May 18, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions quick-replace.vim
    Original file line number Diff line number Diff line change
    @@ -25,10 +25,10 @@ function! s:gn_next()
    endfunction

    function! s:match_visual()
    let reg = @@
    let [reg, type] = [@@, getregtype('"')]
    normal! gvy
    let @/ = '\V' . escape(@@, '\')
    let @@ = reg
    call setreg('"', reg, type)
    endfunction

    onoremap <expr> <Plug>(gn-next) <SID>gn_next()
  3. PeterRincker revised this gist May 18, 2018. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions quick-replace.vim
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    " Quick Replace
    " Makes *``cgn like workflows faster by automatically moving to next match
    " Repeat with `.`
    " Makes *``cgn like workflows faster by automatically moving to next match. Repeat with `.`
    "
    " Requires repeat.vim
    "
    " Example mappings:
    " nmap cm <Plug>(quick-replace)
    @@ -17,6 +18,7 @@ function! s:gn_next()
    autocmd!
    autocmd CursorMoved <buffer>
    \ execute "autocmd! gn_next_repeat" |
    \ silent! call repeat#set(v:operator . "\<Plug>(gn-next)" . (v:operator == 'c' ? "\<c-a>\<esc>" : '')) |
    \ normal! n
    augroup END
    return "\<esc>:let &hlsearch=&hlsearch\<cr>" . v:operator . "gn"
  4. PeterRincker revised this gist May 18, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions quick-replace.vim
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,9 @@
    "
    " mnemonic: cm for change matches
    "
    " The execute `cmfoo<esc>` on a word. Your cursor will then move to the next word.
    " Repeat change with `.`
    " Can also visually select characters and use `C` to start changes
    " Execute `cmfoo` on a word. Your cursor will then move to the next word after the change.
    " Repeat change with `.` (jumps to next match).
    " Can also visually select characters and use `C` set match and start a change

    function! s:gn_next()
    augroup gn_next_repeat
  5. PeterRincker revised this gist May 18, 2018. 1 changed file with 15 additions and 5 deletions.
    20 changes: 15 additions & 5 deletions quick-replace.vim
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,22 @@
    " Quick Replace
    " Makes *``cgn like workflows faster by automatically moving to next match
    " Repeat with `.`
    "
    " Example mappings:
    " nmap cm <Plug>(quick-replace)
    " xmap C <Plug>(quick-replace)
    "
    " mnemonic: cm for change matches
    "
    " The execute `cmfoo<esc>` on a word. Your cursor will then move to the next word.
    " Repeat change with `.`
    " Can also visually select characters and use `C` to start changes

    function! s:gn_next()
    augroup gn_next_repeat
    autocmd!
    autocmd CursorMoved <buffer>
    \ execute "autocmd! gn_next_repeat" |
    \ silent! call repeat#set(v:operator . "\<Plug>(gn-next)" . (v:operator == 'c' ? "\<c-a>\<esc>" : '')) |
    \ normal! n
    augroup END
    return "\<esc>:let &hlsearch=&hlsearch\<cr>" . v:operator . "gn"
    @@ -19,7 +32,4 @@ endfunction
    onoremap <expr> <Plug>(gn-next) <SID>gn_next()
    nmap <Plug>(quick-replace) :let @/ = '\V\<' . escape(expand('<cword>'), '\') . '\>'<cr>c<Plug>(gn-next)
    xmap <Plug>(quick-replace) :<c-u>call <SID>match_visual()<cr>c<Plug>(gn-next)
    nmap <leader>r <Plug>(quick-replace)
    xmap <leader>r <Plug>(quick-replace)
    xmap <Plug>(quick-replace) :<c-u>call <SID>match_visual()<cr>c<Plug>(gn-next)
  6. PeterRincker renamed this gist Dec 15, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. PeterRincker revised this gist Dec 14, 2014. 1 changed file with 20 additions and 39 deletions.
    59 changes: 20 additions & 39 deletions gistfile1.vim
    Original file line number Diff line number Diff line change
    @@ -1,44 +1,25 @@
    " 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")
    endif
    function! s:gn_next()
    augroup gn_next_repeat
    autocmd!
    autocmd CursorMoved <buffer>
    \ execute "autocmd! gn_next_repeat" |
    \ silent! call repeat#set(v:operator . "\<Plug>(gn-next)" . (v:operator == 'c' ? "\<c-a>\<esc>" : '')) |
    \ normal! n
    augroup END
    return "\<esc>:let &hlsearch=&hlsearch\<cr>" . v:operator . "gn"
    endfunction

    let g:should_inject_replace_occurences = 0
    function! s:match_visual()
    let reg = @@
    normal! gvy
    let @/ = '\V' . escape(@@, '\')
    let @@ = reg
    endfunction

    augroup auto_move_to_next
    autocmd! InsertLeave * :call MoveToNext()
    augroup END
    onoremap <expr> <Plug>(gn-next) <SID>gn_next()
    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
    nmap <Plug>(quick-replace) :let @/ = '\V\<' . escape(expand('<cword>'), '\') . '\>'<cr>c<Plug>(gn-next)
    xmap <Plug>(quick-replace) :<c-u>call <SID>match_visual()<cr>c<Plug>(gn-next)
    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>"
    endif

    call feedkeys("n")
    call repeat#set("\<Plug>ReplaceOccurences")
    endfunction
    nmap <leader>r <Plug>(quick-replace)
    xmap <leader>r <Plug>(quick-replace)
  8. @orlp orlp created this gist Dec 13, 2014.
    44 changes: 44 additions & 0 deletions gistfile1.vim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    " 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")
    endif

    let g:should_inject_replace_occurences = 0
    endfunction

    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>"
    endif

    call feedkeys("n")
    call repeat#set("\<Plug>ReplaceOccurences")
    endfunction