Tabnine Office Hours: Learn how to get AI to write code like an engineer on your team
Home / Blog /
How to select Vim code completion for any language
//

How to select Vim code completion for any language

//
Tabnine Team /
6 minutes /
April 17, 2022

Digitalization is at the forefront of every industry. As more innovative companies compete to produce best-in-class products and services, speeding up processes becomes necessary to keep ahead of the market. Manually writing every line of code is not only an avoidably complex chore but also a time-wasting one. 

 

Thankfully, long gone are the days when developers needed to write every single line of code manually, all thanks to automation. Auto-completion tools are vital for faster, easier, and more accurate coding, and there are various plugins available for any text editor. This tutorial will teach you how to use the Vim Code Completion tool for any language. 

  

What is Vim?

Vi is the first built text editor for the Unix environment. It is an improved version of Vi editor and the name Vim stands for **V** **im**proved. Vim is a free, open-source, and cross-platform text editor available for all operating systems, released to the public in 1991. It is designed for use both from the command line and GUI.

 

VIM is available under the Vim license, which is compatible with the GNU General Public License through a particular clause that allows the distribution of modified copies under the GNU GPL version 2.0 or later.

 

Main features of Vim Text Editor

Vim supports multiple text editing functionalities by default. Some popular features include *Text auto-completion*, *Syntax highlighting*, and *Text editing using commands*. Vim also supports multiple file formats and programming languages such as Javascript, HTML, and Python. Hence, developers can use the Vim editor to write programs in different files such as `.js` and `.html` files. When writing programs using the vim editor, the syntax highlighting functionality works out of the box and highlights the keywords based on the selected file type. 

 

There are six main modes in Vim editor, and the other six modes are variants of the main modes. The Vim editor footer shows a label of the current mode you’re on. The main modes are:

 

  • Normal Mode This allows you to enter all the editor commands. This mode is the default mode when you enter the editor.
  • Visual Mode – This is similar to normal mode, but the movement command extends the highlighted area.
  • Select Mode – This mode is identical to the MS windows selection mode. When you type a printable character, it deletes the selected text and enters into the *insert* mode.
  • Insert Mode – allows you to type the text and add the typed text to the buffer.
  • Command Line Mode allows you to enter one line of text at the bottom of the text editor window. It is used for executing commands on the Vim editor. You can enter into command line mode by pressing the Esc key. Example commands are the Save file command, Exit editor command, etc. 

Do you stay updated with the latest advancements and trends in software development?

It’s essential to remain informed about new technologies and practices to excel in this constantly evolving industry. Learn how you can leverage AI to optimize your software engineering in 2023.

How to use Vim’s built-in auto-completion tool

Vim editor supports Autocomplete by default for the standard text files and enables autocomplete for programmatic files by explicit configuration.

 

Auto-complete in Normal Text File

 

First, let’s see how to use the auto-complete functionality in the standard text file. 

The default auto-complete functionality completes the words you type by checking the available terms in the buffer. To use this functionality, Type some words in the file. Next, type a few characters of the word you need to write and press `Ctrl + N` to complete the word automatically. If Vim finds only one match, it will automatically complete the term.

 

If Vim finds more matches with similar text typed, the list of words will appear. You can select the term you prefer, as shown in the screenshot below. 

 

If no similar match is found to complete the word, you’ll see an error “Pattern not Found,” as shown below. 

 

We have seen how to use the auto-complete functionality for the text files. Now, let’s check how to enable and utilize the auto-complete functionality for the specific programming languages. 

 

AutoComplete in the Program Files

 

This section will teach you how to enable auto-complete for the JavaScript file. 

 

JavaScript programs are usually written in the `.js` file.

 

First, create a .js file using the command below. 

 

“`

$vim sample.js 

“`

 

A js file will be created and opened in a normal mode. 

 

Enter into the vim command mode by pressing the *Esc* key and Enter the below command. 

 

“`

:set omnifunc=javascriptcomplete#CompleteJS

“`

 

Now press the *Enter* key. Autocomplete is enabled for Javascript. 

 

Note that auto-complete is set only for the current session. If you want to make the auto-complete permanent, you can add this command to your `.vimrc` file. 

 

Now press `i` to enter the *edit* mode and type any JavaScript keyword in the text editor and press *Ctrl + x* followed by *Ctrl + o*. Vim editor will show the possible auto-complete suggestions. 

 

For example, type the keyword `document.` and press *Ctrl + x* followed by *Ctrl + o* to see the auto-complete suggestions as below. 

 

This is how you can enable and use the auto-complete feature of Vim editor to write Javascript programs. Each time you open the Vim editor, it takes some effort to enable the auto-complete functionality. Also, the auto-complete only works for a single language during each session. You’ll learn how to overcome these barriers in the next section. 

Vim Completion Engines for any language

Vim is also highly customizable and extensible. You can customize the Vim editor by setting the configurations using the `.vimrc` file. You can also include plugins to extend the Vim functionality by using the extensive plugins available in the market. 

 

Multiple plugins are available for enabling auto-complete functionality for all languages at once. While using the advanced plugins, the suggestions are based on context rather than standardized. Let’s discover some of the auto-completion engine plugins available. 

 

YouCompleteMe

YouCompleteme is a fast and comprehensive code completion and refactoring engine available for Vim editor. It has multiple built-in completion engines, and developers can use it for any language without any specific language configuration. You can find the list of engines available in the YouCompleteMe plugin here.

 

When you use the YouCompleteMe plugin, there are no keys to be pressed to auto-complete the code. The suggestions appear as and when you type something in the editor. You can press *Tab* to accept the suggestion if you think it’s valid.

 

The auto-complete suggestions are based on the file type you’re working with because of the identifier-based engine available in YouCompleteMe. It collects all the identifiers in the current file you work with and the other files you visit to identify the appropriate suggestions. 

 

YouCompleteMe is a Python-based plugin. Also, YouCompleteMe is supported in the Vim editor available in the latest LTS version of Ubuntu. The latest LTS version is Ubuntu 20.04, which contains `vim-nox` at `v8.1.2269`. This Vim version must have Python version 3.6 runtime installed in your system. 

 

To install the plugin, read the following instructions.

 

Deoplete.nvim

Deoplete.nvim is yet another auto-completion engine available for the neovim and the Vim 8 editor. But neovim is the recommended vim editor for this plugin. You can check this article to understand the difference between neovim and vim editor. 

 

Deoplete.nvim plugin is based on Python3. It provides an asynchronous and extensible completion framework for vim editors. The main advantage of the Deoplete.nvim plugin is that it is highly configurable. Also, you need to install various sources for auto-completion of different programming languages. You can find the list of available sources here.

 

Since this plugin has more settings and different configurations for each programming language, you should use this plugin ONLY when you want to configure every setting explicitly. Otherwise, it is best to use the YouCompleteMe plugin for auto-completion. 

 

You need the Python3 installed in your system to install and use the Deoplete.nvim plugin. To install the plugin, read the following [instructions.

 

Note that this plugin is currently in maintenance mode and not developed anymore. Ddc.vim is the next-generation version of the Deoplete.nvim plugin. 

 

Coc.nvim

*Coc.nvim* is another auto-completion plugin available for Vim editor. This plugin is node.js-based. It is a fast and instant increment completion engine. It uses a `jsonc` as a configuration file similar to VS code, making it a very flexible plugin to configure. With this type of configuration, you make the Vim editor similar to the VS code, acting as a comprehensive IDE for your development activities. 

 

You can use this plugin when you prefer to work with the `nodejs` environment. You need to have nodejs installed in your system to use the Coc.nvim plugin. To install the plugin, read the following instructions.

 

Increase productivity with code completion technology

You’ve learned how to install and use the auto-completion plugin in the Vim editor. Using sophisticated plugins such as YouCompleteMe makes the developer’s work more straightforward, as it predicts the code they need to type. Tabnine is a comprehensive code completion solution that plugs into the engines mentioned in this article. It supports ALL languages on 17 IDEs and can write 30% or even more of the code for you, freeing up your time and ensuring you produce best-in-class software. And the best part is: it is 100% free. 

Tabnine for Enterprise provides a secure coding environment that allows teams and organizations to host and train their own AI models. This feature facilitates collaborative autocompletion across IDEs and enhances code security by keeping the codebase and AI model on secure corporate servers. With Tabnine for Enterprise, your development team can enjoy the benefits of powerful AI code assistance, which promotes more productive and error-free coding, all while ensuring the confidentiality and protection of your company’s data.