Skip to content

Instantly share code, notes, and snippets.

@jan-warchol
Forked from junegunn/functions.sh
Last active June 26, 2017 09:04
Show Gist options
  • Save jan-warchol/0a9cf720c465cdbb24b34c23f3689ea2 to your computer and use it in GitHub Desktop.
Save jan-warchol/0a9cf720c465cdbb24b34c23f3689ea2 to your computer and use it in GitHub Desktop.
Key bindings for git with fzf (https://junegunn.kr/2016/07/fzf-git/)
# GIT heart FZF
# -------------
is_in_git_repo() {
git rev-parse HEAD > /dev/null 2>&1
}
fzf-down() {
fzf --height 50% "$@" --border
}
# "Smart" checkout
# choose from both local and remote branches (if you have a remote branch
# "origin/X" and do `git checkout X`, git will set up local "X" automatically)
gg() {
git branch --all --color=always |
cut -c 3- |
grep -v "remotes/.*/HEAD" |
sed 's|remotes/[^/]*/||' |
# remove remote branches that duplicate local ones
# prepend dummy string to have even columns (some branches are colored)
sed 's|^|____|' | sed 's|____||' |
sort --uniq --key=1.5 | # ignore color code (usually 4 chars)
sed 's|____||' |
# list local branches before remote ones
sort --reverse |
fzf-down --ansi |
xargs git checkout
}
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}) | head -500' |
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 --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
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 {} | head -'$LINES
}
gh() {
is_in_git_repo || return
git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always |
fzf-down --ansi --no-sort --reverse --multi --bind 'ctrl-s:toggle-sort' \
--header 'Press CTRL-S to toggle sort' \
--preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -'$LINES |
grep -o "[a-f0-9]\{7,\}"
}
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} | head -200' |
cut -d$'\t' -f1
}
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-g": "gg\n"'
join-lines() {
local item
while read item; do
echo -n "${(q)item} "
done
}
bind-git-helper() {
local char
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
}
bind-git-helper f b t r h g
unset -f bind-git-helper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment