Autocompletion in Vim
Asked Answered
M

11

337

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.

Moraceous answered 2/3, 2011 at 15:25 Comment(0)
F
172

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

2015 Edit: I personally use YouCompleteMe now.

Frazil answered 2/3, 2011 at 16:2 Comment(7)
This is highly recommended. Takes the effort out of typing C-X C-O, C-P or C-N.Clower
I've tried this but it seems doesn't work.. there's probably a conflict with other scripts..Moraceous
are you putting the files into the specified directories in ~/.vim?Frazil
@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.Electrostriction
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 thanksMoraceous
many thanks, but missing some steps in your instruction. I have read this: bitbucket.org/ns9tks/vim-autocomplpop/issue/22/add-php-support and I fixed the problem with php filesMoraceous
YCM conflicts with vim-snipmate as of 201708.Wreath
H
540

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.

Hoskinson answered 2/3, 2011 at 15:29 Comment(8)
thanks this is great, but how can I have this list in automatic without pressing <ctrl+N>?Moraceous
That's what michaelmichael was telling you with the plugin he mentioned. Also, you might look at SuperTab.Barranca
For CSS support, add the dash for built in autocomplete by adding 'set iskeyword+=-' to your .vimrcGide
This is the most comprehensive and native way of doing it :-)Indebted
can I change Ctrl-N to something else, such as ctrl-space?Phillipphillipe
Omg, why it is so highly upvoted. The «C-n» is neither semantic, nor even «auto» (i.e. it isn't autopopups, you have to manually press the combo every time).Schurman
@Schurman the 'auto' part is that it automatically determines options for completion of a word, and automatically competes the word upon selection of an option.Icaria
oh wow, years of using vim and never knew that!Horrible
F
172

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

2015 Edit: I personally use YouCompleteMe now.

Frazil answered 2/3, 2011 at 16:2 Comment(7)
This is highly recommended. Takes the effort out of typing C-X C-O, C-P or C-N.Clower
I've tried this but it seems doesn't work.. there's probably a conflict with other scripts..Moraceous
are you putting the files into the specified directories in ~/.vim?Frazil
@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.Electrostriction
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 thanksMoraceous
many thanks, but missing some steps in your instruction. I have read this: bitbucket.org/ns9tks/vim-autocomplpop/issue/22/add-php-support and I fixed the problem with php filesMoraceous
YCM conflicts with vim-snipmate as of 201708.Wreath
P
73

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

Parasiticide answered 8/3, 2019 at 18:50 Comment(3)
Is there a way i could make them pop up without pressing ctrl+p ? i.e i want it to keep suggesting me as along with my typingBechtold
Maybe this can help you: #527358Valse
ctrl +n/p will work in earlier versions as wellCandycecandystriped
E
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.

Eyler answered 6/5, 2013 at 14:10 Comment(7)
what does setl mean? Thanks.Venue
Ok, I got it, it means setlocal. But why do we need setlocal here?Venue
Take a look at how it annoys you when you open another file in the same buffer.Eyler
@Eyler Open another file in the same buffer? Do you mean Vim instance?Ewen
@Ewen No, buffer/window/tab/instance are completely different concepts in Vim. See also sanctum.geek.nz/arabesque/buffers-windows-tabs and :help global-localEyler
@Eyler 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.Ewen
@Ewen 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.Eyler
D
10

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

Delude answered 26/6, 2014 at 18:27 Comment(0)
V
7

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.

Validate answered 4/4, 2020 at 15:30 Comment(0)
N
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.

Naturalism answered 2/5, 2012 at 18:49 Comment(0)
D
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

Denominational answered 23/5, 2014 at 10:43 Comment(0)
L
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.

Letty answered 23/12, 2015 at 15:46 Comment(0)
M
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.

Mcgowen answered 4/12, 2017 at 13:5 Comment(0)
A
-1

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/

Aspergillum answered 25/3, 2018 at 2:2 Comment(5)
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 ..Germanium
@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.Dipsomaniac
vim does not need GUI, but the electron needs, this is why they cannot be comparable :)Restrainer
@Dipsomaniac 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.Assignat
FYI OniVim is deadInsufficient

© 2022 - 2024 — McMap. All rights reserved.