Autocompletion in Vim - vim

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.

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.

You can use a plugin like AutoComplPop to get automatic code completion as you type.
2015 Edit: I personally use YouCompleteMe now.

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

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.

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

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.

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.

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

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.

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.

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!
OniVim: https://www.onivim.io/

Related

How can I use vim plugins with Ideavim?

I would like to use Tim Pope's vim surround plugin in my Pycharm IDE. I've been using the IdeaVim plugin for Pycharm to use vim motions and commands.
I know I can use ~/.ideavimrc like my normal .vimrc but I cannot find
information about how to use plugins with ideavim.
Can I specify the plugins directory inside my ~/.ideavimrc or do I have to go another way? Can I use a plugin manager like pathogen?
The latest version of IdeaVim includes the vim-surround plugin. Enable it by adding
set surround
to your .ideavimrc file
https://github.com/JetBrains/ideavim#emulated-vim-plugins
Most applications only emulate Vim's / vi key bindings (and often only the basic navigation and editing commands). That goes a long way to helping vi users edit comfortably, but it isn't the real thing.
Unfortunately, to be able to use Vim plugins, you'll need the full Vimscript interpreter and infrastructure around 'runtimepath'. I'm not aware of any application that provides this, and because of the complexities and idiosyncrasies of Vim, this would be very hard indeed.
To get more of Vim's special capabilities into your IDE, use both concurrently; it is very easy to set up a external tool in your IDE that launches Vim with the current file (and position). Automatic reloading of changes allows you to edit source code in both concurrently.
If it's any comfort to you, the same applies to Emacs / Elisp as well.
The ideaVim plugin added "Support for vim-surround commands ys, cs, ds, S, enable it with set surround in your ~/.ideavimrc" since version 0.46.
No. Vote for VIM-506 for Vim scripts support (unlikely to be implemented) and for VIM-769 for vim-surround emulation (likely to appear in future versions).
This was mentioned in a changelog relatively recently.
https://github.com/JetBrains/ideavim/blob/master/CHANGES.md#features-5
I didn't mange this work. Looks like I need vim-plug installed and I use another package manager in my NeoVim and also I don't want to mix up two configs. It should work in general.

Vim: good pascal support (syntax, indent, probably omnicomplete)

I need to work on existing freepascal project ( it's an open-source project double commander ), so I'm trying to set Vim for editing source files, but pascal support bundled with Vim is surprisingly ugly.
More, I can't even find anything for better pascal support. At least, I really need for working syntax and indent scripts (both of them are buggy by default).
It would be very nice to set up omnicompletion too, but it seems this is completely hopeless.
I will probably hack on syntax and indent scripts myself, but I can't believe we still don't have them, so I decided to ask first.
Perhaps you can improve the situation by telling Vim to use the "delphi" mode instead of the "pascal" mode (this trick works with Emacs too). The reason for this is that in general "Pascal" refers to the old language designed by Wirth in the '70, while Free Pascal incorporates many syntactical additions introduced by Turbo Pascal and Delphi in the following years. Most of the development on Vim/Emacs modes usually goes in the Delphi mode, not the Pascal one.
Have a look at http://www.vim.org/scripts/script.php?script_id=3078. This will not provide autocompletion, but at least it will highlight keywords like "unit", "interface", "implementation", etc., as well as // comments, correctly.
As VIM 7.4.52 for Linux there isn't 'Delphi' option. So far, the 'Pascal' mode works good.
for a freepascal project (fpc), you need to get fpc.vim.
Doing it with 'Plug'
open your .vimrc file and add:
call plug#begin('~/.vim/plugged')
Plug 'vim-scripts/fpc.vim'
call plug#end()
reopen your vim, and use the command :PlugInstall
Highlighting
in order to highlight pascal use command:
:set filetype=pascal
this also enables indent for the code. and it will auto indent the existing code.
or you can use:
:set syntax=pascal
this may fail to indent.

How to create your own TComment syntax in vim

Is there a way to make your own comment syntax in TComment because I'm currently studying laravel
and its native templating which is called blade has a syntax for comment which looks like this
{{--Hello i'm a comment--}}
I would like to be able to toggle with this particular comment syntax when i'm editing a file which has an extension of .blade.php
Thanks in advance. If there's a native vim way it would help also. :)
Update:
I'm skimming through the help page of Tcomment and i've stumbled upon this function
tcomment#Comment(beg, end, ...), but i don't know how to implement it since i haven't dealt with vimscript yet. Even a small snippet of how this command is implemented could help
If the other suggestion (using an ftplugin, which is preferable since it provides info for other vim features) doesn't work, you could do (in .vimrc):
call tcomment#DefineType('blade', '{{--%s--}}')
You'd then have to find a way to set the filetype to blade, e.g. (in ~/.vim/filetype.vim)
au BufNewFile,BufRead *.blade.php setf blade
Here is a Solution that works for me:
I create a syntax file for the exotic programming language (my language called HRDT).
If I open a file called .script vim change the file type to HRDT.
My .vimrc contains is line:
autocmd FileType hrdt set commentstring=\\%s
This line automatically change the commentstring from standard *some Text*\ to \some Text .
For commenting I use the very famous NerdCommenter.
It might be that all you need to do is set 'commentstring'. In your case:
setlocal commentstring={{--%s--}}
This would probably go in a filetype plugin/ftplugin.

Latex and Vim usage

How can I use Latex effectively in VIM?
Is there a way to configure compile errors by highlighting the line in vim?
I have syntax highlight. What are other recommended add-ons? Is a makefile the recommended way to compile a latex file to pdf?
TexWorks lets you open and replace the opened pdf everytime it's recompiled. Is there a plugin to do something similar in vim?
I've just begun playing around with LaTeX-Box. It seems like a good plugin. I, also used VIM-LaTeX for a while, but I didn't really like the key mappings, and it seemed a bit to heavyweight as Jeet described.
I like LaTeX-Box so far because it used latexmk to compile, which is what I was using anyway. Latexmk will sit in the background and watch your .tex file for changes, and then automatically compile for you. And if you use a pdf viewer which refreshed changes (such as evince on Linux) you can see updates every time you change. Adding
let g:LatexBox_latexmk_options = "-pvc -pdfps"
to my .vimrc got latexmk working properly. You also need the latexmk script somewhere on you PATH. The key mapping to start latexmk is the same as Vim-Latex's compile: '\ll' (that's lowercase LL).
I also use SuperTab plugin for completions, which is great. And I took the dictionary files from Vim-LaTeX so I have a ton of auto completion words to use. This dictionary file is: ftplugin/latex-suite/dictionaries/dictionary in the vim-latex files. What I did was copy this file into ~/.vim/dictionaries/ and renamed it 'tex' then I added these lines to my .vimrc file:
set filetype on
au FileType * exec("setlocal dictionary+=".$HOME."/.vim/dictionaries/".expand('<amatch>'))
set complete+=k
Then if I type the beginning of a latex command and hit 'tab' I will get a list of completions. Pretty handy. BTW that 'au' command in the vimrc will also load dictionaries for any other filetypes if you want. A useful trick.
check out vim latex
If you use vim latex put the following in your .vimrc:
let g:Tex_DefaultTargetFormat='pdf'
and it should compile to pdf by default. (I think the default compilation key
is \ll).
You can also check AutomaticLatexPlugin, it has many nice features (see the features list).
Its main point is to compile the document in the background using autocommands, so that you are free from compilation cycle. This works nicely on Linux and MacOs. It contains (extended version of) Latex-Box.
vim-latex is great. But I found it too heavyweight for my tastes. I prefer more of a "Vim with LaTeX compile & view" approach, rather than "A LaTeX IDE with Vim key-bindings". So I rolled my own: 'TeX-PDF: Lightweight "stay-out-of-your-way" TeX-to-PDF development support'.
Also check out: "LaTeX Help : Help for LaTeX in vim.help format" for calling up help of LaTeX from within Vim.
I personally can get by on:
autocmd BufNewFile,BufRead *.tex set makeprg=pdflatex\ %\ &&\ open\ %:r.pdf
where open is Mac OS X specific. Linux users will want a different command to view their compiled file after running make. This works best if you have mapped a key to write and then run make (and you should - once you have single key save and compile, you'll never go back).

How do you use indent in vim for web development?

I'm starting to use Linux and Vim at work. I'm started reading vims documentation and creating my own .vimrc file and such.
I'm a web developer working with HTML, XML, CSS, JS, Python, PHP, ZPT, DTML and SQL.
I would like to have an indent feature like this one: for each language/set, a corresponding indent solution.
So, in js, writing function test(){|} would turn in
function test(){
|
}
If php, writing <?php function test(){|}:
<?php
function test(){
|
}
?>
...and such. Writing a function definition in Python, and then creating a for loop sentece, it would automatically create an indent.
I'm starting with autoindent, smartindent, cindent but I'm a little confused about their differences.
How do the indent in vim works? Am I supposed to download plugins for each language? Is the behavior I described possible with already existing plugins you're used to or do I have to create it?
I keep seeing people using Vim and I'm trying to do this as well since the machine I'm using is too limited, but I'm afraid I won't be able to have a decent auto indenting solution in it. And I really think that having to manually indent code all the time (instead of sometimes only) is a waste of time and it's against vim's "MOTTO" I've seen called "productivity".
(I have used autoindenting in a little small project in Visual Studio, and really liked their approach. Is there a plugin for that?)
Vim is usually pretty smart about indenting once you define the correct settings for tab size and the like. (Edit: As Igor mentions in the other answer, be sure to turn on filetype-specific indenting.) It seems that you want vim to automatically insert newlines though, which I don't think it can do without a plugin.
However, I think you may want to look at snipMate, which is a plugin that defines a large number of 'snippets' for different programming languages, and you can also define your own. It's basically a kind of improved tab-completion:
One example:
php<tab>
turns into
<?php
|
?>
With | being your cursor. Some snippets even define multiple cursor-positions which you can switch to with another press of tab.
vim usually comes with a bunch of syntax plugins for different languages. if you want to use those for indenting, you will need:
set autoindent
filetype indent on
you might also need syntax on but i'm not sure if that's needed for indenting. couldn't hurt though...
I found the setup for this based on a blog post:
set autoindent
inoremap {<CR> {<CR>}<Esc>O<Tab>
Having this with snipmate.vim and autoclose.vim is working flawlessly.

Resources