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.
The script has two main functions:
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
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:
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.
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
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! 😁 🙏 💚 ✨ 🤙
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.
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. 😁 🙏 💚 ✨ 🤙