0

I've been messing around a bit with fzf and I'd like to be able to run a command that does something like take the output of diff -w -M origin/main...HEAD --name-only and make the contents available in a preview window.

I've gotten this far, with some copy/paste from the docs:

command! -bang -nargs=* Gdomo
   \ call fzf#vim#grep(
   \   'git diff -w -M origin/main...HEAD --name-only', 0,
   \   fzf#vim#with_preview({'dir': systemlist('git rev-parse --show-toplevel')[0]}), <bang>0))

That gets me the preview window, but selecting any of the files results in:

Error detected while processing function 24[30]..<SNR>53_callback[23]..function 24[30]..<SNR>53_callback:
line   21:
Vim(let):E684: list index out of range: 1

I'm guessing that trying to wrap grep is my first mistake, but I've looked at the plugin code and it's not clear to me what a better approach would be.

2
  • Aren't you simply looking for :h :GFiles which is built-in in fzf.vim and which opens the not staged files? And if :GFiles is not exactly what you're looking for you can either give it more arguments or look at fzf#vim#gitfiles code which should give your enough inspiration to do what you want.
    – statox
    Apr 16, 2021 at 10:27
  • Thanks. :GFiles is not quite what I'm looking for. I want a preview window with just the files in a branch which have diverged from main, even if there are no unstaged changes. This helps me quickly find, for example, a unit test which I want to tweak or just continue to work on files that I care about which are not currently open in a buffer. I'll have a closer look at fzf#vim#gitfiles.
    – oalders
    Apr 16, 2021 at 17:29

1 Answer 1

1

It turns out I was overthinking this and I also missed some of the documentation. fzf#run can accept an arbitrary command which returns a list of files.

A working example without a preview window:

command! GDomo
  \ call fzf#run({'source': 'git diff -w -M origin/main...HEAD --name-only', 'sink': 'e'})

A working example with a preview window:

command! GDomo
  \ call fzf#run(
  \   fzf#vim#with_preview(
  \     fzf#wrap({ 'source': 'git diff -w -M origin/main...HEAD --name-only' })
  \   )
  \ )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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