Published on

Persistent Search with Neovim and Telescope

Authors
  • avatar
    Name
    Linell Bonnette
    Twitter

Persistent Search with Neovim and Telescope


Iā€™ve been doing a lot of deep refactoring in SBLiveā€™s biggest Rails codebase lately, and something thatā€™s come up as noticeably missing from my Neovim setup, which is using Telescope.nvim for my file browser and searching, is the ability for me to see a persistent list of search results for whatever code Iā€™m trying to refactor.

This is what Telescopeā€™s live grep searching looks like ā€” itā€™s pretty great. I can use the arrow keys to scroll through the results and the window on the right side shows a preview of that line within the file. Itā€™s super useful for thing like finding every instance of .delay in a codebase.

The problem is that as soon as I select one of the results, or as soon as I click on any other window, I lose those search results. Iā€™ve got to redo the search and figure out where I was exactly within them all over again every time. I havenā€™t noticed this previously because 90% of the time thatā€™s not a big deal at all and meshes well with how I write code.

Butā€¦. Iā€™ve got dozens of instances of .delay in this codebase and itā€™s so frustrating having to redo the same search dozens of times when what I really want is just to have a persistent list of the search results for me to look through at my leisure. I know other people want it too because itā€™s come up more than once in Telescopeā€™s issues:

The best advice I really found online was this Reddit post that told me that Ctrl-q puts all of the results in the quickfix list. That is almost exactly what I wantā€¦ except the quickfix list seems to be sort of painful to interact with, and the time I spent trying to learn started feeling like a sunk cost so I moved on with life.

Then, this morning, I was hit by a silly little epiphany that Iā€™m sure was mentioned on every page I encountered while searching for a solution:

  1. Open a new buffer. :enew
  2. Put the search results in the buffer. :%! rg --vimgrep '\.delay' .

Itā€™s quick and easy, doesnā€™t require installing anything else, and I feel like a bit of a fool for having spent more than a few minutes being bothered by this problem in the first place.