Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vim: usher in a new era of autocompletion productivity
So, I'm using YouCompleteMe everywhere now, not just on my Mac, so I am expecting to be doing a lot more autocompletion from here on. Time to fix some long-held grievances I have with the way autocompletion is working with UltiSnips, YouCompleteMe and Supertab. Here's an example scenario. I have a snippet `log` that expands to `console.log();`, with a placeholder inside the parens. Now, say I have a file with: var fooBar = 1; and I type: log<tab> UltiSnips puts me here: console.log(|); ^ (cursor here) If I then type: console.log(fooB<tab>); I wind up here: console.log(fooB);| ^ (cursor here) When what I really want is: console.log(fooBar|); ^ (cursor here) ie. if Supertab _can_ complete something, I want it to, otherwise I want UltiSnips to get the tab. With YouCompleteMe in the picture, the experience is even worse, because you'll see the pop-up as soon as you start typing "fooB". Reflexively, you'd think you could push `<Tab>` to expand the suggestion, but you can't; you have to remember to push `<C-j>` or `<C-n>` or `<Down>` (ugh) instead. Historically, the way I've gotten YouCompleteMe and UltiSnips to play nicely is as described here: http://0x3f.org/blog/make-youcompleteme-ultisnips-compatible/ Effectively: - Override YouCompleteMe's default bindings, freeing up `<Tab>` and `<S-Tab>`; set a new completion binding like `<C-j>` or something - Install Supertab and tell it to use the completion binding that you just set up (in other words, pressing `<Tab>` will get "forwarded" to the `<C-j>` or other binding that you set up for YouCompleteMe) - Tell UltiSnips to use `<Tab>` and `<S-Tab>` to do its thing So, this works pretty darn well, except for the awkwardness described above. In this commit, we make things better with a trick similar to the one described here: ycm-core/YouCompleteMe#36 (comment) [via http://stackoverflow.com/questions/14896327/ultisnips-and-youcompleteme] Specifically: - Get rid of Supertab - Make a custom function that does the following when `<Tab>` is pressed (note, the precedence here is different than in the linked examples): - Try to expand a snippet - If that doesn't work, try to cycle through completion options (if the YouCompleteMe popup is visible) and otherwise try to jump to the next UltiSnips placeholder - If none of this works, just do a normal tab - Set up mappings for `<Tab>` and `<S-Tab>` that call this custom function; note that these have to happen inside an autocommand in order to overwrite the late-mapped default shortcuts set by YouCompleteMe (although, as I write this, I think that may not be true any more because I can select non-clashing mappings) - Get rid of `g:UltiSnipsJumpBackwardTrigger` so that it doesn't overwrite our `<S-Tab>` mapping - Set up `<CR>` to accept completion; this gives us an easy way to break out of an endless cycle when choosing YouCompleteMe completions inside an UltiSnips placeholder Some TODOs noted inline, but this basically works. It basically means: - `<Tab>` to cycle completions, `<S-Tab>` in reverse - `<Tab>` to expand snippets - `<Tab>` to jump between placeholders (for now `<S-Tab>` doesn't work here, although I think I could make it do so; `<C-k> does work) - `<Enter>` to get out of completion mode (as noted inline, may need to tap twice; not sure why, as the same happens with `<C-y>`) This last behavior is a slight deviation from how Vim normally handles autocomplete mappings (see `:h ins-completion-menu`). Basically, it sometimes inserts newlines when you hit `<Enter>` during completion. I find this annoying far more often than not, so I am happy to drop this behavior in the name of progress.
- Loading branch information
0664b62
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have the same grievances, and it looks like you solved them. This is great! I'll definitely be stealing this.
0664b62
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I consider it solved. Just make sure you look at the entire series of commits (or the final state) as there are a few subsequent tweaks/fixes.
0664b62
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are a Vim God.
I wasted a lot of time on various YCM/UltiSnips alternatives and this is the only one that worked for me. Thank you so much! 😄