1

Consider the following scenario

Content of the buffer:

I saw the water in a thermal bath.

Commands issued:

:set incsearch

/th^R^W^R^W^R^W^R^W

Result

thewaterIII

Question

What exactly is the algorithm of the incremental search autocomplete in this case? Why does it behave in such a strange way?

In particular:

  • why does it add water without a space?
  • why does it add only the first I letter of the next word in?
  • why is that letter capitalized?
  • why does it get stuck there and does not proceed further?

I would have expected something like this:

^R^W the

^R^W^R^W the water

^R^W^R^W^R^W the water in

... etc

2 Answers 2

5

why does it add water

C-r C-w inserts the word positioned under your cursor; however, if 'incsearch' is set, and there is a match in the buffer for the current contents of the command-line, Vim doesn't use the cursor position anymore, but the end of the match. From :h c^r^w:

When 'incsearch' is set the cursor position at the end of the
currently displayed match is used.

When your command-line contains the, the end of the match is here:

I saw the water in a thermal bath.
         ^

There is no word at that position, so Vim inserts the next word which is water. I guess Vim uses a similar algorithm as with the * command:

*                       Search forward for the [count]'th occurrence of the
                        word nearest to the cursor.  The word used for the
                        search is the first of:
                                1. the keyword under the cursor |'iskeyword'|
                                2. the first keyword after the cursor, in the
                                   current line
                                3. the non-blank word under the cursor
                                4. the first non-blank word after the cursor,
                                   in the current line
                        Only whole keywords are searched for, like with the
                        command "/\<keyword\>".  |exclusive|
                        'ignorecase' is used, 'smartcase' is not.

without a space?

I guess that's because C-r C-w is meant to insert a word, and a space character is not considered as being part of a word.

why does it add only the first I letter of the next word in?

It's not the first letter of the next word; it's the word under your cursor.

I saw the water in a thermal bath.
^

When your search command-line contains thewater, Vim can't use the end of the match as the position of the word because there is no match for thewater in your buffer; so it falls back to your cursor position. And under your cursor, the word is I, not in.

why is that letter capitalized?

Again, it's not just a capitalized letter; it's the word under your cursor.

why does it get stuck there and does not proceed further?

Because no matter how many I you insert, there is still no match in the buffer; so Vim keeps using the cursor position to decide which word to insert.


To insert multiple words from the current line, you could also:

  1. press C-r C-l to insert the whole contents of the line (see :h c^r^l)
  2. use traditional editing commands to remove the words you're not interested in (see :h ex-edit-index)

Note that c^r^l requires the patch 8.0.1787. Without, you could try some custom mapping (e.g. cno <c-r><c-l> <c-r><c-r>=getline('.')<cr>).

3

user938271's great answer already answers your explicit questions about what happens when you press Ctrl-RCtrl-W during an incremental search.

I just wanted to suggest that you might want to try pressing Ctrl-L instead. During a search with 'incsearch' set, this adds the next character after the current match each time it is pressed.

So with your example, typing:

/th^L^L^L^L^L^L^L

Will result in:

/the water

(You can of course hold down the keys to auto-repeat.)

See :help /_CTRL-L for more details.

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.