Configure neovim for rust development
Tags: vim, rust, ide, lsp
Published: 2020-01-03 21:48:13

nvim's 0.5.0 release includes a native lsp-client: *nvim-lsp*.

I have toyed with neovim for a while as a long time vim user and decided to make the switch proper. This is my current setup, and a little advice on putting all the pieces together.

First of all, the vim config:

## init.vim

```vim
call plug#begin()
Plug 'neovim/nvim-lsp'
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
Plug 'Shougo/deoplete-lsp'
Plug 'ervandew/supertab'
Plug 'Chiel92/vim-autoformat'
call plug#end()

" setup rust_analyzer LSP (IDE features)
lua require'nvim_lsp'.rust_analyzer.setup{}

" Use LSP omni-completion in Rust files
autocmd Filetype rust setlocal omnifunc=v:lua.vim.lsp.omnifunc

" Enable deoplete autocompletion in Rust files
let g:deoplete#enable_at_startup = 1

" customise deoplete                                                                                                                                                     " maximum candidate window length
call deoplete#custom#source('_', 'max_menu_width', 80)

" Press Tab to scroll _down_ a list of auto-completions
let g:SuperTabDefaultCompletionType = "<c-n>"

" rustfmt on write using autoformat
autocmd BufWrite * :Autoformat

"TODO: clippy on write
autocmd BufWrite * :Autoformat

nnoremap <leader>c :!cargo clippy
```

There's a fair amount going on here, but all of this is related to helping format, lint and auto-complete rust code.

I'm using [rust](https://www.rust-lang.org/) for this example because I am loving learning rust at the moment but there are many supported languages - if you are setting yourself up for a different language (JS, python and others), try letting 
[nvim-lsp](https://neovim.io/doc/user/lsp.html) install the language server for you. We'll be compiling the language server from source (don't worry, [cargo](https://doc.rust-lang.org/cargo/) makes it a breeze).

## Installs

First of all, nvim-lsp at the time of writing is new, and you need at least version [0.5.0](https://github.com/neovim/neovim/releases) of nvim installed. Your current install is probably stable like mine was, which isn't new enough. So grab 0.5.0, `tar -xzf` and whack it somewhere in your path, like this (on linux):

```
tar -xzf nvim-linux64.tar.gz
sudo cp nvim-linux64/bin/nvim /usr/bin/nvim
nvim # this should load up 0.5.0
```

*If you don't have nvim 0.5.0 or greater nvim-lsp won't exist, let alone work!*

nvim-lsp and [autoformat](https://github.com/Chiel92/vim-autoformat#how-to-use) rely on external tooling to work their magic, so let's get it installed.

In your shell, from the top.

```
# install rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# install rustfmt (for formatting)
rustup component add rustfmt

# install clippy (for semantic linting)
rustup component add clippy

# hardest one last, install rust-analyzer
git clone https://github.com/rust-analyzer/rust-analyzer && cd rust-analyzer
cargo xtask install --server
```

To check it worked (your commit hash will likely vary!)

```
$ ra_lsp_server --version
rust-analyzer 823c152
```

Back in the world of vim plugins, start up nvim and install your plugins. If you have [vim-plug](https://github.com/junegunn/vim-plug#installation) that's as easy as

(inside nvim)
`:PlugInstall`

If you don't, it's just (in your shell)

```
curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

nvim
```

and then `:PlugInstall` as above. If you don't use vim-plug and don't know what a vim plugin manager is, my advice is simple: [vim-plug](https://github.com/junegunn/vim-plug#installation) is easily good enough and so simple. I highly recommend it.

Depending on the state of your nvim install, you might need a python module called `pynvim`. I think deoplete needs this.

```
pip install pynvim
```

Now you can edit some rust code!

* auto-completion should just appear (use tab to choose a suggestion, thanks supertab)
* run clippy by pressing `<leader>c` (in this case `-c`)
* on each write, your code will be formatted using rustfmt (never argue about code style again)
* Enjoy!

Let me know if you try this and run into issues and I'll update (m)any mistakes.

Would love to hear what nvim setup you are running, particularly for rust programming. I have loads to learn so let me know what you think, good or bad.