-
-
Save zshihang/d073f03dee894f64eb9a30b64e4954ad to your computer and use it in GitHub Desktop.
Key bindings for fzf
This file contains hidden or 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
# required | |
# ------------- | |
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh | |
export FZF_DEFAULT_COMMAND='fd -t f' | |
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND" | |
export FZF_ALT_C_COMMAND='fd -t d' | |
export FZF_COMPLETION_TRIGGER='' | |
bindkey '^T' fzf-completion | |
bindkey '^I' $fzf_default_completion | |
# git | |
# ------------- | |
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 --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 char | |
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 | |
# tmux | |
# ------------- | |
tm() { | |
[[ -n "$TMUX" ]] && change="switch-client" || change="attach-session" | |
if [ $1 ]; then | |
tmux $change -t "$1" 2>/dev/null || (tmux new-session -d -s $1 && tmux $change -t "$1"); return | |
fi | |
session=$(tmux list-sessions -F "#{session_name}" 2>/dev/null | fzf --exit-0) && tmux $change -t "$session" || echo "No sessions found." | |
} | |
ftpane() { | |
local panes current_window current_pane target target_window target_pane | |
panes=$(tmux list-panes -s -F '#I:#P - #{pane_current_path} #{pane_current_command}') | |
current_pane=$(tmux display-message -p '#I:#P') | |
current_window=$(tmux display-message -p '#I') | |
target=$(echo "$panes" | grep -v "$current_pane" | fzf +m --reverse) || return | |
target_window=$(echo $target | awk 'BEGIN{FS=":|-"} {print$1}') | |
target_pane=$(echo $target | awk 'BEGIN{FS=":|-"} {print$2}' | cut -c 1) | |
if [[ $current_window -eq $target_window ]]; then | |
tmux select-pane -t ${target_window}.${target_pane} | |
else | |
tmux select-pane -t ${target_window}.${target_pane} && | |
tmux select-window -t $target_window | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment