Vim Tip #31: Completion - Much More Than Ctrl-P

2020-08-05(Wed)

tags: Vim

Vim Tips

If you're a (Neo)Vim user and you're not familiar with Ctrl-p, well, Hallelujah, you just learned one of Vim's best features. In INSERT mode, type a part of a word and press Ctrl-p and Vim will attempt to complete the word by matching any words that exist in all your open buffers. This is, as you'll quickly find, incredibly useful for matching method or variable names when you're programming, and long words or titles when you're writing. If that's all you ever learn about Vim's completion modes, you're doing okay. However, Vim has several other completion modes and some of them are worth learning too.

I should first admit that this is cribbed directly from Drew Neil's superb Practical Vim - I've tried to read several books about Vim over the years, but this is the first that I've got through.

Ctrl-ngeneric keywords
Ctrl-x Ctrl-ncurrent buffer keywords
Ctrl-x Ctrl-iincluded file keywords
Ctrl-x Ctrl-]tags file keywords
Ctrl-x Ctrl-kdictionary lookup
Ctrl-x Ctrl-lwhole line completion
Ctrl-x Ctrl-ffilename completion
Ctrl-x Ctrl-oOmni-completion

I was already aware of Ctrl-x Ctrl-f - that's a beautiful thing, to be able to complete filenames from your directory structure. And I immediately tested dictionary completion Ctrl-x Ctrl-k for the word "Hallelujah" which I've always had trouble remembering how to spell.

Which of these are useful to you will depend on how you use Vim: it's very rare for me to need a method from an included file (although that may change) so I'm likely to forget Ctrl-x Ctrl-i, but the filename completion has already glued itself into my head. Try them all out and see which is most useful to you.

For further reading on the topic, open vim and type :help ins-completion.