Using Neovim for Rust Development
If you currently use Vim1 or its "modern"2 iteration Neovim, or maybe
you've always wanted to check it out because you once read that all the cool-kids ™ use it and you want to write Rust code in an editor that is not VSCode. In this post, I'll share some notes on how to setup your machine3 so that you can start hacking4 some Rust code and look mighty cool doing it.
Specifically, this guide will demonstrate how to setup Rust dev environment using Neovim on MacOS.
THINGS YOU WILL NEED
- Bash (or zsh or fish) terminal <- choose your poison
- Homebrew (or a similar package manager supported by your OS)
- Resourcefulness in case things don't work as expected5
INSTALL NEOVIM
Open your terminal. Go to home directory.
cd ~ pwd // verify that you are in home directory
Install
neovim
usinghomebrew
.// Run these one at a time brew update brew upgrade brew install neovim
Confirm that the installation was successful.
nvim -v
Ok, so far so good. Let's now set it up as a proper IDE so that you can start re-implementing everything written in C/C++ in the last 50 years in Rust. We can do the setup in one of two ways - learn Lua, read the Vim and Nvim docs, and build the Lua config file by hand; OR we can cheat and use a pre-built base configuration that has sane defaults and a sensible set of plugins to start using Neovim as an IDE6.
INTRODUCING ASTRONVIM
There are quite a few dotfiles that will turn your Neovim editor into a functional IDE. I am going to talk about AstroNvim because that's what I personally use for Rust development. There is no objective reason for why I chose AstroNvim over others. I guess I like its aesthetics. 🤷🏽♂️ Enough rambling, onwards to installing and setting up AstroNvim.
Backup your current nvim configuration (if one exists)
mv ~/.config/nvim ~/.config/nvimbackup
Clone the GitHub repo in
~/.config/nvim
<- this is where nvim config livesgit clone https://github.com/AstroNvim/AstroNvim ~/.config/nvim
Run the nvim package manager to install plugins
nvim +PackerSync
Install Rust analyzer using
LspInstall
nvim . // open nvim
You should now have a blank .nvim file open in front of you.
// Pres <Esc> to enter normal mode, then type the following :LspInstall rust
Follow the on-screen instructions. Next, install tree-sitter (a parser generator tool) for Rust.
nvim . // open nvim again if you exited before // Press <Esc> to enter normal mode :TSInstall rust
Post install cleanup. Run the following commands in normal mode.
- Run
:PackerClean
to remove any disabled or unused plugins - Run
:PackerSync
to update and clean plugins
- Run
Keep the config updated by occassionally running
:AstroUpdate
Now you should have everything you need to start (and hopefully finish) a Rust project.Expand to see a few screenshots of Nvim and Rust:
CONCLUSION
Using Nvim (or Vim) as an IDE is not a lifestyle choice for the faint hearted or for the impatient, but if you persist you will be rewarded with an incredibly productive toolkit to efficiently crank out software. Hopefully, this tutorial helped you. Happy coding!
FOOTNOTES
Or at minimum, you've figured out how to exit Vim without restarting your machine.
API-first, Lua plugins, AST-producing parsing engine...fancy fancy 💅
These instructions are MacOS specific, might work on a subset of Linux distros.
Did I say hacking? I meant copy pasting code from Stack Overflow ™
Google is your friend.
You should still consider learning Lua. It's a nifty scripting language.
This work is licensed under CC BY-NC-SA 4.0.