Skip to content

Instantly share code, notes, and snippets.

@AndrewSouthpaw
Created January 14, 2022 20:32
Show Gist options
  • Save AndrewSouthpaw/18e0257b4d731260118f426275f18efb to your computer and use it in GitHub Desktop.
Save AndrewSouthpaw/18e0257b4d731260118f426275f18efb to your computer and use it in GitHub Desktop.
Configures fzf (fuzzy-finder) tool with git operations.
# custom aliases for fzf
# ======================
# open up a visual finder with git branches to switch to them, including fuzzy-finding
# demo: https://i.imgur.com/xR5ILE5.jpg
alias gf='git checkout $(_gb)'
# opens visual fzf finder for git branches to delete them
alias gbd='git bd $(_gb)'
# fzf
[ -f ~/.fzf.bash ] && source ~/.fzf.bash
export FZF_DEFAULT_OPTS="--extended"
export FZF_DEFAULT_COMMAND="fd --type f"
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
# Use fd to generate the list for directory completion
_fzf_compgen_dir() {
fd --type d --hidden --follow --exclude ".git" . "$1"
}
# GIT heart FZF
# https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236
# -------------
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 {}'
}
_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' |
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}' |
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
}
if [[ $- =~ i ]]; then
bind '"\er": redraw-current-line'
# these bind the various commands (like _gf, _gb, etc.) to keyboard shortcuts.
# Look to the function definitions to figure out what each one does.
# e.g. C-g\C-f means type Ctrl+G then Ctrl+F.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment