Notes & TILs
Search…
Configuring LSP on NeoVim
Posted on 26 May, 2021
Make sure you have NeoVim version >=0.5.5 installed

Necessary stuff

1
" For language server configurations
2
Plug 'neovim/nvim-lspconfig'
Copied!
For autocompletion choose either of these choices both seems to be equally good
1
Plug 'nvim-lua/completion-nvim'
Copied!
Or
1
Plug 'hrsh7th/nvim-compe'
Copied!
For the sake of this guide I have chosen nvim-compe

Installing language servers

You would have to manually install the language servers you need.
  1. 1.
    Install language server pack for python (make sure you are running latest pip and Python 3.6+)
    1
    pip install 'python-language-server[all]'
    Copied!
  2. 2.
    Language server pack for go is maintained by the go team itself. Use go get to install gopls​
    1
    GO111MODULE=on go get golang.org/x/tools/[email protected]
    Copied!
    Or, You can also use the vim-go plug-in
    1
    Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
    2
    " run :GoInstallBinaries if setting up first time
    3
    " this will also install the languager server for go (gopls) automatically
    Copied!
  3. 3.
    For bash, install bash-language-server​
    1
    # if you have npm installed
    2
    npm i -g bash-language-server
    3
    # snap
    4
    sudo snap install bash-language-server
    Copied!

Configure keybindings

The following boilerplate config can be used as it is
1
set completeopt=menuone,noselect
2
​
3
" Use <Tab> and <S-Tab> to navigate through popup menu
4
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
5
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
6
​
7
nnoremap <silent> gd <cmd>lua vim.lsp.buf.definition()<CR>
8
nnoremap <silent> gD <cmd>lua vim.lsp.buf.declaration()<CR>
9
nnoremap <silent> gr <cmd>lua vim.lsp.buf.references()<CR>
10
nnoremap <silent> gi <cmd>lua vim.lsp.buf.implementation()<CR>
11
nnoremap <silent> gh <cmd>lua vim.lsp.buf.hover()<CR>
12
nnoremap <silent> <C-k> <cmd>lua vim.lsp.buf.signature_help()<CR>
13
nnoremap <silent> <C-n> <cmd>lua vim.lsp.diagnostic.goto_prev()<CR>
14
nnoremap <silent> <C-p> <cmd>lua vim.lsp.diagnostic.goto_next()<CR>
15
​
16
" Auto format using LSP on save
17
autocmd BufWritePre *go,*.py lua vim.lsp.buf.formatting_sync(nil, 100)
Copied!

Setup nvim-lspconfig

1
" Initialise server packs
2
lua <<EOF
3
require'lspconfig'.gopls.setup{}
4
require'lspconfig'.pyls.setup{}
5
require'lspconfig'.bashls.setup{}
6
EOF
Copied!

Setup nvim-compe

1
let g:compe = {}
2
let g:compe.enabled = v:true
3
let g:compe.autocomplete = v:true
4
let g:compe.debug = v:false
5
let g:compe.min_length = 1
6
let g:compe.preselect = 'enable'
7
let g:compe.throttle_time = 80
8
let g:compe.source_timeout = 200
9
let g:compe.resolve_timeout = 800
10
let g:compe.incomplete_delay = 400
11
let g:compe.max_abbr_width = 100
12
let g:compe.max_kind_width = 100
13
let g:compe.max_menu_width = 100
14
let g:compe.documentation = v:true
15
​
16
let g:compe.source = {}
17
let g:compe.source.path = v:true
18
let g:compe.source.buffer = v:true
19
let g:compe.source.calc = v:true
20
let g:compe.source.nvim_lsp = v:true
21
let g:compe.source.nvim_lua = v:true
22
let g:compe.source.vsnip = v:true
23
let g:compe.source.ultisnips = v:true
24
let g:compe.source.luasnip = v:true
25
let g:compe.source.emoji = v:true
Copied!
Notice: If you are embedding lua code in your vimscript config, use the following heredoc
1
lua <<EOF
2
-- [[ this is lua comment ]]
3
lua code
4
...
5
EOF
Copied!
After setting up both the plugins open a file whose language server you installed and run :LspInfo to check server diagnostics

Spicy Stuff 🌢️

Below are some plugins which spice up the neovim lsp experience

​lspsaga.nvim​

Richer UI for LSP
1
Plug 'neovim/nvim-lspconfig'
2
Plug 'glepnir/lspsaga.nvim'
Copied!
1
" lsp provider to find the cursor word definition and reference
2
nnoremap <silent> gh <cmd>lua require'lspsaga.provider'.lsp_finder()<CR>
3
" preview definition
4
nnoremap <silent> gd <cmd>lua require'lspsaga.provider'.preview_definition()<CR>
5
" show hover doc
6
nnoremap <silent> K <cmd>lua require('lspsaga.hover').render_hover_doc()<CR>
7
" scroll down hover doc or scroll in definition preview
8
nnoremap <silent> <C-f> <cmd>lua require('lspsaga.action').smart_scroll_with_saga(1)<CR>
9
" scroll up hover doc
10
nnoremap <silent> <C-b> <cmd>lua require('lspsaga.action').smart_scroll_with_saga(-1)<CR>
11
" code action
12
nnoremap <silent><leader>ca <cmd>lua require('lspsaga.codeaction').code_action()<CR>
13
vnoremap <silent><leader>ca :<C-U>lua require('lspsaga.codeaction').range_code_action()<CR>
Copied!

​lsp_signature​

Show function signature help as you type
1
Plug 'ray-x/lsp_signature.nvim'
Copied!
1
lua <<EOF
2
require'lspconfig'.gopls.setup {
3
on_attach = function(client)
4
require 'lsp_signature'.on_attach(client)
5
end,
6
}
7
EOF
Copied!

​vim-illuminate​

Highlighting other uses of the current word under the cursor
1
Plug 'RRethy/vim-illuminate'
Copied!
After installing make sure you have a language server running, it should work out of the box You can manually enable what files to use illuminate on as well
1
let g:Illuminate_ftwhitelist = ['vim', 'sh', 'python']
Copied!

Resources

Last modified 1mo ago