5

When editing a buffer and initiating a search-and-replace, I use :s/oldword/newword/g

Now the word 'oldword' will be in the buffer I am editing, and sometimes it is a long word.

It would be helpful if I was able to auto-complete somehow the words 'oldword' and 'newword' from the contents of the buffer to save some typing, say by typing just :s/ol then TAB to cycle through matches in the buffer, and then similarly for 'newword'.

Is something like this possible without plugin ?

In general, is there a way of getting completion suggestions from the current buffer to assemble the : command line ?

1
  • while in : command mode <C-r><C-w> yanks the word under the cursor into the command line. Feb 10, 2020 at 12:16

3 Answers 3

4

I don't know if there's a way to do exactly what you are asking, but there are two alternatives I think are close enough:

  1. If you open a command-line window with q: (from normal mode) or CTRL-F (from command-line mode), you can edit your commands with the full power of Vim. This includes using CTRL-P for autocompletion, which depending on the value of your 'complete' setting, can look for matches in your current buffer (include . in the value of the setting for that).
  2. Not autocompletion, but if you have the long word under the cursor while editing the buffer, you could yank it first, and then paste it into the command line with CTRL-R {register} when editing the command line.
1
  • 2
    If you have the long word under the cursor while editing the buffer, you don’t need to yank it first: you can use <C-R>= to paste from the special expression register. I have cnoremap <C-Space> <C-R>=expand("<cword> ")<CR> in $MYVIMRC.
    – wchargin
    Feb 10, 2020 at 6:55
4

With set incsearch you can complete using ctrl-L in / and :s/ command lines (see :help /_CTRL-L).

3
  • 1
    Use this in combination with ctrl-G to jump to the word you want to match. Feb 10, 2020 at 4:37
  • For some reason, this works with / but not with :s/ Feb 10, 2020 at 14:38
  • @user3203476, perhaps you are using neovim (in which case see inccommand)
    – Mass
    Feb 11, 2020 at 1:46
3

Ideas:

  • yank the word and paste it while typing (<C-r>")
  • type the command in the buffer and use :exec getline('.') to run it

I prefer the q:/<C-f> from the other answer though.

Your Answer

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

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