333

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.

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
diegocstn
  • 4,447
  • 2
  • 20
  • 17

11 Answers11

530

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.

Jeff B
  • 8,572
  • 17
  • 61
  • 140
Brandon Frohbieter
  • 17,563
  • 3
  • 40
  • 62
171

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

2015 Edit: I personally use YouCompleteMe now.

michaelmichael
  • 13,755
  • 7
  • 54
  • 60
  • 13
    This is highly recommended. Takes the effort out of typing C-X C-O, C-P or C-N. – Amjith Mar 02 '11 at 17:32
  • I've tried this but it seems doesn't work.. there's probably a conflict with other scripts.. – diegocstn Mar 02 '11 at 17:43
  • 1
    are you putting the files into the specified directories in ~/.vim? – michaelmichael Mar 02 '11 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](http://stackoverflow.com/questions/4777366/recommended-vim-plugins-for-javascript-coding/4779916#4779916) I explain how I do it. And [here](http://stackoverflow.com/questions/4580695/omni-completion-does-not-contain-all-the-php-functions/4585865#4585865) is another hint for PHP. – romainl Mar 02 '11 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 02 '11 at 20:16
  • many thanks, but missing some steps in your instruction. I have read this: https://bitbucket.org/ns9tks/vim-autocomplpop/issue/22/add-php-support and I fixed the problem with php files – diegocstn Mar 02 '11 at 22:57
  • YCM conflicts with vim-snipmate as of 201708. – hhh Aug 04 '17 at 22:52
70

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

Ângelo Polotto
  • 8,463
  • 2
  • 36
  • 37
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.

ernix
  • 3,442
  • 1
  • 17
  • 23
  • what does `setl` mean? Thanks. – qed Aug 01 '13 at 09:19
  • Ok, I got it, it means setlocal. But why do we need setlocal here? – qed Aug 01 '13 at 09:21
  • Take a look at how it annoys you when you open another file in the same buffer. – ernix Aug 01 '13 at 10:05
  • @ernix Open another file in the same buffer? Do you mean Vim instance? – JakeD Jul 13 '18 at 21:54
  • @JakeD No, buffer/window/tab/instance are completely different concepts in Vim. See also https://sanctum.geek.nz/arabesque/buffers-windows-tabs/ and `:help global-local` – ernix Jul 19 '18 at 01:49
  • 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 '18 at 02: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 '18 at 03: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

Nishant
  • 20,354
  • 18
  • 69
  • 101
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.

ak17
  • 167
  • 1
  • 9
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.

sp3ctum
  • 429
  • 6
  • 9
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

Brian Kenya
  • 71
  • 1
  • 3
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.

tning
  • 1,231
  • 10
  • 22
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.

Matthieu
  • 948
  • 8
  • 8
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/

Parker Ault
  • 3,268
  • 3
  • 22
  • 24
  • 60
    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 .. – Mohamed Benkedadra Oct 02 '18 at 04: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 '19 at 20:39
  • 1
    vim does not need GUI, but the electron needs, this is why they cannot be comparable :) – Andrii Soluk Aug 08 '20 at 09: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. – Dimitri Lesnoff May 25 '23 at 14:47