LSP for Vim Boomers

21. 05. 2024 | 25. 05. 2024 | Jakub Kadlčík | EN video vim lsp python

There are many great videos and articles about configuring NeoVim LSP using quickstart plugins or opinionated NeoVim distributions. Here I would like to focus on people who still use Vim and refuse to use plugins and custom configuration.

This is a supporting article for my LSP for Vim Boomers video, so you can easily copy-paste things. Please watch the video for more information.

Installation

mkdir -p ~/.vim/pack/vendor/start
cd ~/.vim/pack/vendor/start
git clone https://github.com/prabirshrestha/vim-lsp.git
sudo dnf install python3-lsp-server+all

Configuration

let g:lsp_diagnostics_enabled = 0

if executable('pylsp')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'pylsp',
        \ 'cmd': {server_info->['pylsp']},
        \ 'allowlist': ['python'],
        \ })
endif

function! s:on_lsp_buffer_enabled() abort
    setlocal omnifunc=lsp#complete
    nmap <buffer> gd <plug>(lsp-definition)
    nmap <buffer> gr <plug>(lsp-references)
    nmap <buffer> K <plug>(lsp-hover)
endfunction

augroup lsp_install
    au!
    " call s:on_lsp_buffer_enabled only for languages that has the server registered.
    autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
augroup END

Appendix

1. Pre-configured servers

There is a plugin not mentioned in the video called vim-lsp-settings. It features a long list of programming languages, and for each, configuration which LSP server should be used. If you wish to make the tradeoff and depend on one more plugin in exchange for a shorter ~/.vimrc, then throw away the Python-specific configuration from the previous example and do

cd ~/.vim/pack/vendor/start
git clone https://github.com/prabirshrestha/vim-lsp.git

2. Performance

A Reddit comment suggests that the following configuration line can noticeably boost the LSP performance.

let g:lsp_use_native_client = 1

3. Alternative LSP plugins

A possible alternative to vim-lsp might be yegappan/lsp. It should have a similarly easy configuration and it may be slightly faster. However, it requires Vim 9 and higher. Another alternative is ALE which should provide a faster autocomplete according to this Reddit comment. Example configuration:

set omnifunc=ale#completion#OmniFunc
nnoremap K <cmd>ALEHover<CR>
nnoremap <leader>gd <cmd>ALEGoToDefinition<CR>
nnoremap <leader>gr <cmd>ALEFindReferences<CR>
nnoremap <leader>ca <cmd>ALECodeAc
let g:ale_completion_enabled = 1