Skip to content

[VIM] Use "YouCompleteMe" Plugin to Auto Complete Code (Take Python as an Example)

When we developing on the remove server, many people used VIM editor to edit the code of project. Although VIM is convenient, it still has many functions less than IDE.

Today, I want to record how to configure the YouCompleteMe (YCM) plugin in the VIM environment to automatically complete the code to make VIM more convenient in remote work.

This article takes the complement of python code as an exanple.


Installation of YouCompleteMe

Step 1: Check you VIM version

Use the following command to check the VIM version:

vim --version

Output:

The VIM version needs to be at least 7.4 and above, and it needs to support python or python3. If it is not supported, you need to reinstall VIM.


Step 2: Install the VIM Plugin Manager - Vundle

You can skip this step if you have already installed it.

First, make a ~/.vim folder in your user dictionary. And use git clone to get the Vundle.

mkdir ~/.vim
mkdir ~/.vim/bundle
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim


Step 3: Add ".vimrc" configuration

If you have no ~/.vimrc file, you need to create it.

filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
call vundle#end()
filetype plugin indent on

" YouCompleteMe
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'
let g:ycm_confirm_extra_conf=0
let g:ycm_python_binary_path='/usr/bin/python3'

The path set by let g:ycm_python_binary_path should be the path of your python interpreter.


Step 4: Use Vundle to install VIM plugin

Then open VIM editor.

vim


Pressed Esc button, and key in:

:PluginInstall


After pressing Enter, you will see:


Step 5: Compile YouCompleteMe Plugin

Finally, we need to compile the YouCompleteMe Plugin using the following command:

sudo ~/.vim/bundle/YouCompleteMe/install.py --clang-completer

In this way, we have finally completed the setting of python auto-complete in VIM!


DEMO:

As you can see, when you import os module, the screen will match all function of os module and hint you. If you pressed Tab, VIM will automatically complete the function name.


References


Read More

Leave a Reply