Vim text editor supports autocompletion for the standard text files by default. Also, when configured properly, Vim enables an autocomplete feature for files with code in the languages it recognizes.
In this tutorial, we’ll see how the autocompletion support in Vim works.
2. Built-in Support
Of course, recent versions of the Vim editor come with a built-in autocomplete feature. Vim completes words by checking the available terms currently in the buffer. Importantly, this functionality is case-sensitive.
First, we’ll see how to use the autocomplete feature in a standard text file.
For this purpose, let’s open an example .vimrc configuration file. Next, we’ll enter a few characters of a word we need to write and press Ctrl-N to trigger autocompletion.
If Vim finds only one match, it will automatically complete the term. However, if Vim finds more than one match for the text, a list of words will appear:
We can select the preferable term from the list.
Also, we can use Ctrl-P to make the list cycle backward. Alternatively, we can use the arrow keys to move up and down the list. If the program finds no match to complete a word, we’ll see an error stating Pattern not Found:
Also, to see Vim’s documentation on the autocomplete functionality, we can use :help i_CTRL-N.
Omnicompletion provides an intelligent autocompletion feature for programs in Vim. When called, Omnicompletion examines the text before the cursor to predict the intended word.
Ordinarily, Omnicompletion is not available by default. Now, to enable Omnicompletion features on Vim, let’s add the following to our ~/.vimrc:
" Enable plugins and load plugin for the detected file type. filetype plugin on " Enable Omnicomplete features set omnifunc=syntaxcomplete#Complete
Here, the double quotation marks begin comment lines. Below each comment is the line with the described function.
Finally, we can press Ctrl-X and Ctrl-O to start Omnicompletion. To check whether Omnicompletion is working, we can use the command :echo &omnifunc in a file.
Similarly, the following commands work for HTML, CSS, and PHP, respectively:
- :set omnifunc=htmlcomplete#CompleteTags
- :set omnifunc=csscomplete#CompleteCSS
- :set omnifunc=phpcomplete#CompletePHP
Furthermore, Vim autocompletion has some excellent substitutes. The command :h ins-completion shows the variety of built-in completion options on our system.
Generally, plugins add extra functionality to any program. Thus, we can add plugins to Vim to extend its auto-completion features. Notably, we can employ plugin managers like vim-plug and vundle to ease the plugin installation process.
Following, let’s look at some autocompletion plugins for vim and their unique features.
SuperTab is a Vim autocompletion plugin that allows the use of Tab for all insert completions. The plugin enables us to hit Tab after a partial entry to get an autocompletion functionality.
For instance, we might want to type “editors”. So, we enter “e” and press Tab. The plugin shows a keyword completion list. Then we can select the desired word from the list.
The SuperTab plugin is similar to the built-in Omnicompletion alternative. However, there are a few differences, as we can:
- configure it to suit our needs
- configure different keys to trigger the completion
- get a completion result when Omnicompletion returns none
To install and set up SuperTab, let’s create a directory for the installation:
$ mkdir -p ~/.vim/pack/plugins/start
Next, let’s clone the SuperTab repository via git:
$ git clone --depth=1 https://github.com/ervandew/supertab.git ~/.vim/pack/plugins/start/supertab
Further, we’ll add packloadall to our ~/.vimrc file:
" Enable packloadall for pack plugins. packloadall
Finally, we can get to the SuperTab documentation from within Vim using the :help supertab command.
Next, Jedi-Vim is a Python autocompletion plugin within Vim. Basically, it’s a Vim binding to the autocompletion library Jedi. Uniquely, Jedi-Vim stands out with its broad support for most of Python’s core features.
We can use Vundle to install Jedi-Vim. To illustrate, let’s add the plugin to our configuration file:
Next, we’ll save and source the file. After that, we run the :PluginInstall command in Vim. Importantly, to use Jedi-Vim while coding, we press Ctrl-Space.
As with other plugins, we can access the Jedi-Vim documentation in vim with the :help jedi-vim command.
YouCompleteMe is a fast and intelligent suggest-as-you-type code-completion and refactoring engine for Vim. It has several completion engines built in. Hence, YouCompleteMe can work with almost any language.
Indeed, the YouCompleteMe plugin is unique and has several advantages over other autocompletion plugins:
- works with any programing language
- we don’t have to press any keyboard combination to trigger the autocompletion
- the filtering is not based on the input being a string prefix of the completion (but that works too)
Similar to other plugins, we use Tab to go through any valid suggestions.
$ sudo apt install build-essential cmake python3-dev
$ sudo apt install mono-complete golang nodejs default-jdk npm
After having the necessary packages, we follow the installation guide to set up and compile YouCompleteMe.
In conclusion, autocompletion is a great feature in the Vim editor. We have seen how to use the built-in autocompletion features in Vim but also learned about plugins with the same function.