One of the longstanding items on the to-do list for my Neovim configuration is to get some kind of autocomplete working. I’ve seen how effective this kind of feature can be for my colleagues who use IntelliJ, but I’ve yet to get something that works the way that I’d like. I’ve tried using Copilot and will probably return to it, but I remain dissatisfied with the operation of the actual plugins.
Part of the problem is that I don’t understand Vim’s native mechanisms for “suggestions about what to type next.” The most basic of these is a feature called “completions.” If you have also been somewhat confused by this feature — fitfully using plugins without really understanding what they’re doing — I hope that this quick write up can help.
Completion refers to a Vim feature in Insert mode. The Neovim docs describe like this:
You type the first part of a word, press
CTRL-P
, and Vim guesses the rest.
If there’s only one match, Vim will go ahead and insert it for you.
If there’s more than one match, a little picker will pop up and you can cycle through the options with CTRL-P
(“p” for “previous”) and CTRL-N
(“n” for “next.”)
How does Vim know what words to suggest?
By default, Vim constructs a list of possible matches the current file, files in other windows, files in other open buffers, tag files, and all files “#included” by the current file. (That last probably only works by default for C files.)
If you know what source you want to use there are special commands to complete with that source in particular. For instance you can use CTRL-X TRL-F
to complete filenames. If you’ve provided Vim with a dictionary, you can use CTRL-X CTRL-K
to complete words from it.
Omnicompletion
Vim also has a feature specifically for doing code completions specifically, which it calls “omnicompletion.” This is triggered with CTRL-X
CTRL-O.
Especially for languages that aren’t C this probably requires some setup to actually work — I need to do more research in this area. I mention it mainly because this is why you’ll see the word “Omni” in completion plugins.
Which brings us to… completion plugins
Completion plugins, like completion-nvim, typically do two things.
First, they add more possibilities to the list of completions. A lot of them these days are getting those completions from the language server somehow. I think this is why they tend to require some setup with autocommands — they need a way to dump their completions into a location that the Vim completion system will check.
They also often tweak the basic completion interface, just to make it clearer what’s actually happening when you hit CTRL-P,
or make it look more similar to systems like IntelliJ’s.
Snippets?
There’s also something called “snippets” that I think are a cross-editor concept that Neovim doesn’t have built-in, but that are provided by various plugins. “Snippets” are similar to completions in that they are bits of text the editor can automatically paste in for you. Unlike completions, they’re defined as a combination of piece of text to be added and the shorter word or symbol that should be expanded into the snippet.