vrothberg / vgrep

a user-friendly pager for grep
GNU General Public License v3.0
662 stars 48 forks source link

Return to vgrep from Editor #177

Open mjklemm opened 2 years ago

mjklemm commented 2 years ago

Not a real issue, more a question. :-)

Is there a way to return to vgrep/fzf after opening a match in the editor? I'm using the bvgrep alias from the README for interactive searching and it would dramatically speedup things if I could go to a match by selecting it in the fzf list, then quit the editor and return to the (interactive) search that had before.

Any chance that this could work?

vrothberg commented 2 years ago

Thanks for reaching out, @mjklemm!

I have absolutely no experience using fzf, and really don't know if or how this could work. I rarely use or need an interactive mode but when I do, I use vgrep --interactive which will bring me back to vgrep when returning from the editor.

I will leave this issue open. Maybe someone in the community has an answer.

mjklemm commented 2 years ago

I have hacked a bit on this and I have come up with this "solution" (it's still very hacky, but it might give people a hint on what can be done).

This alias triggers the search with vgrep and uses fzf to presen the results:

vg() {
    vgrep --no-header --no-less "$1" | fzf --ansi --bind "enter:execute:vgrep-vim.sh {}"
}

The vgrep-vim.sh script then further processes the selected result to open Vim:

#!/bin/bash

file="$(echo $@ | sed 's/[0-9]* \(.*\) [0-9]* .*/\1/')"
line="$(echo $@ | sed 's/[0-9]* .* \([0-9]*\) .*/\1/')"

vim "$file" +"$line"

I'm sure that this is not the most efficient way to do this, and for sure it will break for certain corner cases, but it seems to work well enough for me.

vrothberg commented 2 years ago

Thanks for sharing, @mjklemm!

suvayu commented 8 months ago

There's no need for a custom script if you use this bind option: --bind "enter:execute:vim {2} +{3}"

mjklemm commented 8 months ago

There's no need for a custom script if you use this bind option: --bind "enter:execute:vim {2} +{3}"

Thanks! That's indeed very useful and quite simplifies the script. I still have, as it makes it easier for me to switch editors w/o changing the ZSH alias I have created.