vimtricks.wiki Concise Vim tricks, one at a time.

How do I run GDB inside Vim to debug programs without leaving the editor?

Answer

:packadd termdebug

Explanation

Vim ships with a built-in termdebug plugin that integrates GDB directly into the editor. Once loaded, you can set breakpoints on source lines, step through code, inspect variables, and watch the program counter move through your file — all without leaving Vim.

How it works

  • :packadd termdebug loads the optional built-in plugin (add to vimrc to make permanent)
  • :Termdebug {binary} opens three windows: the source view, a GDB terminal, and a program output window
  • In the source window, :Break sets a breakpoint on the current line; :Clear removes it
  • :Run, :Continue, :Step, :Next, :Finish map to GDB commands and can also be triggered from the GDB window directly
  • :Evaluate (or K in normal mode) shows the value of the expression under the cursor

Example

" In vimrc (or run interactively)
:packadd termdebug

" Open a compiled binary for debugging
:Termdebug ./my_program

" In the source file, move cursor to a line and set a breakpoint
:Break

" Start execution
:Run

" Inspect a variable under cursor
K

Tips

  • Add packadd termdebug (no colon) to your vimrc so it is always available
  • Use let g:termdebug_wide = 1 to arrange windows side-by-side instead of stacked
  • Set let g:termdebugger = 'rust-gdb' (or lldb) to swap the backend
  • Works in both Vim 8+ (requires +terminal) and Neovim (uses nvim_open_term)
  • You can type raw GDB commands directly in the GDB terminal window at any time

Next

How do I insert the entire current line into the command line without typing it?