How Did I Setup NeoVim for Rust

Anon
The Startup
Published in
4 min readJan 13, 2021

A little while ago I started using VIM as my primary text editor and IDE(NeoVim to be precise). And I am going to use Rust for my new Web Project. I looked for some simple guides for the setup but I didn’t find one. So why not just create one for others who are going to face the same problem.

Quick Solution

Install the NeoVim and Rust add this NeoVim Config to your ~/.config/nvim/. Run these both commands in your terminal.

sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
nvim +PlugInstall

The entire explanation

Whatever operating system you are using I suppose you have already installed Rust(https://www.rust-lang.org/tools/install) and required components(rls, rustfmt, rust-analysis, rust-src). Also I will consider that you have installed the latest Vim or better the NeoVim. For the plugin manager I personally use Plug you may use any plugin manager.

Plugins

Plug ‘cespare/vim-toml’
Plug ‘dense-analysis/ale’

These are the only two plugins you need for linting, syntax highlighting, Going to Definition, Finding References, as well as auto completing the syntax.

Vim-toml

This plugin is specifically used for syntax highlighting for the .toml files. This plugin does not require any further settings.

Cargo.toml syntax highlighting

Ale

This plugin is going to do all the heavy lifting. I used separate plugins before but they don’t seem to go well together. So finally I decided to go with Ale. It’s mostly an All-In-One solution for linting and all other stuff I mentioned earlier. It uses the RLS(Rust Language Server) for most of the basic functionalities. Let us set up ALE.

First off we are not using any additional code completion tools. Therefore, we are going to enable the Ale default completion tool. All the below lines should go to your config(.vimrc or ~/.config/nvim/init.vim or wherever you specify your configs).

set omnifunc=ale#completion#OmniFunc
let g:ale_completion_enabled = 1
let g:ale_completion_autoimport = 1

These lines must be added before loading Ale itself.

Now for the error gutters, we don’t want our text area to move randomly. So, we will make the gutters visible always. Add the following line to your config.

let g:ale_sign_column_always = 1

For simplicity, if you want to auto check on save then add the following line to your config.

let g:ale_fix_on_save = 1

For a visual perspective, let us add the error and warning symbol to our config. You need to have proper fonts for this to work. I prefer “Fira Code Nerd Mono Fonts”. You may have to work around a little bit for this. Or if you are fine with the default ones then don’t add these lines to your config.

let g:ale_sign_error = ‘✗’
let g:ale_sign_warning = ‘’
Warning by Ale
Error by Ale

We have to define the fixers for auto formatting our code with the universal definition by creators. Even if you don’t do this, Ale will try to figure it out on its own but just for extra preciseness add the fixers for Ale in config. Also, I’ve added a line for removing any additional trailing lines and white spaces.

let g:ale_fixers = {
\ ‘*’: [‘remove_trailing_lines’, ‘trim_whitespace’],
\ ‘rust’: [‘rustfmt’],
\}

We’ve to trigger the auto completion menu while typing and also we gotta move around it through the tab key. Following lines will make this possible.

inoremap <silent><expr><TAB>
\ pumvisible() ? “\<C-n>” : “\<TAB>”
Auto Completion menu

For, Go To Definition and Find Reference I use F12 and F24. You can edit this both as per your liking.

nmap <silent> <YOUR PREFERRED KEY HERE> :ALEGoToDefinition<CR>
nmap <silent> <YOUR PREFERRED KEY HERE> :ALEFindReferences<CR>

Finally to navigate between the errors I use F11 and F23. Similarly, use the key binding as per your liking.

nmap <silent> <YOUR PREFERRED KEY HERE> <Plug>(ale_previous_wrap)
nmap <silent> <YOUR PREFERRED KEY HERE> <Plug>(ale_next_wrap)

The entire Ale configuration

Conclusion

Hope I explained well. If there is any suggestion or complaint please go ahead and let me know.There is one more way to do all this with different plugins. I will post it later if needed. Do check out my NeoVim config) to get an exact idea of what is going on.

--

--

Anon
The Startup

CS Dropout(Suspended). Self learner(So that I can feel good). Mostly chilling(Jobless). Atheist(Trend). Freelancer(Dropout remember). Logically Sound(Actually).