How to debug rust applications with VIM
Tags: rust, vim, debug, lldb
Published: 2020-12-23 05:08:52

---
title: How to debug rust applications with VIM
published: true
description: This is a simple guide to configure the loved (and hated) VIM editor to develop and debug rust applications.
tags: rust, vim, debug, lldb
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/gq8ck1iknt1hlm1w25ks.png
---

Some time ago a started to learn Rust language, but recently I changed my edition tool. I migrated from Visual Studio Code to VIM (Neo Vim actually) and the most difficult thing was learning how to debug my Rust code using my new editor. Happily, I found a package to debug Rust applications, its name is [Vimspector](https://github.com/puremourning/vimspector).

## Requirements
According to your operative system, you would need to install different packages.

For Mac OS X:
```bash
# Install LLDB application
xcode-select --install
```

## Installing [Neo]Vim package

Package installation depends on the package manager you have been using. In my case, I've been using [VIM-Plug](https://github.com/junegunn/vim-plug), the installation could be done by adding the dependency to the configuration file:

```vim
" Using vim-plug
Plug 'puremourning/vimspector'

" Using NeoBundle
NeoBundle 'puremourning/vimspector'

" Using Vundle
Plugin 'puremourning/vimspector'

```
To enable the plugin shortcuts easily you can add the next line to your configuration file:
```vim
" You can use 'VISUAL_STUDIO' or 'HUMAN'
" Visual Studio config avoid to the mapping of <F3> key, sometimes used to map file explorer buffer.
let g:vimspector_enable_mappings = 'VISUAL_STUDIO'
```
To know the fast config key mappings you can use [this link](https://github.com/puremourning/vimspector#mappings) or directly from [the code](https://github.com/puremourning/vimspector/blob/61179b7670491a70b40cb34ddeefe793bee7fcb8/plugin/vimspector.vim#L63).

Before using the debugger you need to prepare the general debugging configuration file, that file is located at `</path/to/vim/plugins>/vimspector/gadgets/<os>/.gadgets.d` and its name is `lldb-vscode.json`, where the `/path/to/vim/plugins` is the path in your file system where the [Neo]Vim plugins are installed. example paths are `/Users/andres/.config/nvim/plugins` and `/home/andres/.config/nvim/plugins` for Mac and Linux respectively.

The content of the file `lldb-vscode.json` is:
```json
{
  "adapters": {
    "lldb-vscode": {
      "variables": {
        "LLVM": {
          "shell": "brew --prefix llvm"
        }
      },
      "attach": {
        "pidProperty": "pid",
        "pidSelect": "none"
      },
      "command": [
        "${LLVM}/bin/lldb-vscode"
      ],
      "env": {
        "LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY": "YES"
      },
      "name": "lldb"
    }
  }
}
```

Also, you need to add your project configuration file `.vimspector.json` in the root of your Rust project (at the same level as your `Cargo.toml` file). The initial content could be:

```json
{
  "configurations": {
    "Rust - Test": {
      "adapter": "CodeLLDB",
      "configuration": {
        "request": "launch",
        "program": "${workspaceRoot}/target/debug/<project name>"
      }
    }
  }
}
```

Finally, you can create breakpoints in your code, Using VISUAL_STUDIO mode you can create a breakpoint with `<F9>`.

Finally, this post is only a way to debug in Mac OS X, in a future post I will go deep on how to do it on Linux operative systems.

Read, Enjoy, Share...