Previously I told you about Racer, an autocompletion tool for Rust that integrates nicely into many other editors. Today I’d like to talk to you about how to configure Rust autocompletion in Vim using Racer and some plugins. Please note that you need
racer installed before proceeding. Check out that blog post if you need some help.
vim-racer: the easy way
A quick and easy plugin that you can use is vim-racer. The installation process is very easy. Provided you use Vundle, Pathogen, VimPlug you can just drop this into your ~/.vimrc and install the plugin:
If you don’t use a plugin manager for Vim, you should totally do that! (I should talk about that someday). Or, download it manually from their repository and place it into your ~/.vim folder.
And that’s it. vim-racer will work provided you have racer on your
$PATH and the Rust source code is pointed by
$RUST_SRC_PATH. If you have this set on your env vars, you are ready to go. Or, you can also set these values in your .vimrc file:
vim-racer is associated to Vim’s native omni completion engine. Therefore, to autocomplete code in vim-racer the default way is to press Ctrl-X Ctrl-O while being on insert mode.
There are also bindings for going to the definition of some function. It comes useful sometimes. Use
gd or check out the README at their Git repository to find out how to open the definition in a split or in a vsplit.
YouCompleteMe: the powerful way
I don’t feel comfortable with YouCompleteMe. On the one hand it is one of the most powerful plugins for Vim I have ever seen. On the other hand, it has a extremely tedious install process and it takes a lot of time to make it work.
There is support for Rust autocompletion using YouCompleteMe. First, add YouCompleteMe to your plugin manager. Drop this on your ~/.vimrc.
Installing this plugin will take a lot of time because of all the submodules it has to get. If you don’t use a package manager for Vim, grab the files from their website and install them into your vim folder. However, you’ll need then to download the submodules by yourself. Read the docs!
Once is done, you must compile support for Racer. Open your terminal and go to the location where YouCompleteMe is installed in your .vim folder. In my case using VimPlug it is
~/.vim/plugged/YouCompleteMe. To configure YCM with support for Rust run
Configuring YCM used to take lots of time when working with C and clang. I’m installing it as I type these lines and the progress for the CMake script it is running seems to be faster – oh wait, it has started compiling other stuff; nevermind.
However, the time it takes to make it work really pays off because YouCompleteMe works automatically without having to press any key, so if you edit any Rust files now, you’ll get autocomplete as you type, similar to othe graphical editors you could use.