Vim Debug & Test

1.24

Debug

Vimspector

Install

Plugin 'puremourning/vimspector'

Dependencies

Vim or Neovim with Python3.10 or later. Check :echo has('python3')

# neovim example
python3 -m pip install --user --upgrade pynvim

Add adapters for node and python

./install_gadget.py --force-enable-node --enable-python

Add configuration

Nodejs and Python example. Press F5 on *.js or *.py files.

.vimspector.json

{
  "$schema": "https://clcode.net/assets/vimspector.schema.json",
  "adapters": {},
  "configurations": {
    "Node": {
      "adapter": "js-debug",
      "filetypes": [ "javascript" ],
      "default": true,
      "configuration": {
        "request": "launch",
        "stopOnEntry": false,
        "console": "integratedTerminal",
        "program": "${file}",
        "cwd": "${workspaceRoot}"
      },
      "breakpoints": {
        "exception": {
          "all": "",
          "uncaught": ""
        }
      }
    },
    "Python": {
      "adapter": "debugpy",
      "filetypes": [ "python" ],
      "default": true,
      "configuration": {
        "request": "launch",
        "program": "${file}",
        "stopOnEntry": false,
        "cwd": "${workspaceRoot}"
      },
      "breakpoints": {
        "exception": {
          "raised": "N",
          "uncaught": "",
          "userUnhandled": ""
        }
      }
    }
  }
}

Mappings

Human Mode
let g:vimspector_enable_mappings = 'HUMAN'
KeyMappingFunction
F5<Plug>VimspectorContinueWhen debugging, continue. Otherwise start debugging.
F3<Plug>VimspectorStopStop debugging.
F4<Plug>VimspectorRestartRestart debugging with the same configuration.
F6<Plug>VimspectorPausePause debuggee.
F9<Plug>VimspectorToggleBreakpointToggle line breakpoint on the current line.
<leader>F9<Plug>VimspectorToggleConditionalBreakpointToggle conditional line breakpoint or logpoint on the current line.
F8<Plug>VimspectorAddFunctionBreakpointAdd a function breakpoint for the expression under cursor
<leader>F8<Plug>VimspectorRunToCursorRun to Cursor
F10<Plug>VimspectorStepOverStep Over
F11<Plug>VimspectorStepIntoStep Into
F12<Plug>VimspectorStepOutStep out of current function scope

In addition, I recommend adding a mapping to <Plug>VimspectorBalloonEval, in normal and visual modes, for example:

" mnemonic 'di' = 'debug inspect' (pick your own, if you prefer!)

" for normal mode - the word under the cursor
nmap <Leader>di <Plug>VimspectorBalloonEval
" for visual mode, the visually selected text
xmap <Leader>di <Plug>VimspectorBalloonEval

You may also wish to add mappings for navigating up/down the stack, toggling the breakpoints window, and showing disassembly, for example:

nmap <LocalLeader><F11> <Plug>VimspectorUpFrame
nmap <LocalLeader><F12> <Plug>VimspectorDownFrame
nmap <LocalLeader>B     <Plug>VimspectorBreakpoints
nmap <LocalLeader>D     <Plug>VimspectorDisassemble
📖