How to Customize Your VIM From Zero to C/C++ (Ubuntu Cinnamon)

  6 mins read  

How to Customize Your VIM From Zero to C/C++ (Ubuntu Cinnamon)

In these article we install plugins for:

The procedures were performed on Ubuntu Cinnamon

All settings


01. Update your system

sudo apt update && \
  sudo apt upgrade -y && \
  sudo apt clean && \
  sudo apt autoclean && \
  sudo apt autoremove -y

02. Install Vim, curl and Git

sudo apt install vim curl git wget

03. Install the Vim Plug

curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Add to ~/.vimrc

The plugin https://github.com/terroo/vim-simple-emoji will be used to test if everything is working.

call plug#begin('~/.vim/plugged')
" Here will be only plugins

" Sample initial plugin
Plug 'terroo/vim-simple-emoji'

call plug#end()

Close ~/.vimrc and open it again and then run:

:PlugInstall

Test to see if it’s working:

vim test.md

Type :smile: and give space then, if the face appears: 😃 it’s because everything is right! Or run :ShowEmoji.


04. Install fonts

git clone https://github.com/terroo/fonts
cd fonts
mv fonts ~/.local/share/fonts
cd && rm -rf fonts
fc-cache -fv

05. Install a vim color theme

https://github.com/Matsuuu/pinkmare

Plug 'matsuuu/pinkmare'

colorscheme pinkmare " OPTIONAL: hi! MatchParen cterm=NONE,bold gui=NONE,bold guibg=NONE guifg=#FFFF00


06. Additional and optional lines of my preference

set nu!
set mouse=a
set title
set cursorline
set encoding=utf-8 " Important to YCM

07. Interesting mappings

map q :quit<CR> " for quit only type q on command mode

" for save using Ctrl + s on command mode
" Need run this command: echo 'stty -ixon' >> ~/.bashrc && exec $SHELL
map <C-s> :write<CR> 

08. Install YouCompleMe

https://github.com/ycm-core/YouCompleteMe

  • Install dependencies
sudo apt install gcc g++ cmake make build-essential vim-nox python3-dev
  • Add to your .vimrc: Plug 'ycm-core/YouCompleteMe'
  • Complete the installation:
cd .vim/plugged/YouCompleteMe/
python3 install.py --clangd-completer # Only C/C++
# python3 install.py --all # Need Go and NPM:
# sudo apt install golang npm
  • Save this file: .ycm_extra_conf.py in ~/.vim/.ycm_extra_conf.py
  • Add this for others libs:

    vim ~/vim/.ycm_extra_conf.py

'-I/usr/include/gtkmm-3.0',
'-I./',
'-I/usr/include'

09. Settings to YouCompleteMe

vim test.cpp

I came testing.cpp

  • By typing #include <iostream> and #include <vector> it will autocomplete! If not, add this line to your ~/.vimrc
let g:ycm_global_ycm_extra_conf = '~/.vim/.ycm_extra_conf.py'
  • Removing the drop-down window when using the autocomplete, add this line:
set completeopt-=preview
  • To disable write-time diagnostics:
let g:ycm_show_diagnostics_ui = 0

Compiling: :terminal and g++ test.cpp, to exit the terminal: exit.


10 Add Auto-pairs

https://github.com/jiangmiao/auto-pairs

Plug 'jiangmiao/auto-pairs'

11. Installing the ccls Language Server

https://github.com/MaskRay/ccls

sudo apt install ccls

Copy this code and paste into your ~/.vimrc

let g:ycm_language_server =
  \ [{
  \   'name': 'ccls',
  \   'cmdline': [ 'ccls' ],
  \   'filetypes': [ 'c', 'cpp', 'cc', 'h', 'hpp' ],
  \   'project_root_files': [ '.ccls-root', 'compile_commands.json' ]
  \ }]

Note that when autocompleting now it automatically adds a header, if you don’t want that to happen, add this line to your .vimrc: let g:ycm_clangd_args=['--header-insertion=never']


12. Add UtilSnips and SuperTab

Plug 'SirVer/ultisnips'
Plug 'honza/vim-snippets'
Plug 'ervandew/supertab'

To work add this code:

let g:ycm_key_list_select_completion = ['<C-n>', '<Down>']
let g:ycm_key_list_previous_completion = ['<C-p>', '<Up>']
let g:SuperTabDefaultCompletionType = '<C-n>'
let g:UltiSnipsExpandTrigger = "<tab>"
let g:UltiSnipsJumpForwardTrigger = "<tab>"
let g:UltiSnipsJumpBackwardTrigger = "<s-tab>"

Edit snippets in: .vim/plugged/vim-snippets/UltiSnips/cpp.snippets or .vim/plugged/vim-snippets/snippets/cpp.snippets


Bonus

  • Add Vim Airline
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'

:AirlineTheme
:help AirLineTheme
  • Install TabNine and make it compatible with YouCompleteMe
let extension = expand('%:e')

if( extension == "cpp" || extension == "cc" || extension == "h" || extension == "hpp" ) 
Plug 'ycm-core/YouCompleteMe'
else
Plug 'zxqfl/tabnine-vim'
endif

Watch the video

If you want to see all the steps above on video I recommend watching the video below. However, the video is in Brazilian Portuguese, but you will be able to follow the commands and settings without any problems.

Add our feed: https://en.terminalroot.com.br/feed to:

Feedly