334

I'm having trouble with autocompletion. How can I get a code suggestion while I'm typing?

I usually develop in PHP, Ruby, HTML, C and CSS.

0

11 Answers 11

533

Use Ctrl-N to get a list of word suggestions while in insert mode. Type :help i_CTRL-N to see Vim's documentation on this functionality.

Here is an example of importing the Python dictionary into Vim.

8
  • 19
    thanks this is great, but how can I have this list in automatic without pressing <ctrl+N>?
    – diegocstn
    Mar 2, 2011 at 15:38
  • 6
    That's what michaelmichael was telling you with the plugin he mentioned. Also, you might look at SuperTab. Mar 2, 2011 at 17:23
  • 5
    For CSS support, add the dash for built in autocomplete by adding 'set iskeyword+=-' to your .vimrc
    – mahalie
    Jan 17, 2012 at 20:54
  • 3
    This is the most comprehensive and native way of doing it :-)
    – Srikant
    Jun 25, 2014 at 19:21
  • 6
    @Hi-Angel the 'auto' part is that it automatically determines options for completion of a word, and automatically competes the word upon selection of an option.
    – CJBS
    Oct 27, 2015 at 21:06
171

You can use a plugin like AutoComplPop to get automatic code completion as you type.

2015 Edit: I personally use YouCompleteMe now.

7
  • 13
    This is highly recommended. Takes the effort out of typing C-X C-O, C-P or C-N.
    – Amjith
    Mar 2, 2011 at 17:32
  • I've tried this but it seems doesn't work.. there's probably a conflict with other scripts..
    – diegocstn
    Mar 2, 2011 at 17:43
  • 1
    are you putting the files into the specified directories in ~/.vim? Mar 2, 2011 at 18:00
  • 1
    @dieguitoweb ACP doesn't work with every filetypes by default. You have to "hack" it to make it work with JavaScript, ActionScript and PHP. Here I explain how I do it. And here is another hint for PHP.
    – romainl
    Mar 2, 2011 at 20:07
  • now it works :) all the files were already in the right directories.. I only replaced the script with a new download from the git repository and now it's fine. many thanks
    – diegocstn
    Mar 2, 2011 at 20:16
71

If you are using VIM version 8+, just type Ctrl + n or Ctrl + p.

3
16

You can start from built-in omnifunc setting.

Just put:

filetype plugin on
au FileType php setl ofu=phpcomplete#CompletePHP
au FileType ruby,eruby setl ofu=rubycomplete#Complete
au FileType html,xhtml setl ofu=htmlcomplete#CompleteTags
au FileType c setl ofu=ccomplete#CompleteCpp
au FileType css setl ofu=csscomplete#CompleteCSS

on the bottom of your .vimrc, then type <Ctrl-X><Ctrl-O> in insert mode.

I always rely on this CSS completion.

7
  • what does setl mean? Thanks.
    – qed
    Aug 1, 2013 at 9:19
  • Ok, I got it, it means setlocal. But why do we need setlocal here?
    – qed
    Aug 1, 2013 at 9:21
  • Take a look at how it annoys you when you open another file in the same buffer.
    – ernix
    Aug 1, 2013 at 10:05
  • 3
    @ernix From your link: A buffer in Vim is...an open instance of a file... I know about buffers, windows, and tabs; but I don't know about opening another file in the same buffer. It seems to me that when you open a file it will always open in a new buffer and add that buffer to the buffer list. I know there are other ways buffers are used, such as the quick fix list, but I didn't know about changing what file is in a buffer. :h buffers says: A buffer is a file loaded into memory for editing. That said, I've only been seriously using Vim for 3 mos. and am certainly open to correction.
    – JakeD
    Jul 19, 2018 at 2:28
  • 1
    @JakeD Ah yes. You're right. I took "instance" meaning the wrong way. I misunderstood you were talking about processes with different PIDs. Sorry, just forget about what I've said.
    – ernix
    Jul 19, 2018 at 3:00
9

There is also https://github.com/Valloric/YouCompleteMe and it includes things like Jedi and also has fuzzy match. So far I found YCM to be the fastest among what I have tried.

Edit: There also exists some new ones like https://github.com/maralla/completor.vim

0
6

Another option is coc.nvim.

It's really fast and the completion is great as it uses intellisense the same autocompletion as VScode has. It also has linting capabilities. So it shows you were you might have a bug. It supports a multitude of languages.

It might take a bit to set up and configure but I think it is the best autocompletion engine for vim out there.

3

I've used neocomplcache for about half a year. It is a plugin that collects a cache of words in all your buffers and then provides them for you to auto-complete with.

There is an array of screenshots on the project page in the previous link. Neocomplcache also has a ton of configuration options, of which there are basic examples on the project page as well.

If you need more depth, you can look at the relevant section in my vimrc - just search for the word neocomplcache.

0
1

Here is link! for PHP.

press the Ctrl + x followed by Ctrl + o keys while writing some PHP functions.

Thanks to Oseems Solutions for the tutorial

1

If you only wanna auto-completion from cache of your current buffers, supertab is easier to install than neocomplete, can work on Mac pre-installed vim out of box without the need of MacVim.

You can check other alternatives at vim awesome.

0

For PHP, Padawan with Deoplete are great solutions for having a robust PHP autocompletion in Neovim. I tried a lot of things and Padawan work like a charm!

For Vim you can use Neocomplete instead of Deoplete.

I wrote an article how to make a Vim PHP IDE if somebody is interested. Of course Padawan is part of it.

0

I recently discovered a project called OniVim, which is an electron-based front-end for NeoVim that comes with very nice autocomplete for several languages out of the box, and since it's basically just a wrapper around NeoVim, you have the full power of vim at your disposal if the GUI doesn't meet your needs. It's still in early development, but it is rapidly improving and there is a really active community around it. I have been using vim for over 10 years and started giving Oni a test drive a few weeks ago, and while it does have some bugs here and there it hasn't gotten in my way. I would strongly recommend it to new vim users who are still getting their vim-fingers!

enter image description here

OniVim: https://www.onivim.io/

5
  • 61
    to me the entire point of vim is using the terminal ... if i wanted to use an electron app i'll just use vscode with a vim extension .. Oct 2, 2018 at 4:31
  • 3
    @MohamedBenkedadra But would that really get you the full power of Vim? I happen to be a huge fan of its search-and-replace pattern system, for one thing, and I'd be pleasantly surprised if a vim-emulation plugin supported that.
    – SilverWolf
    Mar 20, 2019 at 20:39
  • 1
    vim does not need GUI, but the electron needs, this is why they cannot be comparable :) Aug 8, 2020 at 9:26
  • @SilverWolf but does an electron app gives you the full power of Vim ? It is not lightweight and thus can not be run through a SSH connection. May 25 at 14:47
  • FYI OniVim is dead
    – adamency
    Oct 14 at 15:20

Your Answer

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

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