Skip to content

Instantly share code, notes, and snippets.

@amanusk
Last active May 20, 2022
Embed
What would you like to do?
Cairo Env for Vim

Setting up Cairo env for vim development

Setup the cairo virtualenv

First things first, you need to install the cairo-lang package, preferably in a virtual env you can later use and upgrade. Currently only python 3.7 and 3.8 is supported

python3 -m venv ~/cairo_venv
source ~/cairo_venv/bin/activate

You can also add an alias in .bashrc to quickly source the cairo env file in the future

alias source_cairo='source /home/$USER/cairo_venv/bin/activate'

It is also recommended to install starknet-devnet for local testing

pip install starknet-devent

For further info checkout the cairo-lang quick-start

Setting up a language server

To easily get auto-complete functionality as well as some just-to-definition functionality, you can use the cairo language server cairo-ls

Installation instructions for vim functionality:

  1. Install Coc

  2. In an empty directory, run npm install cairo-ls. This can also be installed as a global dir, then your DIR in step 3 is ~/ or a direct path to the user home dir

  3. Run Vim, then use :CocConfig command to edit coc-settings.json with the following:

{
    "languageserver": {
        "cairo": {
            "module": "YOUR_DIRECTORY_FROM_STEP_2/node_modules/cairo-ls/out/server.js",
            "args": ["--node-ipc"],
            "filetypes": ["cairo"],
            "rootPatterns": [".git/"]
       }
    }
}
  1. Add the following to ~/.vimrc
au BufReadPost *.cairo set filetype=cairo
au Filetype cairo set syntax=cairo
  1. Optionally you can add the Coc's example Vim configuration to ~/.vimrc

Syntax highlight

For syntax highlighting, download https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/lang/ide/vim/syntax/cairo.vim to ~/.vim/syntax/

Auto-Format

The cairo virtual env comes with the executable cairo-format that formats the cairo code with pre-defined conventions.

The executable receives the -i tag to modify and save the file in place

To Run the auto-format code on-save when using vim, add this to your vimrc:

:autocmd BufWritePost *.cairo silent ! cairo-format % -i  2> /dev/null

This will format the current buffer in place. The command will do nothing if cairo_venv is not activated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment