Sunday, March 10, 2019

Installing YouCompleteMe in vim

YouCompleteMe is a nice little vi plugin transforming vim into a full-featured IDE with the power of vim on top of it. In fact, it's an auto-completion plugin supporting most of the languages and I use it for C and C++. If you follow the installation guide it may work... or not. I've spent my evening trying to figure out what was necessary to have something cute and nice to use. Alongside tmux and vim, it makes a simple terminal, an incredibly powerful IDE to program and develop. No need for a mouse, no need for a super large screen and on top of it I can move my desk every where in the world with a simple ssh connection. Much better than anything I've tried in fact... Sometimes, in life, the simplest tools are the best. Anyway, here is what I did:
  • I assume you use a Linux system like I do for your everyday tasks, not one of those bloated operating systems which name starts with a M or a W (it's my blog, so I'm allowed to give my personal preferences here).
  • First of all, I needed to enable YouCompleteMe otherwise, it just doesn't work at all:

let g:ycm_use_clangd="Never"
" required because Vundle didn't install it
set runtimepath+=~/.vim/bundle/YouCompleteMe
" required to make YCM works
let g:ycm_global_ycm_extra_conf='~/.ycm_extra_conf.py'
  • Then, I've changed colors a bit to make things not to harsh on my eyes and especially to solve the problem that errors in the source code are not readable anymore when highlighted by YouCompleteMe
" Colors
" the left column (gutter/sign column) has the same color as the background
highlight clear SignColumn
" underline only the error (default was not good)
highlight YcmErrorSection cterm=underline
" sign was too red flashy
highlight YcmErrorSign cterm=bold,reverse

And that's it. But don't forget to make a compilation database in your project's directory too. I use CMake these days and all you have to do is to add the following line in your CMakeLists.txt file:

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
It will create a file called compile_commands.json in your project directory that YouCompleteMe will use to find the proper compilation parameters to analyse your code. It's useful when you use 3rd party libraries for example.