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 termdebugloads the optional built-in plugin (add tovimrcto make permanent):Termdebug {binary}opens three windows: the source view, a GDB terminal, and a program output window- In the source window,
:Breaksets a breakpoint on the current line;:Clearremoves it :Run,:Continue,:Step,:Next,:Finishmap to GDB commands and can also be triggered from the GDB window directly:Evaluate(orKin 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 yourvimrcso it is always available - Use
let g:termdebug_wide = 1to arrange windows side-by-side instead of stacked - Set
let g:termdebugger = 'rust-gdb'(orlldb) to swap the backend - Works in both Vim 8+ (requires
+terminal) and Neovim (usesnvim_open_term) - You can type raw GDB commands directly in the GDB terminal window at any time