Chendi Xue

I am linux software engineer, currently working on Spark, Arrow, Kubernetes, Ceph, c/c++, and etc.

Using VIM as Cpp IDE

11 Nov 2019 »

Using VIM as IDE has many benefits, the reason I use VIM is because it’s free and I can compile and test on the same machine I wrote. While it also has some drawbacks, I always leave lots of typo errors inside codes, forget to add headers and didn’t quote namespaces, etc. And another drawbacks is coding style, after I added one more parameter or change function name, I need to shifts rest of codes in same line to make it align.

To fix those hassle, I chose to use google-codefmt and YouCompleteMe to make VIM a little bit smart, for now I don’t have some features like tagtree, since I feel it is quite convenient for me to use “grep” to find the codes definition. But I do added auto fold in my vimrc, so I can fold function when I don’t want to see them.


Here is all the steps to make My VIM CPP IDE done.

vundle

Vundle is a project used to install plugins and manage plugins in VIM, if you don’t have vundle installed, please [click here.

google/vim-codefmt

vim-codefmt is a tool which can help you to do codefmt by vim, two ways can be used to trigger codefmt

  1. use “:FormatCode”, vim will format your codes by detected code type.
  2. add auto format inside vimrc, and every time when you do “:w” for saving, codes will be auto formatted.

Installation of vim-codefmt is listed as below.

  1. Change your .vimrc and add below lines in the beginning.
    set nocompatible
    filetype off
    set rtp+=~/.vim/bundle/Vundle.vim
    call vundle#begin()
    Plugin 'google/vim-maktaba'
    Plugin 'google/vim-codefmt'
    Plugin 'google/vim-glaive'
    call vundle#end()
    call glaive#Install()
    " Optional: Enable codefmt's default mappings on the <Leader>= prefix.
    Glaive codefmt plugin[mappings]
    
  2. Trigger the installation
    option 1. Launch vim and run :PluginInstall
    
    option 2. To install from command line: vim +PluginInstall +qall
    
  3. now you should installed codefmt, to make it autoformatting, add below lines in your vimrc.
    augroup autoformat_settings
      autocmd FileType bzl AutoFormatBuffer buildifier
      autocmd FileType c,cpp,proto,javascript AutoFormatBuffer clang-format
      autocmd FileType dart AutoFormatBuffer dartfmt
      autocmd FileType go AutoFormatBuffer gofmt
      autocmd FileType gn AutoFormatBuffer gn
      autocmd FileType html,css,sass,scss,less,json AutoFormatBuffer js-beautify
      autocmd FileType java AutoFormatBuffer google-java-format
      autocmd FileType python AutoFormatBuffer yapf
      " Alternative: autocmd FileType python AutoFormatBuffer autopep8
      autocmd FileType vue AutoFormatBuffer prettier
    augroup END
    
  4. now open any unformatted codes of yours, and use “:w”, you should see codes are shifted.

YouCompleteMe

YouCompelteMe is a tool to use your Makefile or CMakefile to understand your codes and then provides auto filling advices and diagnose codes error while you writing codes.

Linux Must Install Full installation Guide

CheckList:

  • install python-devel and python3-devel package
  • install YouCompeleteMe through Vundle
    vim ~/.vimrc
    set nocompatible
    filetype off
    set rtp+=~/.vim/bundle/Vundle.vim
    call vundle#begin()
    Plugin 'Valloric/YouCompleteMe'
    call vundle#end()
    :wq
    Launch vim and run :PluginInstall
    
  • Install Clangd compeleter to YouCompleteMe
    cd ~/.vim/bundle/YouCompleteMe
    python3 install.py --clangd-completer
    
  • Compile ycm_core
    cd ~
    mkdir ycm_build
    cd ycm_build
    cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
    cmake --build . --target ycm_core --config Release
    
  • Enable C-family Semantic Completion I used Makefile for now, and use compiledb to generate compilation database
    pip install compiledb
    cd ${CPP project}
    compiledb -n make
    # now you should see a file called "compile_commands.json" in your root dir.
    

Now, YouCompleteMe should be working for your CPP codes, let’s have a try open one of your cpp code with vim

:YcmDiags

some infomation will be shown at bottom, if there is no error, it would appear to be “No warinings or errors detected.”

Other vimrc settings

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=2
" when indenting with '>', use 4 spaces width
set shiftwidth=2
" On pressing tab, insert 4 spaces
set expandtab
set foldmethod=indent
set foldnestmax=10
set nofoldenable
set foldlevel=2
" set textwidth=90
set colorcolumn=90
set number
set foldmethod=syntax
hi Folded ctermbg=black
map <C-]> :YcmCompleter GoToImprecise<CR>
let g:ycm_enable_diagnostic_signs = 1
highlight YcmErrorLine ctermfg=White cterm=bold