The Wayback Machine - https://web.archive.org/web/20200602084217/https://github.com/junegunn/fzf/issues/1630
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--bind command clears when capturing output in variable #1630

Open
0phoff opened this issue Jul 3, 2019 · 2 comments
Open

--bind command clears when capturing output in variable #1630

0phoff opened this issue Jul 3, 2019 · 2 comments

Comments

@0phoff
Copy link

@0phoff 0phoff commented Jul 3, 2019

  • Category
    • fzf binary
    • fzf-tmux script
    • Key bindings
    • Completion
    • Vim
    • Neovim
    • Etc.
  • OS
    • Linux
    • Mac OS X
    • Windows
    • Windows Subsystem for Linux
    • Etc.
  • Shell
    • bash
    • zsh
    • fish

Issue

If I want to capture the output of an fzf command (or pipe it to a next command), --bind commands that print things (using less) do not seem to work (I can only see the terminal flashing).
It seems like it is launching less, but then exiting it for some reason

Example function

Here is an example of things going wrong.
What I basically want to achieve is a git commit browser like you show on your wiki page, but it should also output the commit hash, so that I could use it in the following way: git commit --fixup=$(gcb)

gcb()
{
    local commit

    commit=$(git log --graph --date=relative --abbrev-commit --color=always --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ad)%Creset' | \
        fzf --height=95% --ansi --no-sort --reverse --tiebreak=index \
        --bind=ctrl-s:toggle-sort \
        --bind "ctrl-g:execute:(echo {} | grep -o '[a-f0-9]\\{7\\}' | xargs -I % sh -c 'git show --color=always % 1>&2 | less -R')" \
        --bind "ctrl-f:execute:(echo {} | grep -o '[a-f0-9]\\{7\\}' | xargs -I % sh -c 'git show --color=always --name-status -r % 1>&2 | less -R')" \
    ) || return

    commit=${commit:2}
    echo ${commit%% *}
}

Note that inside the --bind commands, I needed to pipe the output from stdout to stderr (1>&2), because otherwise it would all be caught in the output commit variable.

@iiey
Copy link

@iiey iiey commented Jul 12, 2019

Instead of 1>&2 I saw Junegunn once did in an example by redirect to /dev/tty. So something like this would work for you:

--bind "ctrl-g:execute:(git show --color=always $(grep -m1 -o '[a-f0-9]\{7,40\}' <<< {} | head -1 ) | less -R > /dev/tty)" \

Maybe someone could give a clue why output execute() of fzf inside $() or <() flickered and disappeared?

Note:

  • better matching from at least 7 to 40 for SHA1 because 7 still ambiguous in big project
  • -m1 stop at first matching line and |head -1 so you don't get multiple results in case committer mentions about another commits in the first commit line
  • why not use --expect or --bind your git commit --fixup to a key, it's more convenient without much typing or getting error by abort
@0phoff
Copy link
Author

@0phoff 0phoff commented Jul 24, 2019

  • better matching from at least 7 to 40 for SHA1 because 7 still ambiguous in big project
  • -m1 stop at first matching line and |head -1 so you don't get multiple results in case committer mentions about another commits in the first commit line

This seems nice, and I will probably use it. Thanks!

  • why not use --expect or --bind your git commit --fixup to a key, it's more convenient without much typing or getting error by abort

The reason for this, is so that the command can be used for more than just git commit --fixup.
By having a command that returns a commit, you can use it for fixup, rebasing, ...
Anything that takes a git commit hash can work with this log browser so this increases its potential.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.