Neovim + clangd Configuration

avator
For Productivity!!!

And ZhuangBility23333

Neovim + clangd Configuration

1. Neovim

I am using Ubuntu18.04 and version of neovim installed by apt is not latest. I choose the installation from source.

How to install

1
2
3
4
5
git clone https://github.com/neovim/neovim.git
cd neovim
git checkout v0.4.2
make CMAKE_BUILD_TYPE=RelWithDebInfo
sudo make install

How to uninstall

1
2
sudo rm /usr/local/bin/nvim
sudo rm -r /usr/local/share/nvim/

2. Vim-plug

1
2
curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

3. LanguageClient-neovim

Create file at ~/.config/nvim/init.vim ,filling it with following content.

1
2
3
4
5
6
7
8
9
10
11
12
13

call plug#begin('~/.config/nvim/plugged')

Plug 'autozimu/LanguageClient-neovim', {
\ 'branch': 'next',
\ 'do': 'bash install.sh',
\ }

" (Optional) Multi-entry selection UI.
Plug 'junegunn/fzf'

call plug#end()

Enter nvim and just type :PlugInstall + Enter to install LanguageClient-neovim, just wait.

4. deoplete.nvim

Add following content to init.vim, between call plug#begin and call plug#end.

1
2
3
4
5
6
7
8
if has('nvim')
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
else
Plug 'Shougo/deoplete.nvim'
Plug 'roxma/nvim-yarp'
Plug 'roxma/vim-hug-neovim-rpc'
endif
let g:deoplete#enable_at_startup = 1

After it, the file looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
call plug#begin('~/.config/nvim/plugged')

Plug 'autozimu/LanguageClient-neovim', {
\ 'branch': 'next',
\ 'do': 'bash install.sh',
\ }

" (Optional) Multi-entry selection UI.
Plug 'junegunn/fzf'

if has('nvim')
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
else
Plug 'Shougo/deoplete.nvim'
Plug 'roxma/nvim-yarp'
Plug 'roxma/vim-hug-neovim-rpc'
endif
let g:deoplete#enable_at_startup = 1

call plug#end()

5. Clang and LLVM

how to add clang’s apt repo : https://apt.llvm.org/

As for Ununtu18.04, I do the following things :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# GPG Key
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -

# Repo source
sudo echo "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main" >> /etc/apt/source.list
sudo echo "deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main" >> /etc/apt/source.list


sudo apt update
sudo apt install clang-9
sudo apt install clangd-9

# For GNU/Linux libstd++
# For OSX libc++
sudo apt install libstdc++-8-dev

## Uninstall default clang and clangd before next step

sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-9 100
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-9 100
sudo update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-9 100


6. Edit the init.vim

add following content after plug#end()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

"make sign column always visiable
set signcolumn=yes

"make indent 4 spaces
set shiftwidth=4

"enable neovim-LanguageClient
let g:LanguageClient_autoStart = 1

"set clangd as server when it is C++
let g:LanguageClient_serverCommands = {
\ 'cpp': ['clangd'],
\ }

"set complete function for deoplete.vim
set completefunc=LanguageClient#complete


7. Enjoy

open nvim and Enjoy~~~

Note

Don’t forget to generate compile_commands.json.

Reference : GETTING STARTED WITH CLANGD