-
-
Save okuramasafumi/9b5eb54850fc4a15813a1d4f87ff85fb to your computer and use it in GitHub Desktop.
Key bindings for git with fzf (https://junegunn.kr/2016/07/fzf-git/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GIT heart FZF | |
# ------------- | |
is_in_git_repo() { | |
git rev-parse HEAD > /dev/null 2>&1 | |
} | |
fzf-down() { | |
fzf --height 50% --min-height 20 --border --bind ctrl-/:toggle-preview "$@" | |
} | |
_gf() { | |
is_in_git_repo || return | |
git -c color.status=always status --short | | |
fzf-down -m --ansi --nth 2..,.. \ | |
--preview '(git diff --color=always -- {-1} | sed 1,4d; cat {-1})' | | |
cut -c4- | sed 's/.* -> //' | |
} | |
_gb() { | |
is_in_git_repo || return | |
git branch -a --color=always | grep -v '/HEAD\s' | sort | | |
fzf-down --ansi --multi --tac --preview-window right:70% \ | |
--preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1)' | | |
sed 's/^..//' | cut -d' ' -f1 | | |
sed 's#^remotes/##' | |
} | |
_gt() { | |
is_in_git_repo || return | |
git tag --sort -version:refname | | |
fzf-down --multi --preview-window right:70% \ | |
--preview 'git show --color=always {}' | |
} | |
_gr() { | |
is_in_git_repo || return | |
git remote -v | awk '{print $1 "\t" $2}' | uniq | | |
fzf-down --tac \ | |
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" {1}' | | |
cut -d$'\t' -f1 | |
} | |
_gs() { | |
is_in_git_repo || return | |
git stash list | fzf-down --reverse -d: --preview 'git show --color=always {1}' | | |
cut -d: -f1 | |
} | |
_go() { | |
is_in_git_repo || return | |
git branch --all | fzf-down --reverse -d: --preview 'git show --color=always {1}' | | |
xargs git checkout -q | |
} | |
join-lines() { | |
local item | |
while read item; do | |
echo -n "${(q)item} " | |
done | |
} | |
() { | |
local c | |
for c in $@; do | |
eval "fzf-g$c-widget() { local result=\$(_g$c | join-lines); zle reset-prompt; LBUFFER+=\$result }" | |
eval "zle -N fzf-g$c-widget" | |
eval "bindkey '^g^$c' fzf-g$c-widget" | |
done | |
} f b t r h s o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if [[ $- =~ i ]]; then | |
bind '"\er": redraw-current-line' | |
bind '"\C-g\C-f": "$(_gf)\e\C-e\er"' | |
bind '"\C-g\C-b": "$(_gb)\e\C-e\er"' | |
bind '"\C-g\C-t": "$(_gt)\e\C-e\er"' | |
bind '"\C-g\C-h": "$(_gh)\e\C-e\er"' | |
bind '"\C-g\C-r": "$(_gr)\e\C-e\er"' | |
bind '"\C-g\C-s": "$(_gs)\e\C-e\er"' | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
join-lines() { | |
local item | |
while read item; do | |
echo -n "${(q)item} " | |
done | |
} | |
() { | |
local c | |
for c in $@; do | |
eval "fzf-g$c-widget() { local result=\$(_g$c | join-lines); zle reset-prompt; LBUFFER+=\$result }" | |
eval "zle -N fzf-g$c-widget" | |
eval "bindkey '^g^$c' fzf-g$c-widget" | |
done | |
} f b t r h s o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment