6 vim lsp
woky edited this page 2021-05-04 15:53:05 +02:00

Install vim-lsp following its README.

Register ccls by adding the following into your ~/.vimrc or ~/.vim/vimrc:

" Register ccls C++ lanuage server.
if executable('ccls')
   au User lsp_setup call lsp#register_server({
      \ 'name': 'ccls',
      \ 'cmd': {server_info->['ccls']},
      \ 'root_uri': {server_info->lsp#utils#path_to_uri(lsp#utils#find_nearest_parent_file_directory(lsp#utils#get_buffer_path(), 'compile_commands.json'))},
      \ 'initialization_options': {'cache': {'directory': expand('~/.cache/ccls') }},
      \ 'allowlist': ['c', 'cpp', 'objc', 'objcpp', 'cc'],
      \ })
endif

Note you may want modify allowlist of C/C++/... file suffixes to your taste.

Next, set up key bindings by adding the following to your ~/.vimrc or ~/.vim/vimrc:

" Key bindings for vim-lsp.
nn <silent> <M-d> :LspDefinition<cr>
nn <silent> <M-r> :LspReferences<cr>
nn <f2> :LspRename<cr>
nn <silent> <M-a> :LspWorkspaceSymbol<cr>
nn <silent> <M-l> :LspDocumentSymbol<cr>

Now it's good to go! Navigate to a C/C++/... project and test. Make sure you have compilation database in that project.