57

Many IDEs automatically complete function names, variables, method names, etc. as the user types. The best ones complete the names based both on the language's built-in library as well as what has already been defined in other files of the same program.

For example, as I'm typing the following Python program:

hungy = True

def eatFood(food):
    pass

if hungry:
    eatF

the line eatF would automatically show eatFood() as an available auto-complete option.

Does Vim have this capability? If so, how can I enable it?

42

There are many different flavors of autocomplete in vim. One way might be to use SuperTab. This provides a way to use tab-completion at more or less any time. This would enable you to hit Tab after you've partially typed the word to get a completion list. For instance, typing eatF followed by Tab to expand to eatFood.

Please Note: these pictures all link to example gifs in action.

supertab completions

Vim has excellent other options. You should read :h ins-completion to see the variety of built-in completion options. Using vim's Ctrl-XCtrl-O, combined with a python-aware plugin like jedi can give a completion flavor. Jedi can be configured to give documentation on omni-completion (this is what Ctrl-XCtrl-O does). Then documentation for the completion candidates would appear in a split window.

ctrlxctrlo completion

Using similar plugins but different options leads to all sorts of behaviours. It's possible, for instance, to not need to prompt for autocompletion (if that's what you're after). Instead, after you type some number of letters (say, 2 or 3) of a word, a plugin can try to intelligently offer possible completions in a menu.

enter image description here

So the short answer is a yes! But the configuration process can be a bit complicated. I think of it as a step in the long stairway of mastering vim.

  • I've tried them all and I always come back to SuperTab, it's the least intrusive. – craigp Feb 4 '15 at 7:31
  • 1
    I also really like the YouCompleteMe plugin, as it suggests as-you-type and you can either choose or ignore suggestions. Yes, visually it is hard to ignore,but it is very unintrusive in my workflow because it requires fewer key strokes. – Thriveth Feb 4 '15 at 22:51
  • 3
    I've changed the animations to be "on click", in the sense that they are linked to the gif animations. – davidlowryduda Feb 5 '15 at 7:06
  • 1
    I can't use YouCompleteMe on my work computer because I don't have root to install the dependencies, SuperTab is by far the next best choice. – Charles Clayton May 25 '17 at 20:48
  • @mixedmath In my case superTab doesn't work. – alhelal Feb 15 '18 at 9:27
24

Such functionality - i.e., searching the current file (and all open files) for auto-completion, should be enabled by default with Ctrl+P:

screenshot of autocompletion in use

You can go to the next suggestion with Ctrl+N, the previous suggestion with Ctrl+P and select it by typing any letter (which will be appended right after the suggestion).

  • 1
    To the editor (Jasper): I more likely want to write again something that I have written recently, not something I'll write in the future or that is at the beginning of the file. Therefore ^P is more natural IMHO. Otherwise thanks for the edit (I hope I clicked the right button and you got your +2). – yo' Feb 3 '15 at 18:48
  • 1
    Yep, I got those imaginary internet points :) I always thought that the suggestion list would be sorted alphabetically... – Jasper Feb 3 '15 at 18:53
  • @Jasper order of appearance it is (at least for me certainly, also see above: world,word,wood and not the other way around). So ^P is the last used completion, which is very handy (I rarely go further than 3 items in the list, even in a long file). – yo' Feb 3 '15 at 18:55
  • protip: inoremap <C-Space> <C-N> – wchargin Feb 3 '15 at 22:04
20

Vim has support for completion natively. You can read about the various different completions that Vim supports at :h ins-completion.

In general, for all purposes I have found, ins-completions are enough for my liking, however there are some completion plugins that add more value beyond what ins-completions offers. NeoComplete, YCM (YouCompleteMe) are a few for the same. They are more advanced in the sense that they try to combine different types of completions more accurately, and they also have advanced caching mechanisms, so they tend to be faster. YCM even goes to the extent to work with external compilers / utilities to provide better IntelliSense.

9

Yes, auto completion scripts for vim exist. The "best" choice is depending on your programming language. As your example code is Python I suggest to take a look at Jedi. Build on top of that You complete me exists, which also has support for other languages, but is sometimes seen as too big. For other languages you can browse through the long set on vim scripts.

7

In insert mode, type the first couple of characters of a word, then press:

Ctrl-N to insert the next matching word; or
Ctrl-P to insert the previous matching word. 

This is particularly useful when entering the names of variables in a program.

The 'complete' option controls where the keywords are searched (include files, tag files, buffers, and more).

The 'completeopt' option controls how the completion occurs (for example, whether a menu is shown).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.