I love fzf.vim for file content searching within vim. But how could I use that to search for content that was once in a git repository's files?
This stackoverflow question has basically two interesting answers, I would say, but they are for searching on the commandline:
Using pickaxe:
git log -SFoo -- path_containing_change
git log -SFoo --since=2009.1.1 --until=2010.1.1 -- path_containing_change
And using git grep:
git grep <regexp> $(git rev-list --all)
git grep <regexp> $(git rev-list <rev1>..<rev2>)
Fzf.vim suggests a GGrep command in its README, which just searches the currently checked in versions of all files (i.e. same as :Rg
).
So I tried plugging the same git grep $(git rev-list)
command into that:
command! -bang -nargs=* GGrep
\ call fzf#vim#grep(
\ 'git grep --line-number -- '.shellescape(<q-args>).' $(git rev-list --all)', 0,
\ fzf#vim#with_preview({'dir': systemlist('git rev-parse --show-toplevel')[0]}), <bang>0)
Although that search seems to work fine, when I press enter I would want it to open fugitive showing the commit of the file. Currently it opens a new empty buffer with a name such as a8b7d22ef54:some/file.txt
, which is really not helpful. I wasn't able to find anything in fzf's documentation that shows how to customize the opening of the file.
The command I hacked together probably also doesn't work for large repositories, where I would want to search specifically from most recent commits through to older ones. I hope someone has a working configuration they will share for fuzzy searching git repositories.
My current workaround is to use the commandline, and then navigate to the commit specified in vim-flog, and then use fugitive's :Gedit [commit-hash]:[filepath]
.
:Gedit
and variants; usevim -c
if you want to run this at the command-lineGedit
on the result? (Also, the title might need work: how would you do is a recipe for opinions.):help fzf#run
) options through some other user's issue. Hang tight and I will soon have an answer that at least somewhat works. I still hope someone else has a streamlined and robust solution I can take over though.