Neovim, solc, and coc.nvim FTW

Having a working knowledge of Solidity for a crypto developer in the EVM ecosystem is critical. You can be much more productive when you have the ability to dive into the source yourself. And diving into Solidity source code becomes much easier when you have deep integration in your dev environment of choice. So of course as a neck-bearded Neovim user, I wanted to share how I set this up!

What is a Language Server?

As of Solidity 0.8.11, the Solidity team has added LSP support to solc, “which means every IDE that has Language Server support will support Solidity out of the box.”

https://blog.soliditylang.org/2021/12/20/solidity-0.8.11-release-announcement/

This makes it easy for us Vim / Neovim users to get intelligent code actions (autocomplete, go to definition, batch renames, etc.) when reading and writing Solidity!

How to Integrate the Solc language server into Neovim with coc.nvim

So here’s my guide on the easiest, fastest way to get Solidity support in your Vim / Neovim environment:

  1. Let’s install solc with a version manager. I recommend asdf. Let’s install asdf by following the instructions on their homepage.

What is asdf? asdf is a meta version manager - it gives you a common interface for installing and switching between multiple versions of multiple languages. I recommend asdf because it makes it easy to install and switch between versions of solc, node (for Typescript!), and other languages. The only language I’ve found it doesn’t play nice with is Rust. Good thing Rustaceans have rustup!

  1. Now add the official Solidity plugin to asdf and use it install your desired version of solc by running:

  2. asdf plugin add solidity [<https://github.com/diegodorado/asdf-solidity.git>](<https://github.com/diegodorado/asdf-solidity.git>)

  3. asdf install solidity 0.8.9

  4. asdf global solidity 0.8.9

  5. You can substitute 0.8.9 for latest, or any other specific version of solc.

  6. Run solc --version to ensure it all worked.

  • If you want to install a specific version of solc, say 0.8.12, you can do asdf install solidity 0.8.12 to install, and asdf global solidity 0.8.17 to make it available in your path
  1. Now install Coc.nvim by following the instructions in their README. This will allow us see LSP suggestions and run LSP actions in Vim.
  • What is Coc? The Coc.nvim plugin stands up a Node process to host VSCode extensions, allowing you to use VSCode plugins in your Vim environment! And it works in both Vim and Neovim - don’t be fooled by the name.
  1. Now install the coc-solidity plugin to get the Language Server actions into Vim by opening Vim / Neovim and running:

  2. :CocInstall coc-solidity

  3. All done! Try it out by opening a .sol file, going to a variable usage, and trying the “go to definition” action. If you followed the Coc.nvim example configuration, you can do this with gd in normal mode.

That was 5 easy steps to get Solidity LSP support in Vim and/or Neovim. Have fun, and please reach out on Twitter if you have questions or suggestions for future posts @0xTARC.

Subscribe

Bye for now,

  • Tarc

Subscribe