Setting up YCM
Prerequisites:
- install pyenv and virtual environment as mentioned here.
YCM is a vim plugin , install Vundle first and and then install YCM,
-
set up Vundle:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
-
Update/create the
~/.vimrc
file with following lines. There are detailed instructions here
below here is an example
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
"All of your Plugins must be added before the following line
call vundle#end() " required
"Not really needed because of "shared" option when building python
"let g:ycm_path_to_python_interpreter = '$HOME/.pyenv/shims/python'
let g:ycm_clangd_binary_path="/usr/bin/clangd-12"
filetype plugin indent on " required
"To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
" :W sudo saves the file when the file is open in readonly mode.
"command W w !sudo tee % > /dev/null
" *********** Line **********
" show line numbets
"
set number
" ********** Indents *********
"replace tabs with spaces
"
set expandtab
" 1 tab = 2 spaces
"
set tabstop=2 shiftwidth=2
" when deleting whitespaces at the begining of a line, delete 1 tab worth of
" spaces
"
set smarttab
" when creating a new line, copy the indentation from the line above
"
set autoindent
" ********* Search **********
" ignorecase
set ignorecase
set smartcase
" highlight search results
"
set hlsearch
" highlight all pattern matches
"
set incsearch
" ************ Code stuff *****
"
set showmatch
"set cursorline
- Now install the plugin by launching
vim
and running:PluginInstall
You may need to update cmake or GCC or LLVM
note that in the above listing I’ve used let g:ycm_clangd_binary_path="/usr/bin/clangd-12"
to point to the clangd server.
Configuring projects
The following is taken from the YCM readme and the wiki
If you are using CMake add -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
when configuring or add set(CMAKE_EXPORT_COMPILE_COMMANDS ON )
to CMakeLists.txt
and copy or symlink the database to the root pf the project.
the second method is having a ycm_extra_conf.py
in the project dir (in or above the source dir as it searches upward until it finds a command file or a extra config python file) check this example provided by YCM developers.
def Settings(**kwargs):
return { 'flags': list_of_compiler_flags,
'override_filename': path_to_actually_compiled_file }
Note: [Clangd for arm64 can be downloaded(/via apt) here.
for clangd to find the compile_commands.json
we need it to be in a top-level directory relative to the source directory.
Note: sadly for this to work you need to run cmake twice (otherwise it won’t see the file)
IF(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json")
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E create_symlink "${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json" "${PROJECT_SOURCE_DIR}/compile_commands.json")
message(STATUS "******* symlinks generated *******")
ENDIF()
Note: read this blog post for more info