Lazy Vim Completion
Vim has a great completion system, capable of intelligently supporting a whole range of filetypes, and overall is a great piece of the vim picture. The key feature that I was hoping to find was the ability to trigger completion and move through the list of completion options using a single key combo, preferably on the home row. What follows is my current method for doing this.
What You Get
- Ctrl-j triggers
C-x C-i
keyword completion, great for long variables & method names - Ctrl-k triggers
C-x C-o
omni completion, mostly used in css files - After triggering completion, subsequent Ctrl-j/k will cycle up and down the menu of potential completions
Note, these will override the default insert mode Ctrl-j/k key mappings, but these provide “Begin a new line” and “Enter a digraph” respectively, so I don’t mind losing them.
How to Get It
The code for each of the keymaps is a single ternary expression that checks to see if the completion menu is visible, then either opens it or cycles within it.
" Use j/k to start, then scroll through autocomplete options
inoremap <expr> <C-j> ((pumvisible())?("\<C-n>"):("\<C-x><c-i>"))
inoremap <expr> <C-k> ((pumvisible())?("\<C-p>"):("\<C-x><c-o>"))
Room for Improvement
My main gripe with how I currently have this working is that the completion menu often presents completion options in the reverse order to what I would want. It looks as though vim searches down through the current file to provide the list of potential completions, but most often I am looking for a variable from a few lines above. I am guessing I can get vim to search upward but have yet to feel a strong need to do this.