73

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?

7 Answers 7

51

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.

7
  • 1
    I've tried them all and I always come back to SuperTab, it's the least intrusive.
    – craigp
    Commented Feb 4, 2015 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
    Commented Feb 4, 2015 at 22:51
  • 3
    I've changed the animations to be "on click", in the sense that they are linked to the gif animations. Commented Feb 5, 2015 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. Commented May 25, 2017 at 20:48
  • @mixedmath In my case superTab doesn't work.
    – alhelal
    Commented Feb 15, 2018 at 9:27
38

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).

4
  • 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'
    Commented Feb 3, 2015 at 18:48
  • 1
    Yep, I got those imaginary internet points :) I always thought that the suggestion list would be sorted alphabetically...
    – Jasper
    Commented Feb 3, 2015 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'
    Commented Feb 3, 2015 at 18:55
  • protip: inoremap <C-Space> <C-N>
    – wchargin
    Commented Feb 3, 2015 at 22:04
23

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.

0
12

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).

For help on Ctrl-N and Ctrl-P, see :help compl-generic .

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.

4

CoC, the Conqueror of Completion also deserves a mention. It is very advanced, comparable to VSCode's Intellisense, with extensions tailored to each language. It works with Vim 8 and with Neovim.

If you are not using a plugin manager, then the initial setup may take a little effort: You will need node installed, then run npm run build in CoC's home folder to get it ready.

After installing, you just need to tell CoC which languages you like to use. For example:

:CocInstall coc-tsserver coc-json coc-html coc-css

:CocInstall coc-python coc-perl

You can find a list here or a list with options here.

After that you are ready to go!


There is also a lot of optional configuration you can play with. Here are some of the things I am currently trying:

    " Rename only in the current file (or maybe only in open buffers)
    nmap <leader>rr <Plug>(coc-rename)
    " Find all occurrences in project
    nnoremap <leader>prw :CocSearch <C-R>=expand("<cword>")<CR><CR>

    " The following are just some of the suggested settings from CoC's README.md {{{

    set shortmess+=c

    " Use `[g` and `]g` to navigate diagnostics
    " Use `:CocDiagnostics` to get all diagnostics of current buffer in location list.
    nmap <silent> [g <Plug>(coc-diagnostic-prev)
    nmap <silent> ]g <Plug>(coc-diagnostic-next)

    " GoTo code navigation.
    nmap <silent> gd <Plug>(coc-definition)
    nmap <silent> gy <Plug>(coc-type-definition)
    nmap <silent> gi <Plug>(coc-implementation)
    nmap <silent> gr <Plug>(coc-references)

    " Use K to show documentation in preview window.
    nnoremap <silent> gh :call <SID>show_documentation()<CR>    

    function! s:show_documentation()
        if (index(['vim','help'], &filetype) >= 0)
            execute 'h '.expand('<cword>')
        else
            call CocAction('doHover')
        endif
    endfunction

    " Highlight the symbol and its references when holding the cursor.
    autocmd CursorHold * silent call CocActionAsync('highlight')

    " Symbol renaming.
    nmap <leader>rn <Plug>(coc-rename)

I got interested in CoC after a friend shared this video:

Having said that, The Primagen seems to prefer using the Neovim LSP these days.

0

One of my favorite features of Visual Studio Code was having an autocomplete menu pop up to help complete words that were previously written in the current file or files that I had open.

The AutoComplPop plugin adds this functionality to Vim. Kindly check out this step-by-step guide by Nick.

1
  • Welcome to Vi and Vim! By default, C-n and C-p should complete words in all buffers (current first, then opened in windows, then others loaded in the buffer list, then unloaded buffers in the buffer list). See :help 'complete'
    – D. Ben Knoble
    Commented Sep 13, 2022 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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