Search code examples
vimautocompletevim-plugin

How to efficiently undo an autocomplete in vim


Right now I'm playing with autocompletions in vim, and I've settled on supertab to handle my completions. While it's running very smoothly and (I think) exactly as it's designed, I'd like to know how to change something.

Firstly, I'm running 7.3.429 on Ubuntu 12.04LTS with

set completeopt=menuone,preview,longest

so that I have bash-type autocompletion with supertab, and default complete.

Let's suppose I have the following in my file:

aaabbbcccddd
aaabbccddeef
aaabbcddeeff

If I type aa and hit Tab, then vim realized that aaabb is the longest common string among the matches, so it inserts aaabb and displays a menu containing the three options. If I really did want one of those options, then all is good. But maybe I really wanted aaaaazzzzz, but didn't realize I hadn't typed it yet.

Is there a good way to say to vim, "Oh, I'm sorry! I didn't mean to tabcomplete after all! Please pretend I didn't."

Right now, the options apparent to me are to:

  1. Hit Tab or Shift+Tab enough times to get back to my initial. But if there are many similar words, especially up to different lengths, this is annoying.
  2. Hit backspace however many times necessary, or some other naive deletion. But these are surely unnecessary key strokes.
  3. Hit Esc+u to get an undo, but this undoes my whole word (or more if I typed quickly). This is entirely unacceptable. And afterwards, I need to re-enter insert mode and retype. Gross.
  4. Hit Ctrl+U to undo without leaving insert-mode. But this also has a tendency to remove way too much.
  5. Hit Ctrl+W to delete the last word. While I get to do this without exiting insert-mode, I still have to retype. This is the best I have found so far.

If I didn't have longest enabled, then I could use Ctrl+E, which quits the menu without inserting anything else. But since longest is on, it stops autocomplete but leaves the longest common match entered.

Surely there has to be a better way to do this.


Solution

  • There are 2 native ways to do this in vim. If you know right way that the item is not in the completion menu you can use <c-y>. <c-y> accepts the current match which if you didn't move though any completions will send you back to only the text you inserted. (Second way) However if you did move through the completion menu you can move though until you get back to the original text.

    However I imagine it isn't too hard to simply accept the longest matching and edit the word. You could also use <c-g>u to break up the undo block by working it into your <tab> mapping. Although that may break the history up more than you want.