12

Usually insert mode completion (<C-n>) inserts the right word the first time I press <C-n>, but not always. Therefore, I would like Vim to insert the match it deems best when I press <C-n>, but still allow me to cancel this insertion afterwards should the correct match not show up or be a long way down the list.

Of course I could code this behavior, but I have a feeling there should be a standard key(-combination) to do this. Could someone point me in the right direction?

Bonus question: is there another key I can use to get the longest match (as it was before I hit <C-n>)?

For what it's worth, I'm using completeopt=menu

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
Octaviour
  • 962
  • 6
  • 18
  • 1
    Is ctrl-u what you are looking for? – statox Dec 22 '16 at 10:11
  • No, ctrl-u seems to delete the entire line. I am looking for a combination that only removes the text that was inserted by pressing ctrl-n. – Octaviour Dec 22 '16 at 10:39
  • My first thought was "doesn't c-g the usual cancel key work here?", then I realised that was emacs. Just a few days of learning years ago has corrupted me, I shall bathe in a sacred river and purify myself! – Sundar R Dec 22 '16 at 17:24

1 Answers1

18

From :h complete_CTRL-E:

                                                        *complete_CTRL-E*
When completion is active you can use CTRL-E to stop it and go back to the
originally typed text.  The CTRL-E will not be inserted.

Also if you, say, type a then ^Pand you get a very long list, you can hit ^N to return to your original text with the completion popup still active. You can then type extra characters to narrow the completion list.

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
Antony
  • 2,570
  • 11
  • 19
  • That was precisely what I was looking for. I had not realized that ctrl-p would also take me back to the original input. Thanks. – Octaviour Dec 22 '16 at 11:00
  • 2
    @Octaviour, I find ctrl-p to generally be more useful than ctrl-n, because it searches for the closest match before (instead of after) where you are. So, if you have just written a word, ctrl-p would quickly show you the same word if you need to repeat it (for example using the same variable again in the code) – Shahbaz Jan 07 '17 at 17:20
  • Yeah, I wrote ^P out of habit for exactly that reason. – Antony Jan 07 '17 at 17:23