Skip to content

Instantly share code, notes, and snippets.

@daveknapik
Last active March 7, 2022 02:27
Show Gist options
  • Save daveknapik/1c63ae6ffa904e9fcf5d0a02a9e6b3cd to your computer and use it in GitHub Desktop.
Save daveknapik/1c63ae6ffa904e9fcf5d0a02a9e6b3cd to your computer and use it in GitHub Desktop.
Making fzf and oh-my-zsh and git happy

Step 1: Installations

Install fzf and its key bindings via Homebrew per the instructions here https://github.com/junegunn/fzf

Install bat so fzf can preview files all pretty n' stuff: https://github.com/sharkdp/bat

Step 2: Integrate git

Read https://junegunn.kr/2016/07/fzf-git/ to learn about how this works. Look over junegunn's scripts here: https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236

These did not work with oh-my-zsh for me, but this did: I saved the following code in my root directory as .fzf_git_functions.sh and made it executable.

# GIT heart FZF
# -------------

is_in_git_repo() {
  git rev-parse HEAD > /dev/null 2>&1
}

fzf-down() {
  fzf --height 50% "$@" --border
}

fzf_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/.* -> //'
}

fzf_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) | head -'$LINES |
  sed 's/^..//' | cut -d' ' -f1 |
  sed 's#^remotes/##'
}

fzf_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
}

fzf_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,\}"
}

fzf_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
}

join-lines() {
  local item
  while read item; do
    echo -n "${(q)item} "
  done
}

bind-git-helper() {
  local c
  for c in $@; do
    eval "fzf-g$c-widget() { local result=\$(fzf_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
unset -f bind-git-helper

And then I added source ~/.fzf_git_functions.sh to the bottom of my .zshrc file.

Step 3: Aliases

I added this to my aliases:

alias fzfp="fzf --preview 'bat --style=numbers --color=always {} | head -1000'"

Normally fzf launches without file preview for speed, but for times I want decent file preview via bat settings, there is this.

@versionsix
Copy link

Cool! Thanks for sharing.

I changed the alias to use bat's built in -r, --line-range instead of a pipe and head.

❯ alias fzfp="fzf --preview 'bat --style=numbers --color=always -r :1000 {}'"

@daveknapik
Copy link
Author

@versionsix Sorry for the late reply! Thanks for your more bat-native version. Much appreciated. :)

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