From Traceback to Editor in One Command

(edited)

Hey everyone,

A lot of my best tools are the small, quality-of-life scripts that save me a few seconds here and there. Over time, those seconds add up. Today, I want to share one of those little tools that I've been working on. I call it e (for editor), and it solves two specific annoyances I have.

First, I can never seem to remember the correct syntax to open a file at a specific line number in Vim (+linenumber). Second, when I get a long error traceback, I want to jump to the relevant files and lines instantly. This script handles both.


What It Does

The script has two main functions:

  1. Directly Opening Files at a Line: You can pass it a file argument with a colon and a line number, and it will open your editor directly to that spot.

    e fail.py:6
    
  2. Parsing Tracebacks from Stdin: This is the most powerful feature. You can pipe any command's output, like a Python traceback, directly into the script, and it will automatically parse out all the file paths and line numbers and open them for you.

For example, if I have a script that produces an error, I can do this:

python3 fail.py |& e -s

The |& is a handy shell shortcut that sends both stdout and stderr to the pipe, similar to doing 2>&1 | (I figured this out today! I was reading the best way to handle this).

Here's an example traceback from a failing script:

traceback_1.png

After running the command above, e parses that output and opens nvim with both files mentioned in the traceback, with the cursor placed at the exact line where the error occurred in each file.

traceback_2.png


How It Works

The script is a work in progress, but the core logic is based on a few regular expressions to find file and line number patterns in the piped-in text.

# Try Python traceback format
m = re.search(r'File "([^"]+)", line (\d+)', line)
if m:
    # ... process file and line ...
    continue

# Try colon format (file.py:123)
m = re.search(r'([^:\s\t]+):(\d+)', line)
if m:
    # ... process file and line ...

It looks for the standard File "...", line ... pattern from Python tracebacks, as well as the more general filename:linenumber format. It collects all unique file locations and then builds a command to launch your $EDITOR (in my case, nvim) with all the files open to the correct lines.

It's a small thing, but it shaves valuable seconds off my debugging loop and makes the whole process feel much smoother.

You can find the full script on my GitHub Gist:
https://gist.github.com/TheCrazyGM/f51fa200fb2d8abe6daf9ac241e17af9

EDIT: I should mention that it's only been testing in vim and neovim.

As always,
Michael Garcia a.k.a. TheCrazyGM

0.08933729 BEE
1 comments

I love these tools! This is another super useful one, though I've never tried to use vim yet. Thank you for all these gems, my friend! 😁 🙏 💚 ✨ 🤙

0.00035788 BEE

vim and neovim are not for the faint of heart, honestly if I didn't learn how to use it like 20 years ago, I don't think I would have even bothered now. But, once you do learn it, I tend to turn on vim keys for EVERYTHING I use, it becomes second nature.

0.00000114 BEE

Hahaha...that's exactly the impression that I got about it, and one reason that I never dove into it. That doesn't surprise me, as I've also heard that it's quite powerful once mastered. 😁 🙏 💚 ✨ 🤙

0.00037512 BEE