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

How do I launch GDB inside Vim using the built-in termdebug plugin without preloading it?

Answer

:packadd termdebug | Termdebug ./a.out

Explanation

If you only debug occasionally, loading termdebug on demand keeps startup lean while still giving you an in-editor GDB workflow. This command loads the optional runtime plugin and immediately starts a debug session for your binary. It is a practical middle ground between always-on debugging plugins and context switching to an external terminal.

How it works

  • :packadd termdebug loads Vim's optional termdebug package from the runtime packages directory
  • | chains the next Ex command in the same line
  • :Termdebug ./a.out starts GDB for ./a.out and opens the debugging layout
  • You get source navigation, breakpoints, and terminal-driven debugger interaction from inside Vim

Example

After compiling your program:

:packadd termdebug | Termdebug ./a.out

Set breakpoints and run:

:Break
:Run

As execution stops, Vim jumps to relevant source lines while GDB output remains available in terminal windows.

Tips

  • Keep this as a command-line abbreviation or mapping if you debug frequently
  • Use :help terminal-debug for the full command set (:Break, :Step, :Over, etc.)
  • For projects with arguments, pass them through GDB commands after launch

Next

How do I tune diffopt so diffs align moved code and produce cleaner hunks?