Fix lots of similar issues more quickly with Telescope and the Quickfix window
Plus, using ctrl-o for rapid code navigation
Hello there! I’m Nat Bennett, and you’re reading Nat’s Neovim Newsletter, where I share what I’ve learned recently about using and configuring Neovim. It’s been a few weeks since my last message — I’ve beens sick — and it may be a few weeks more before the next one — holiday travel coming up — but I wanted to share a really useful trick I discovered this week for making changes across a codebase.
Quicker Fixing with Quick Fix
Earlier this week I needed to remove a function from my codebase and then remove every call for that function. It wasn’t quite as simple as just deleting every line that contained the function, because it was returning a boolean that was used in an if statement — I had to remove the whole branch it was guarding, and leave the optional alternate branch as the only code path. So I had to make a similar but not trivial edit across a bunch of places in the codebase.
I started by using Telescope’s live_grep function. In my setup that means \fg,
then type in the query string.
This worked fine for removing it in one place, and even two, but by the third time I’d opened the Telescope window and entered the search string I started to think, now, wouldn’t it be nice to have a stable list I could work off of? And… isn’t this just what Quick Fix is for?
If you’re not already familiar with Quickfix, it’s a Vim feature for turning a list of file locations — originally produced by a build system, but the mechanism is generic — into a list of locations you can easily jump to. I wrote a general overview back in May — it’s worth getting familiar with if you don’t already use it.
Anyway, as it happens— it’s very simple to use Quickfix and Telescope together. When you’re in a Telescope list you can hit <C-q> (control-q) and Telescope will open the currently filtered list in a new window.
This list doesn’t automatically update as you remove items from the search, so you have to keep track of which locations you’ve already edited, but the window retains your cursor position when you switch to an edit window which works in a pinch.
Getting around with Ctrl-o
The other big upgrade in my workflow this week was that I finally have figured out how to actually move my cursor position back to where it was before I jumped files. Previously I relied a lot on bp
and bn
, or popping open Telescope’s buffer search, to get back to a previous file after I jump files. This has gotten more and more awkward as I’ve gotten better at navigating and at using things like go-to-definition. bp
takes me back to the previous buffer in the buffer list, but that’s not actually the buffer I was in previously.
Instead, the command I was missing was <C-o>
, which is straight up just “undo” for cursor location. I’ve been using it a ton this week — if you’re not already using it I hope it improves your vim life as much as it has mine.