3

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].

7
  • I’m not sure this is on topic—it really has nothing to do with vim? I see the bit about wanting to open using fugitive, but the rest seems unrelated. Perhaps narrow the question to « how can I open a hash with fugitive »? To which the answer is probably :Gedit and variants; use vim -c if you want to run this at the command-line
    – D. Ben Knoble
    Mar 14, 2021 at 15:49
  • Ah, perhaps I was unclear; my question has everything to do with vim, will edit. I don't want to be quite so narrow in specifying a solution, because it would also be perfectly valid to say 'use these fugitive / fzf bindings' or even 'use plugin X'. Mar 14, 2021 at 18:53
  • so, I'm not looking for a commandline solution at all, I was just linking to the best equivalent that I currently have, which uses the commandline and is extremely inefficient in terms of keystrokes. Mar 14, 2021 at 19:00
  • I’m sorry, I still don’t see the full picture—are you asking how to use fzf to do the search? Or how to use the fzf results with fugitive? Or...? It seems like search works fine, so you just need to make fzf execute Gedit on the result? (Also, the title might need work: how would you do is a recipe for opinions.)
    – D. Ben Knoble
    Mar 14, 2021 at 19:07
  • alright, I just found fzf's sink (: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. Mar 14, 2021 at 19:46

2 Answers 2

1

Just came across another solution for fzf.vim which was introduced with this commit. It doesn't seem to be documented apart from this commit message:

[[B]Commits] Support arguments for git-log

Add support for command arguments that are passed to git-log (e.g.

:Commits -S foo -3).

It however can be used this way:

Commits
Commits!
Commits -S foo
Commits! -S foo
0

Just had the same question and couldn't find a solution with fzf.vim myself, but there seems to be a solution using search_log_content_file as part of the advanced-git-search.nvim plugin (which is however based on Telescope):

Advanced Git Search

Opens a Telescope window with a list of git commits that changed the current >file (renames included).

Grep behavior: filter on added, updated or removed code (log content: -G option in git).

It is invoked using:

:Telescope advanced_git_search search_log_content_file

which opens a fuzzy finder window in which you can search for content.

There is even a small demo here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.