This is a quick guide to setting up Language Server Protocol (https://microsoft.github.io/language-server-protocol/) for vim.
You will need Vim 8 or NeoVim.
LSP setup consists of a client and one or more servers.
The client is installed as a vim plugin. Servers are easily installed with another vim plugin.
Install the plugins
Install the following plugins with your favorite plugin management solution, like https://github.com/junegunn/vim-plug, https://github.com/tpope/vim-pathogen, or Vim 8’s native packages (see :help packages
for more):
- https://github.com/prabirshrestha/vim-lsp
- https://github.com/mattn/vim-lsp-settings
- https://github.com/prabirshrestha/asyncomplete.vim
- https://github.com/prabirshrestha/asyncomplete-lsp.vim
Vim-lsp is the LSP client.
Vim-lsp-settings is a companion plugin that provides a convenient way of installing LSP server configurations.
Asyncomplete is a completion engine for Vim, and asyncomplete-lsp is a companion plugin that configures it to work with vim-lsp.
Configure an LSP server
Open a file of the type you want to have LSP enabled for. For example, I work with TypeScript a lot, so I open any file with the .ts
extension.
You can confirm that vim recognizes the filetype correctly by running :set ft?
in Vim and looking at the output.
Then, run :LspInstallServer
, a command provided by vim-lsp-settings.
It will prompt you to choose a server and then install it. Note down what’s the name of the server, because you will probably want to find documentation for it at some point.
After that succeeds, restart vim, open the file again, and run :LspStatus
.
If it prints running
, you’re set!
Note that vim-lsp-settings doesn’t have configuration available for every single LSP server in existence, so even if it tells you that no server can be found, it doesn’t necessarily mean that there is no LSP server for the language of your choice.
If you can find a server implementation on https://microsoft.github.io/language-server-protocol/implementors/servers/, you can configure it manually by following the documentation of vim-lsp.
Usage and customization
Take a look at the documentation of vim-lsp and the documentation of the LSP server you’re using to learn what operations are available.
To get started, type :Lsp
and <C-d>
to see all commands provided by vim-lsp, but note that some of them might not work with every LSP server.
You will probably want to set up mappings for the commands you want to use often. Here are mine:
nnoremap <leader>dd :LspDefinition<cr>
nnoremap <leader>dn :LspNextDiagnostic<cr>
nnoremap <leader>dp :LspPreviousDiagnostic<cr>
nnoremap <leader>df :LspReferences<cr>
nnoremap <leader>dr :LspRename<cr>
nnoremap <leader>ds :LspStopServer<cr>
nnoremap <leader>dp :LspPeekDefinition<cr>
nnoremap <leader>da :LspCodeAction<cr>
nnoremap <leader>dh :LspHover<cr>
nnoremap <leader>df :LspDocumentFormat<cr>
nnoremap <leader>dd :LspDefinition<cr>
One thing that seems missing to me in the set of commands provided by vim-lsp is a way to disable and enable LSP manually. But it turns out you can do it by calling the function lsp#disable()
:
:call lsp#disable()
You can map it as well:
nnoremap <leader>dq :call lsp#disable()
There is also a symmetrical function lsp#enable()
.
Notes on Vim compilation options
Vim-lsp docs mention that having a Vim installation with Lua support has performance benefits for some features.
You can check if you’re installation has the Lua support, run :version
and see if it is included (+lua
) or not (-lua
).
To compile vim with your selection of features, follow https://github.com/vim/vim/blob/master/src/INSTALL.
Alternatives
Vim-lsp is not the only LSP client available for Vim users.
Some alternatives:
- https://github.com/natebosch/vim-lsc
- https://github.com/dense-analysis/ale
- https://github.com/autozimu/LanguageClient-neovim