Skip to content

Instantly share code, notes, and snippets.

@Micrified
Created April 21, 2023 23:01
Show Gist options
  • Save Micrified/d2453e935310364bcfa80a445f29a1ad to your computer and use it in GitHub Desktop.
Save Micrified/d2453e935310364bcfa80a445f29a1ad to your computer and use it in GitHub Desktop.
Language Servers with VIM

Introduction

Autocompletion is certainly easily attainable for VIM through AutoComplPop, which provides a sort of simplistic suggestion approach using previously encountered words. However, auto-completion for languages is not provided. If you want that, you need to look to language servers. Language servers are effectively processes that abide by some kind of protocol and provide tips, corrections, and other language-specific feedback. An open source standard called the Language Server Protocol is available.

To make use of language server support via the Language Server Protocol, two separate components are needed:

  1. A language-server implementation that complies with the language server protocol
  2. A plugin for your editor or IDE that can take advantage of different language servers

Preparing the language plugin

For the plugin, I've chosen to try out vim-lsc. This is a project by natebosch, and provides access to language-server support for native VIM. In order to use it though, it's best to go via a plugin manager. I don't have one at this moment, but I noticed that the plugin author did recommend one to me called vim-plug. So I'll start by installing it.

Setting up vim-plug

Bootstrapping the plugin

To install vim-plug, the README provides the following bootstrap instruction for UNIX-like systems, which I've selected to use:

curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Configuring the plugin

To configure the plugin, additions are made to the .vimrc file using the README provided in the vim-plug repository. For the time being, no plugins are listed:

~/.vimrc

"Plugin manager (vim-plug)                                                      
call plug#begin('~/.vim/plugged')                                               
                                                                               
call plug#end()

Installing the language plugin

Now that we have a plugin manager (vim-plug), we can install our language server plugin, vim-lsc. We can do so by using the vim-plug ability to specify plugins using their github repository links:

"Plugin manager (vim-plug)
call plug#begin('~/.vim/plugged')
Plug 'https://github.com/natebosch/vim-lsc.git', { 'tag': 'v0.3.1' }
call plug#end()

We specify the latest version of vim-lsc, which is tagged as v0.3.1.

Fetching the plugins

To fetch the plugin, I once again consult the README for the vim-plug repository. This contains a list of commands:

PlugInstall [name ...] [#threads]  # Install plugins
PlugUpdate [name ...] [#threads]   # Install or update plugins
PlugClean[!]                       # Remove unlisted plugin
PlugUpgrade                        # Upgrade vim-plug
PlugStatus                         # Check plugin status
PlugDiff                           # Examine changes from update
PlugSnapshot[!] [output path]      # Get script to restore snapshot

Starting up an empty vim session, and running :PlugInstall and then :PlugStatus indicates that the language-server client vim-lsc is now installed:

- vim-lsc: OK  

Using the language server

To use language servers, we now need to look at the instructions for using the vim-lsc plugin in its' README. There, we find we can configure a language server by making the following changes to our ~/.vimrc file:

"Language support                                                            
let g:lsc_auto_map = v:true                                                     
let g:lsc_server_commands = {'c': 'clangd'} 

Cleaning up

To clean up a bit, I decided to move my previous install of AutoComplPop to the plugin manager by just adding another line to my vimrc file, and removing the manually installed files I had added:

Plug 'https://github.com/vim-scripts/AutoComplPop', { 'tag': '2.14.1' }

Bonus (Golang)

Since clangd was already present, I took it upon myself to add support for Go. Looking at langserver.org indicated an official language server was available for Go straight from the Go development team called gopls. So I went ahead and followed the installation command from the README:

go install golang.org/x/tools/gopls@latest

This ends up compiling and installing the binary at go/bin/gopls. I then added support for it by linking it in my .vimrc file:

 let g:lsc_server_commands = {'c': 'clangd', 'go': 'gopls' } 

(I have exported /usr/local/go/bin as my GOBIN variable in `.zshrc')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment