Switching from a graphical editor like VS Code to a terminal-based editor (Vim, Neovim, Emacs, Nano) changes how you debug. This guide walks you through the essential steps to set up and use debugging tools effectively in the terminal, covering breakpoints, stepping, inspecting variables, and common pitfalls.
What You’ll Need
- A terminal editor installed (Vim, Neovim, Emacs, or Nano) with basic proficiency in its commands.
- A programming language with a command-line debugger (e.g., gdb for C/C++, pdb for Python, node inspect for Node.js).
- Optional: A terminal multiplexer like tmux or screen to run editor and debugger side-by-side.
Step-by-Step
- Learn your editor’s basic file navigation. Before debugging, you must move through files quickly. In Vim/Neovim, use
/patternto search,ggto go to top,Gto bottom, and:Nto go to line N. In Emacs, useCtrl-sto search andAlt-g gto go to line. In Nano, useCtrl-Wto search. - Install a debugger plugin (if using Vim/Neovim). For Neovim, use a plugin like
nvim-dap(Debug Adapter Protocol). For Vim, considervimspector. Install via your plugin manager (e.g., vim-plug). For Emacs, built-ingudmode works with many debuggers. Nano has no plugin system; use external debugger in another terminal. - Configure the debugger for your language. For example, in Neovim with nvim-dap, add a configuration for Python (using
debugpy) or Node.js (usingnode2). In Vimspector, create a.vimspector.jsonfile in your project root. For Emacs, useM-x gdband follow prompts. - Start the debugger from the editor. In Neovim, use
:lua require('dap').continue()to start. In Vimspector,:VimspectorLaunch. In Emacs,M-x gdband type the executable name. For Nano, open a separate terminal and run your debugger (e.g.,gdb ./program). - Set breakpoints. In Neovim with nvim-dap, place cursor on a line and press
F9(or:lua require('dap').toggle_breakpoint()). In Vimspector,F9also works. In Emacs, usegud-break(usuallyC-x SPC). In external debugger, usebreak filename:line. - Run and step through code. Start execution with
continue(orcin gdb). Usestep(s) to go into functions,next(n) to skip over,finishto exit current function, andcontinueto run to next breakpoint. In nvim-dap, map keys likeF10for step over,F11for step into. - Inspect variables. In gdb, type
print variable_name. In pdb, usep variable. In nvim-dap, hover over variable (if supported) or use:lua require('dap').repl.open()to open a REPL. In Emacs, usegud-print(usuallyC-x C-p). - Use watch expressions. In gdb,
watch variablestops when variable changes. In nvim-dap, add watch expressions via the DAP UI. In Vimspector, use:VimspectorWatch. - Edit code while debugging. In terminal editors, you can modify code and recompile/restart. In Vim, use
:maketo compile. In Emacs,M-x compile. Then restart the debugger (in gdb,runagain). - Use terminal multiplexer for side-by-side view. In tmux, split panes with
Ctrl-b %(vertical) orCtrl-b "(horizontal). Run editor in one pane, debugger in another. This mimics VS Code’s split view.
Common Mistakes
- Forgetting to compile with debug symbols. For C/C++, use
-gflag. Without it, breakpoints won’t work and variable names are unavailable. - Confusing step over vs step into. Step into goes into functions; step over executes the function without entering. Using the wrong one can waste time.
- Setting breakpoints on incorrect lines. Ensure the line is executable (not a comment or blank). In Python, breakpoints on function definitions may not trigger.
- Not using the REPL for quick evaluation. Many terminal debuggers have a REPL; using it to test expressions saves time.
- Ignoring the debugger’s help. Type
helpin gdb or pdb to see available commands. Most users only know a few commands and miss powerful features.
Tips
- Learn a few key bindings. In Vim/Neovim, map
F5to continue,F10to step over,F11to step into. This mimics VS Code and reduces friction. - Use conditional breakpoints. In gdb,
break file.c:10 if x > 5. In pdb,break file.py:10, condition(though pdb’s condition syntax is limited). In nvim-dap, set condition in the breakpoint UI. - Combine with logging. Sometimes adding print statements is faster than stepping. Use the debugger to inspect, but don’t forget quick
print()orconsole.log(). - Use the call stack. In gdb,
btshows backtrace. In pdb,w(where). In nvim-dap, the UI shows stack frames. This helps understand how you got to a line. - Customize your editor’s status line. Show current line and column. In Vim,
set ruler. In Emacs,line-number-modeis default.
Troubleshooting
- Breakpoints not hitting. Check that you compiled with debug symbols (
-gfor C/C++). For Python, ensure the file is saved. For Node.js, usenode --inspect-brkto pause at first line. Also, verify the debugger is attached to the correct process. - Debugger not starting. Ensure the debugger is installed and in your PATH (e.g.,
gdb,pdb,debugpy). Check the plugin configuration for correct adapter names. For nvim-dap, run:checkhealth dapto see issues. - Variable values show as “optimized out�. This happens when compiling with optimization (
-O2). Recompile with-O0 -gto see variables. - Can’t see output from the program. In gdb, use
set pagination offto avoid paging. In nvim-dap, check the REPL or output channel. In tmux, ensure you’re looking at the correct pane. - Keyboard shortcuts conflict with editor. For example,
F9might be used by your terminal. Remap the debugger keys in your editor config or use a different terminal (e.g.,Ctrl+Shift+F9).
Frequently Asked Questions
Can I use the same debugging workflow as VS Code?
Yes, with plugins like nvim-dap or vimspector, you get similar features: breakpoints, stepping, variable inspection, and watch expressions. The key difference is that you’ll use keyboard shortcuts rather than mouse clicks.
Do I need to learn a terminal editor before debugging?
It helps to be comfortable with basic navigation (search, go to line) to set breakpoints efficiently. But you can start debugging immediately and learn as you go.
What if my language doesn’t have a built-in debugger?
Most languages have a command-line debugger: C/C++ (gdb), Python (pdb), Node.js (node inspect), Java (jdb), Ruby (byebug). If not, use a logging approach or a debugger like LLDB for LLVM languages.
How do I debug a running process?
Use gdb -p PID to attach to a process. For Python, use py-spy dump --pid PID or py-spy top to see stack traces without stopping. For Node.js, send SIGUSR1 to enable inspector.
Is it possible to debug code in Nano?
Nano is a simple editor without plugin support. You can run your debugger in a separate terminal and edit files in Nano, but you won’t have integrated breakpoints or variable inspection. Consider switching to Vim or Emacs for a better experience.
How do I debug a web application in a terminal editor?
For Node.js, use the built-in inspector: run node --inspect app.js and connect from your editor via nvim-dap or vimspector. For Python web frameworks, use pdb in the view functions or set breakpoints in the code with breakpoint().
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L
- Remote Desktop on Raspberry Pi: How to Enable It
- Word Select Last Line in Paragraph?
- Google Assistant IFTTT Recipes: 7 Useful Automations
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t