-
Notifications
You must be signed in to change notification settings - Fork 0
/
.fzf_helpers
101 lines (86 loc) · 2.9 KB
/
.fzf_helpers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
##
#
# FZF - The Holiest of Holies
#
##
export FZF_COMPLETION_TRIGGER=','
# Helpers first
join-lines() {
local item
while read item; do
echo -n "${(q)item} "
done
}
is_in_git_repo() {
git rev-parse HEAD > /dev/null 2>&1
}
#
# Show hashes with control-g-h (like tig)
#
_hashes_gh() {
is_in_git_repo || return
git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h %s %C(blue)(%an)" --graph --color=always |
fzf-tmux --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-gh-widget() LBUFFER+=$(_hashes_gh | join-lines)
zle -N fzf-gh-widget
bindkey '^g^h' fzf-gh-widget
#
# Find docker images
#
_docker_images() {
docker image ls |
fzf-tmux --ansi --no-sort --reverse | awk '{print $3}'
}
fzf-di-widget() LBUFFER+=$(_docker_images | join-lines)
zle -N fzf-di-widget
bindkey '^g^i' fzf-di-widget
#
# Find files that have changed
#
_changed_files() {
is_in_git_repo || return
git ls-files -m -o --exclude-standard | fzf-tmux --no-sort --reverse --height 10 -m
}
fzf-gy-widget() {
LBUFFER+=$(_changed_files | join-lines)
zle reset-prompt
}
zle -N fzf-gy-widget
bindkey '^g^y' fzf-gy-widget
# FZF WITH PREVIEW (Try highlight, then fall back to cat)
export FZF_CTRL_T_OPTS='--preview "[[ $(file --mime {}) =~ binary ]] && \
echo {} is a binary file || \
(highlight -O ansi -l {} || cat {}) 2> /dev/null | \
head -500"'
# lastpass
pass() {
lpass show lucidchart.com >/dev/null # dummy get to cache the password
lpass show -c --password $(lpass ls | fzf | awk '{print $(NF)}' | sed 's/\]//g')
}
# Tmux manager
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."
}
# git find - general purpose checkout tool
gfa() {
local tags local_branches remote_branches target
tags=$(git tag | awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') || return
local_branches=$(git branch | grep -v HEAD | sed "s/.* //" |
awk '{print "\x1b[32;1mbranch\x1b[m\t" $1}') || return
remote_branches=$(git branch --remotes | grep -v HEAD |
sed "s/.* //" | sed "s#remotes/[^/]*/##" |
awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
target=$((echo "$local_branches"; echo "$tags"; echo "$remote_branches") |
fzf --no-hscroll --no-multi --delimiter="\t" -n 2 --ansi \
--preview="git show --color=always $(echo {+2..})") || return
# ${target} has origin/ in front. we use or # for prefix of // for substring
git checkout $(echo "${target//origin\/}" | awk '{print $2}')
}