Skip to content

Instantly share code, notes, and snippets.

@aluxian
Forked from junegunn/functions.sh
Last active December 9, 2023 19:34
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save aluxian/9c6f97557b7971c32fdff2f2b1da8209 to your computer and use it in GitHub Desktop.
Save aluxian/9c6f97557b7971c32fdff2f2b1da8209 to your computer and use it in GitHub Desktop.
Key bindings for git+fzf ported to Fish shell https://junegunn.kr/2016/07/fzf-git/
function __git_fzf_is_in_git_repo
command -s -q git
and git rev-parse HEAD >/dev/null 2>&1
end
function __git_fzf_git_status
__git_fzf_is_in_git_repo; or return
git -c color.status=always status --short | \
fzf -m --ansi --preview 'git diff --color=always HEAD -- {-1} | head -500' | \
cut -c4- | \
sed 's/.* -> //'
commandline -f repaint
end
function __git_fzf_git_branch
__git_fzf_is_in_git_repo; or return
git branch -a --color=always | \
grep -v '/HEAD\s' | \
fzf -m --ansi --preview-window right:70% --preview 'git log --color=always --oneline --graph --date=short \
--pretty="format:%C(auto)%cd %h%d %s %C(magenta)[%an]%Creset" \
(echo {} | sed s/^..// | cut -d" " -f1) | head -'$LINES | \
sed 's/^..//' | cut -d' ' -f1 | \
sed 's#^remotes/##'
commandline -f repaint
end
function __git_fzf_git_tag
__git_fzf_is_in_git_repo; or return
git tag --sort -version:refname | \
fzf -m --ansi --preview-window right:70% --preview 'git show --color=always {} | head -'$LINES
commandline -f repaint
end
function __git_fzf_git_log
__git_fzf_is_in_git_repo; or return
git log --color=always --graph --date=short --format="%C(auto)%cd %h%d %s %C(magenta)[%an]%Creset" | \
fzf -m --ansi --reverse --preview 'git show --color=always (echo {} | grep -o "[a-f0-9]\{7,\}") | head -'$LINES | \
sed -E 's/.*([a-f0-9]{7,}).*/\1/'
commandline -f repaint
end
# https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236
function git_fzf_key_bindings -d "Set custom key bindings for git+fzf"
bind \cg\cf __git_fzf_git_status
bind \cg\cb __git_fzf_git_branch
bind \cg\ct __git_fzf_git_tag
bind \cg\ch __git_fzf_git_log
end
@max-sixty
Copy link

These are great, thank you.

I have one issue: When I select an entry and hit Return, the entry appears above the prompt rather than in the prompt. Here's an example. This doesn't happen for normal fzf commands. Do you know a way off hand to make the entry stay in the prompt?

Screen Cast 2020-09-09 at 2 25 52 PM

@aluxian
Copy link
Author

aluxian commented Sep 10, 2020

These are great, thank you.

I have one issue: When I select an entry and hit Return, the entry appears above the prompt rather than in the prompt. Here's an example. This doesn't happen for normal fzf commands. Do you know a way off hand to make the entry stay in the prompt?

I don't, sorry.

@max-sixty
Copy link

Thanks for the code anyway!

@max-sixty
Copy link

max-sixty commented May 16, 2021

Edit: revised & expanded here.


Here's the version that adds the entry to the commandline. I'm sure there's a better way of doing this in fish — feel free to critique.

# Deciphered from fzf-file-widget. Somewhat unclear why it doesn't exist already!
function fzf_add_to_commandline -d 'add stdin to the command line, for fzf functions'
  read -l result
  commandline -t ""
  commandline -it -- (string escape $result)
  commandline -f repaint
end

# https://gist.github.com/aluxian/9c6f97557b7971c32fdff2f2b1da8209
function __git_fzf_is_in_git_repo
  command -s -q git
    and git rev-parse HEAD >/dev/null 2>&1
end

function __git_fzf_git_status
  __git_fzf_is_in_git_repo; or return
  git -c color.status=always status --short | \
    fzf -m --ansi --preview 'git diff --color=always HEAD -- {-1} | head -500' | \
    cut -c4- | \
    sed 's/.* -> //' | \
    fzf_add_to_commandline
  commandline -f repaint
end

function __git_fzf_git_branch
  __git_fzf_is_in_git_repo; or return
  git branch -a --color=always | \
    grep -v '/HEAD\s' | \
    fzf -m --ansi --preview-window right:70% --preview 'git log --color=always --oneline --graph --date=short \
      --pretty="format:%C(auto)%cd %h%d %s %C(magenta)[%an]%Creset" \
      --print0 \
      --read0 \
      (echo {} | sed s/^..// | cut -d" " -f1) | head -'$LINES | \
    sed 's/^..//' | cut -d' ' -f1 | \
    sed 's#^remotes/##' | \
    fzf_add_to_commandline
end

function __git_fzf_git_tag
  __git_fzf_is_in_git_repo; or return
  git tag --sort -version:refname | \
    fzf -m --ansi --preview-window right:70% --preview 'git show --color=always {} | head -'$LINES | \
    fzf_add_to_commandline

end

function __git_fzf_git_log
  __git_fzf_is_in_git_repo; or return
  git log --color=always --graph --date=short --format="%C(auto)%cd %h%d %s %C(magenta)[%an]%Creset" | \
    fzf -m --ansi --reverse --preview 'git show --color=always (echo {} | grep -o "[a-f0-9]\{7,\}") | head -'$LINES | \
    sed -E 's/.*([a-f0-9]{7,}).*/\1/' | \
    fzf_add_to_commandline
end

@kidonng
Copy link

kidonng commented Dec 10, 2021

Awesome work 👍 just want to mention fzf.fish also provides key bindings for git log, git status and more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment