keepcalculator

06 Numbers and Powers

Debugging Code When Switching to a Terminal Editor

Card No. 06·12093 · Updated · Standard formula, shown below

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

  1. Learn your editor’s basic file navigation. Before debugging, you must move through files quickly. In Vim/Neovim, use /pattern to search, gg to go to top, G to bottom, and :N to go to line N. In Emacs, use Ctrl-s to search and Alt-g g to go to line. In Nano, use Ctrl-W to search.
  2. Install a debugger plugin (if using Vim/Neovim). For Neovim, use a plugin like nvim-dap (Debug Adapter Protocol). For Vim, consider vimspector. Install via your plugin manager (e.g., vim-plug). For Emacs, built-in gud mode works with many debuggers. Nano has no plugin system; use external debugger in another terminal.
  3. Configure the debugger for your language. For example, in Neovim with nvim-dap, add a configuration for Python (using debugpy) or Node.js (using node2). In Vimspector, create a .vimspector.json file in your project root. For Emacs, use M-x gdb and follow prompts.
  4. Start the debugger from the editor. In Neovim, use :lua require('dap').continue() to start. In Vimspector, :VimspectorLaunch. In Emacs, M-x gdb and type the executable name. For Nano, open a separate terminal and run your debugger (e.g., gdb ./program).
  5. Set breakpoints. In Neovim with nvim-dap, place cursor on a line and press F9 (or :lua require('dap').toggle_breakpoint()). In Vimspector, F9 also works. In Emacs, use gud-break (usually C-x SPC). In external debugger, use break filename:line.
  6. Run and step through code. Start execution with continue (or c in gdb). Use step (s) to go into functions, next (n) to skip over, finish to exit current function, and continue to run to next breakpoint. In nvim-dap, map keys like F10 for step over, F11 for step into.
  7. Inspect variables. In gdb, type print variable_name. In pdb, use p variable. In nvim-dap, hover over variable (if supported) or use :lua require('dap').repl.open() to open a REPL. In Emacs, use gud-print (usually C-x C-p).
  8. Use watch expressions. In gdb, watch variable stops when variable changes. In nvim-dap, add watch expressions via the DAP UI. In Vimspector, use :VimspectorWatch.
  9. Edit code while debugging. In terminal editors, you can modify code and recompile/restart. In Vim, use :make to compile. In Emacs, M-x compile. Then restart the debugger (in gdb, run again).
  10. Use terminal multiplexer for side-by-side view. In tmux, split panes with Ctrl-b % (vertical) or Ctrl-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 -g flag. 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 help in 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 F5 to continue, F10 to step over, F11 to 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() or console.log().
  • Use the call stack. In gdb, bt shows 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-mode is default.

Troubleshooting

  • Breakpoints not hitting. Check that you compiled with debug symbols (-g for C/C++). For Python, ensure the file is saved. For Node.js, use node --inspect-brk to 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 dap to see issues.
  • Variable values show as “optimized outâ€�. This happens when compiling with optimization (-O2). Recompile with -O0 -g to see variables.
  • Can’t see output from the program. In gdb, use set pagination off to 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, F9 might 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

The whole Numbers and Powers drawer →