Vim + Youcompleteme Settings

Vim, as an excellent editor which have already been admitted by many coders, may be the first choice to editor C/C++ program.

Vim can do many things fluently in editing, however, vim can't do everything in coding. In many IDEs, the basic function is to complete a variable when we just enter one or two single words. Actually, vim itself have some complete function, but not so powerful. When we use STL, we may not remember every functions attributes clearly, so we need a reminder, or a completer.

Youcompleteme is an also excellent utility in completing and could be easily installed to vim. So, let's begin.

Install Vim with python support

To install youcompleteme on your vim, python support should be added to your vim. It is always lucky for mac user, you just need to install macvim with the lastest version.

Install Vundle

If you are an old vimer, you must have been worried about your numerous add-ons to your vim. It is hard for user to arrange all the add-ons manually. Vundle is a vim add-ons manager, moreover, it could help you to install add-ons.

After you installed Vundle, you can add these lines to your .vimrc

""""""""""""""""""""" Vundle
set nocompatible
filetype off


set rtp+=~/.vim/bundle/vundle
call vundle#rc()


Bundle 'gmarik/vundle'


Bundle 'Valloric/YouCompleteMe'
Bundle 'Valloric/ListToggle'
Bundle 'scrooloose/syntastic'


filetype plugin indent on
""""""""""""""""""""" Vundle

After that, your could start your vim and get into commandline mode and insert:

:source ~/.vimrc
:BundleInstall

Wait for a while, youcompleteme and syntastic are added to your vim.

Install clang-completer

You have already installed Youcompleteme, and you don't have its cpp completer yet. What you need to do is quite simple.

cd ~/.vim/bundle/YouCompleteMe
./install.sh --clang-completer

Settings

Youcompleteme works with an important setting file called .ycm_extra_conf.py. It determines many important configuration includes stl positions and completer encoding. Youcompleteme has just give you a simple one in the youcompleteme homedir.

Add STL relation

The first thing to do is to add stl dirs into the completion dirs. If you are a mac user, you can use the following direction.

echo | clang -std=c++11 -stdlib=libc++ -v -E -x c++ -

You will get many lines popped, and find such lines:

#include "..." search starts here:
#include <...> search starts here:
 /Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/bin/../lib/clang/5.0/include
 /Library/Developer/CommandLineTools/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)

Add these lines to the corresponding place in .ycm_extra_conf.py:

flags = [
'-isystem',
...
]

Change Encoding

This situation happened on my mac. When I created a cpp file and type #include <iostream>, the completion showed when I typed the first some of these letters. And when I continued to type several letters, the completion disappeared.

After I searched on google, and make such decision, the completion is the file and .ycm_extra_conf.py is not the same. So I tried this,

reload(sys)
sys.setdefaultencoding('utf-8')

I add the above lines to my .ycm_extra_conf.py file. And the lovely completion come back to me.

Some useful settings in .vimrc for Youcompleteme

These are my personal vimrc settings for ycm, please have a look.

"""""""YouCompleteMe""""""""
nmap <leader>gd :YcmDiags<CR>
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py' "default ycm conf location
let g:ycm_error_symbol = '>>'
let g:ycm_warning_symbol = '>*'
let g:ycm_confirm_extra_conf = 0 "no annoying tips on vim starting
let g:ycm_collect_identifiers_from_tags_files = 1
let g:ycm_min_num_of_chars_for_completion = 1
let g:ycm_cache_omnifunc = 0
let g:ycm_seed_identifiers_with_syntax = 1
let g:ycm_complete_in_comments = 1
let g:ycm_complete_in_strings = 1
let g:ycm_collect_identifiers_from_comments_and_strings = 0
let g:ycm_filetype_blacklist = {'tex' : 1, 'markdown' : 1, 'text' : 1, 'html' : 1}
let g:syntastic_ignore_files = [".*\.py$"] "python has its own check engine
"let g:ycm_semantic_triggers = {}
"let g:ycm_semantic_triggers.c = ['->', '.', ' ', '(', '[', '&']
set completeopt = longest,menu
autocmd InsertLeave * if pumvisible() == 0|pclose|endif

Now, your vim is already a strong cpp work environment.

blogroll

social