FZF: adds git bindings and toggle preview

I have added a default binging of ctrl+/ to toggle the preview in fzf

I have added some git key bindings for git stuff. They are all in the
form with ctrl+g ctrl+<something>. Most are what they want, for example
branches are ctrl+g ctrl+b however commits are not. I assume this is
because I can't overwrite what ctrl+c does, even if prefixed. I will
look into this at some point but for now commits are ctrl+g ctrl+h.

Largely stolen the code from here:
although with minor tweaks.

I might change the initial ctrl+g binding as it is awkward on colemak
although we'll see how often I use it.
Jonathan Hodgson 9 months ago
parent 0c0a7f81ec
commit ab27433fc4
  1. 78

@ -1,6 +1,6 @@
# this rg command will get a list of files that are not in gitignore or similar
export FZF_DEFAULT_COMMAND="fd --type f --hidden --follow --color=always --exclude .git --exclude .PlayOnLinux --exclude \"PlayOnLinux\'s virtual drives\""
export FZF_DEFAULT_OPTS="--reverse --ansi --height 40% --bind change:top"
export FZF_DEFAULT_OPTS="--reverse --ansi --height 40% --bind change:top --bind ctrl-/:toggle-preview"
export FZF_CTRL_R_OPTS=""
export FZF_CTRL_T_OPTS="$FZF_DEFAULT_OPTS --ansi --preview \"fzf-preview {}\""
export FZF_ALT_C_OPTS="$FZF_DEFAULT_OPTS --ansi --preview \"fzf-preview {}\""
@ -76,4 +76,80 @@ if [ "$sourced" = "True" ]; then
export FZF_CTRL_T_COMMAND="fzf_crl_t"
export FZF_ALT_C_COMMAND="fzf_crl_t true"
is_in_git_repo() {
git rev-parse HEAD > /dev/null 2>&1
# Files
_gf() {
is_in_git_repo || return
git -c color.status=always status --short . |
fzf -m --ansi --nth 2..,.. \
--preview '(git diff --color=always -- {-1} | sed 1,4d; cat {-1})' |
cut -c4- | sed 's/.* -> //'
# Branches
_gb() {
is_in_git_repo || return
git branch -a --color=always | grep -v '/HEAD\s' | sort |
fzf --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/##'
# Tags
_gt() {
is_in_git_repo || return
git tag --sort -version:refname |
fzf --multi --preview-window right:70% \
--preview 'git show --color=always {}'
# Commit
_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 --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,\}"
# Remote
_gr() {
is_in_git_repo || return
git remote -v | awk '{print $1 "\t" $2}' | uniq |
fzf --tac \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" {1}' |
cut -d$'\t' -f1
# Stash
_gs() {
is_in_git_repo || return
git stash list | fzf --reverse -d: --preview 'git show --color=always {1}' |
cut -d: -f1
join-lines() {
local item
while read item; do
echo -n "${(q)item} "
bind-git-helper() {
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"
bind-git-helper f b t r h s
unset -f bind-git-helper