3

How do I get my cursor to not move to the last thing it replaced after a search and replace. No <C-o> or extra key presses. Is there some autocmd magic I can setup to listen on any substitute execution and perform a <C-o> automatically after it completes?

If you're curious about further details, it's really for this bread-and-butter mapping, search and replace word under cursor for the whole file, honoring case. I type the binding then start typing the replacement:

nnoremap <leader>sr :%s/<c-r><c-w>//gI<left><left><left>

and the neovim feature to animate the search and replace:

if has("nvim")
    set inccommand=nosplit " No preview window
endif
3
  • It is highly recommended not to set gdefault, as it confuses the intuitive meaning of flags and can interfere with plugins which aren’t expecting it/haven’t coded defensively for it.
    – D. Ben Knoble
    Commented Jul 20, 2019 at 13:43
  • @D.BenKnoble Good point, updated my vimrc and this question. Commented Jul 20, 2019 at 13:58
  • add a '' after substitution?
    – JJoao
    Commented Jul 26, 2019 at 12:48

1 Answer 1

0

I forgot that you can prepare a series of commands separated by |.

The solution I'm happy with is:

if has("nvim")
  set inccommand=nosplit " Remove horizontal split that shows a preview of whats changing
endif
" E means edit with confirms, e is no confirm. 
" Second letter is source: w is word under cursor, y is yanked text.
" Even with very no magic (\V) modifier, still need to escape / and \ with \
" The \< and \> means don't do a raw string replace but a word replace
" so if you want to replace someVar, it won't touch vars name someVarOther
" edit word in whole file
nnoremap <leader>ew :%s/\V\<<c-r><c-w>\>//gI \|normal <c-o><c-left><c-left><left><left><left><left>
" Edit confirm word in whole file
nnoremap <leader>Ew :,$s/\V\<<c-r><c-w>\>//gIc \|1,''-&&<c-left><left><left><left><left><left>
" edit word under cursor within the visual lines
" gv selects the last vis selection (line, block or select)
vnoremap <leader>ew <Esc>yiwgv:s/\V\<<c-r>"\>//gI \| normal <c-o><c-left><c-left><c-left><left><left><left><left>
" Visually selected text in file
" If mode is visual line mode, edit the prev yank acros the vis lines, else across the whole file
" see :help escape()
vnoremap <expr> <leader>ey mode() ==# "V" ? 
      \ ":s/\\V<c-r><c-r>=escape(@\", '/\\')<cr>//gI \| normal <c-o><c-left><c-left><c-left><left><left><left><left>"
      \: "y:%s/\\V<c-r><c-r>=escape(@\", '/\\')<cr>//gI \| normal <c-o><c-left><c-left><c-left><left><left><left><left>"

nnoremap <leader>ey :%s/\V<c-r>=escape(@", '/\\')<cr>//gI <bar> normal <c-o><c-left><c-left><c-left><left><left><left><left>
nnoremap <leader>Ey :%s/\V<c-r>=escape(@", '/\\')<cr>//gIc <bar> normal <c-o><c-left><c-left><c-left><left><left><left><left><left>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.