reddit / emacs
Is there a magit-like interface for grep?
JeffreyBenjaminBrown · 2022-02-18 02:40
Magit has a philosophy of making everything explorable. You don't just see a diff; you can jump to the file from the diff. You don't just see filenames; you can jump to the file from the name. The view is transformable. Etc, etc.
I learned today about running grep from within emacs. I tried it out, and the results were spoiled by the inclusion of files from my __mypy__ folder, which aren't of interest. I could modify the grep command to excplude that folder, but I'm lazy. I'd prefer to be able to click on the output and say "hide this and anything else from the same folder".
I'd also like to be able to interactively toggle the -h option, to hide the filenames on the left side.
Is this possible? I see that I can jump to the file, and the line in question, which is great. Other than that, though, the output does not seem to be very interactive.
vonfuckingneumann · 2022-02-18 03:18
[Deadgrep](https://github.com/Wilfred/deadgrep) does this, IIUC (I use ripgrep.el instead, but I think deadgrep does something like what you want)
ieure · 2022-02-18 03:32
Regular deadgrep user here, and yes, it's similar.
JeffreyBenjaminBrown · 2022-02-18 03:33
That does look nice. Even just being able to collapse files not of interest would save a lot of time. Thanks!
lrochfort · 2022-02-18 08:47
If you're using magit, then I'd suggest looking at projectile.
Projectile understands your git repos and lets you navigate them intelligently with smart completion options.
Of most interest to you is the "ag" aka "the silver searcher" integration. Ag is a fast grep alternative. Projectile automatically limits the scope of the ag search to your git project.
Thaodan · 2022-02-18 10:30
>Is this possible? I see that I can jump to the file, and the line in
question, which is great. Other than that, though, the output does not
seem to be very interactive.
It is possible as explained above but it takes up ressources rendering every result where as M-X find-grep is very simple.
jpe-90 · 2022-02-18 11:45
The interactive prompts of Magit are provided by a library called "transient" by the same author. I don't know of such an interface for grep, but there is one for ag: https://github.com/gexplorer/counsel-ag-popup
7890yuiop · 2022-02-18 12:17
> I'd prefer to be able to click on the output and say "hide this and anything else from the same folder".
`M-x flush-lines` (or https://github.com/dgtized/winnow.el if you feel like installing something extra.)
> I see that I can jump to the file, and the line in question, which is great. Other than that, though, the output does not seem to be very interactive.
How else should one interact with "filename + line number" ?
JeffreyBenjaminBrown · 2022-02-18 12:28
> flush-lines
That's cool to know about, thanks! But it also illustrates my point -- the *grep* buffer is read-only, so flush-lines fails in it.
Other ways you might interact with the buffer would be to unfold the matches it displays from a given file while keeping all other files folded (deadgrep allows this, I just learned), toggle whether line numbers or filenames are displayed, toggle how wide of a context to show (as if you had edited the -C option to grep and rerun it), etc.
Come to think of it, maybe the only interactions missing are changes to the view of the results. But if those changes actually reran the search again, they would be expensive; they'd have to do something smarter.
maw · 2022-02-18 13:14
deadgrep is brilliant and I don't know how I survived so long without it.
Maybe I'm just weird like this but I prefer this sort of tool to run in the window I launch it from. I therefore use this:
(defun deadgrep-this-window (search-term)
(interactive "sSearch term: ")
(let ((display-buffer-overriding-action
'((display-buffer-reuse-window
display-buffer-same-window)
(inhibit-same-window . nil))))
(funcall-interactively #'deadgrep search-term)))
and have it bound to something convenient. I use something similar for `magit-status`.
hmelman · 2022-02-18 14:36
I use a different ripgrep integration, [rg](https://github.com/dajva/rg.el). It has a menu using transient, just like magit (set it up with `(rg-enable-menu)`). It makes rerunning the searches with different parameters easy.
It doesn't have a keybinding to hide a section like magit does, but I quickly tried enabling `outline-minor-mode` with pretty good results, see [rg Issue #132](https://github.com/dajva/rg.el/issues/132).
hmelman · 2022-02-18 14:50
>> flush-lines
> That's cool to know about, thanks! But it also illustrates my point -- the grep buffer is read-only, so flush-lines fails in it.
You can always use `C-x C-q` to make the buffer editable. Or add something to `grep-mode-hook` to always do this.
JeffreyBenjaminBrown · 2022-02-18 15:02
Ooh that's awesome. Thanks!
dparen12 · 2022-02-18 16:12
Tooting my own horn here, but I created https://github.com/dp12/double-saber for this very purpose.
It lets you quickly delete lines or narrow to matching lines in a search buffer, even for read-only search buffers. And it provides undo/redo functionality
wargxs · 2022-02-18 18:23
Nice!
7890yuiop · 2022-02-18 21:30
Is that different to [`winnow`](https://github.com/dgtized/winnow.el)?
Horrih · 2022-02-18 22:17
For programming i use a lot projectile+ag, ag (also called the silver searcher) being a grep replacement for the terminal with friendlier formatting
dparen12 · 2022-02-21 19:26
Thanks for pointing that package out! I didn't realize it existed. They are similar, but slightly different.
`winnow` will pass search terms as regexps directly to `flush-lines` or `keep-lines`. `double-saber` does the same if the search term is a single item, but if there are multiple items, it generates a matching regexp for all of them (e.g., you can enter `emacs vim` to have `double-saber` make a regexp matching those two words, whereas `winnow` would treat those two words as a regexp, which probably won't do what you want). Also, `double-saber` has a sorting function and undo/redo functionality.
7890yuiop · 2022-02-21 20:05
Ah, I see; thanks. `winnow` does what I want then (and I'm glad to know you hadn't inadvertently made an exact duplicate :) The sorting command is an interesting addition; I might have to experiment with that. Cheers.
tgerdino · 2022-02-22 21:17
And there is the venerable `vc-git-grep`, which I think beats stock `grep` and `rgrep` any day.
Lumpy-Breakfast-2656 · 2022-03-16 20:31
I completely agree. magit, I love you, but stop splitting my buffers up.
You will live in my init file forever, thank you.