Language Server Protocol (LSP) plugin for Vim. You need Vim version 9.0 or above to use this plugin. This plugin is written using only the Vim9 script.
You can install this plugin directly from github using the following steps:
$ mkdir -p $HOME/.vim/pack/downloads/opt
$ cd $HOME/.vim/pack/downloads/opt
$ git clone https://github.com/yegappan/lsp
After installing the plugin using the above steps, add the following line to your $HOME/.vimrc file:
packadd lsp
You can also install and manage this plugin using any one of the Vim plugin managers (dein.vim, pathogen, vam, vim-plug, volt, Vundle, etc.).
You will also need to download and install one or more language servers corresponding to the programming languages that you are using. Refer to the https://langserver.org/ page for the list of available language servers. This plugin doesn't install the language servers.
The following language server protocol (LSP) features are supported:
- Code completion
- Jump to definition, declaration, implementation, type definition
- Display warning and error diagnostics
- Find all symbol references
- Workspace symbol search
- Display code outline
- Rename symbol
- Display type and documentation on hover
- Signature help
- Code action
- Display Call hierarchy
- Display Type hierarchy
- Formatting code
- Folding code
- Visually select symbol block/region
To use the plugin features with a particular file type(s), you need to first register a LSP server for that file type(s).
To register a LSP server, add the following lines to your .vimrc file (use only the LSP servers that you need from the below list). If you used vim-plug to install the LSP plugin, the steps are described later in this section.
let lspServers = [
\ #{
\ filetype: ['c', 'cpp'],
\ path: '/usr/local/bin/clangd',
\ args: ['--background-index']
\ },
\ #{
\ filetype: ['javascript', 'typescript'],
\ path: '/usr/local/bin/typescript-language-server',
\ args: ['--stdio']
\ },
\ #{
\ filetype: 'sh',
\ path: '/usr/local/bin/bash-language-server',
\ args: ['start']
\ },
\ #{
\ filetype: 'vim',
\ path: '/usr/local/bin/vim-language-server',
\ args: ['--stdio']
\ },
\ #{
\ filetype: ['go', 'gomod'],
\ path: '/usr/local/bin/gopls',
\ args: ['serve'],
\ syncInit: v:true
\ },
\ #{
\ filetype: ['rust'],
\ path: '/usr/local/bin/rust-analyzer',
\ args: [],
\ syncInit: v:true
\ },
\ #{
\ filetype: ['python'],
\ path: '/usr/local/bin/pyls',
\ args: []
\ },
\ #{
\ filetype: ['fortran'],
\ path: '/usr/local/bin/fortls',
\ args: ['--nthreads=1', '--use_signature_help', '--hover_signature']
\ },
\ #{
\ filetype: ['php'],
\ path: '/usr/local/bin/intelephense',
\ args: ['--stdio'],
\ syncInit: v:true,
\ initializationOptions: {
\ licenceKey: 'absolute path to file with key or key itself'
\ }
\ }
\ ]
call LspAddServer(lspServers)
The above lines add the LSP servers for C, C++, Javascript, Typescript, Shell script, Vim script, Go, Rust, Python, Fortran and PHP file types. In addition to the above listed file types, this plugin also supports other file types.
To add a LSP server, the following information is needed:
Field | Description |
---|---|
filetype | One or more file types supported by the LSP server. This can be a String or a List. To specify multiple multiple file types, use a List. |
path | complete path to the LSP server executable (without any arguments). |
args | a list of command-line arguments passed to the LSP server. Each argument is a separate List item. |
initializationOptions | User provided initialization options. May be of any type. For example the intelephense PHP language server accept several options here with the License Key among others. |
The LSP servers are added using the LspAddServer() function. This function accepts a list of LSP servers with the above information.
If you used vim-plug to install the LSP plugin, then you need to use the VimEnter autocmd to initialize the LSP server and to set the LSP server options. For example:
let lspServers = [
\ #{
\ filetype: ['c', 'cpp'],
\ path: '/usr/local/bin/clangd',
\ args: ['--background-index']
\ }
\ ]
autocmd VimEnter * call LspAddServer(lspServers)
let lspOpts = {'autoHighlightDiags': v:true}
autocmd VimEnter * call LspOptionsSet(lspOpts)
The following commands are provided to use the LSP features.
Command | Description |
---|---|
:LspCodeAction | Apply the code action supplied by the language server to the diagnostic in the current line. |
:LspDiagCurrent | Display the diagnostic message for the current line |
:LspDiagFirst | Display the first diagnostic message for the current buffer |
:LspDiagHighlightDisable | Disable diagnostic message highlights |
:LspDiagHighlightEnable | Enable diagnostic message highlights |
:LspDiagNext | Display the next diagnostic message after the current line |
:LspDiagPrev | Display the previous diagnostic message before the current line |
:LspDiagShow | Display the diagnostics messages from the language server for the current buffer in a new location list. |
:LspFold | Fold the current file |
:LspFormat | Format a range of lines in the current file using the language server. The shiftwidth and expandtab values set for the current buffer are used when format is applied. The default range is the entire file. |
:LspGotoDeclaration | Go to the declaration of the keyword under cursor |
:LspGotoDefinition | Go to the definition of the keyword under cursor |
:LspGotoImpl | Go to the implementation of the keyword under cursor |
:LspGotoTypeDef | Go to the type definition of the keyword under cursor |
:LspHighlight | Highlight all the matches for the keyword under cursor |
:LspHighlightClear | Clear all the matches highlighted by :LspHighlight |
:LspHover | Show the documentation for the symbol under the cursor in a popup window. |
:LspIncomingCalls | Display the list of symbols calling the current symbol. |
:LspOutgoingCalls | Display the list of symbols called by the current symbol. |
:LspOutline | Show the list of symbols defined in the current file in a separate window. |
:LspPeekDeclaration | Open the declaration of the symbol under cursor in the preview window. |
:LspPeekDefinition | Open the definition of the symbol under cursor in the preview window. |
:LspPeekImpl | Open the implementation of the symbol under cursor in the preview window. |
:LspPeekReferences | Display the list of references to the keyword under cursor in a location list associated with the preview window. |
:LspPeekTypeDef | Open the type definition of the symbol under cursor in the preview window. |
:LspRename | Rename the current symbol |
:LspSelectionExpand | Expand the current symbol range visual selection |
:LspSelectionShrink | Shrink the current symbol range visual selection |
:LspServerRestart | Restart the language server for the current buffer |
:LspShowReferences | Display the list of references to the keyword under cursor in a new location list. |
:LspShowServers | Display the list of registered language servers |
:LspShowServerCapabilities | Display the language server capabilities for the current buffer |
:LspShowSignature | Display the signature of the keyword under cursor |
:LspSubTypeHierarchy | Display the sub type hierarchy in a popup window |
:LspSuperTypeHierarchy | Display the super type hierarchy in a popup window |
:LspSwitchSourceHeader | Switch between a source and a header file. |
:LspSymbolSearch | Perform a workspace wide search for a symbol |
:LspWorkspaceAddFolder {folder} |
Add a folder to the workspace |
:LspWorkspaceListFolders | Show the list of folders in the workspace |
:LspWorkspaceRemoveFolder {folder} |
Remove a folder from the workspace |