Resource | Vim’s most magical autocomplete

Vim’s most magical autocomplete

One of Vim’s most incredible, built in, features is autocomplete. It’s also got some neat tricks that you can use to make your coding very efficient. This is all native Vim. No plugins here.

Let’s jump straight to the point: Vim can autocomplete a word, but it also knows the context of where it found the keyword, and can complete the next word after it.

A place I often find this used is refactoring specs. Say I have a user = ... assignment in a spec that I want to extract out into a let. I can’t autocomplete the whole line, because they start differently. I could copy the line and modify it. OR, I could do this…

Notice how the context after the create :user is all autocompleted. So fast.

The relevant Vim help

The help text for :h i_CTRL-P states,

Further use of CTRL-X CTRL-N or CTRL-X CTRL-P will copy the words following the previous expansion in other contexts unless a double CTRL-X is used.

So, the pattern is

  1. Autocomplete a word with CTRL-P or CTRL-N

  2. Complete context by mashing CTRL-X CTRL-P or CTRL-X CTRL-N

Give it a go, it wont take long to become intuitive and incredibly useful.

P.S. for the Emacs users who have yet to convert to Vim, you can check out HippieExpand.

Message sent
Message could not be sent
|