-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib_fzf.sh
191 lines (159 loc) · 4.63 KB
/
lib_fzf.sh
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
_fzf() {
fzf --layout reverse --exact --cycle --height 50% --info hidden --prompt ' ' --border rounded --color light "$@"
}
f-git-show-file() {
local commit file
commit=$(
git log --stat --color=always "$@" |
delta |
fzf --layout reverse --exact --ansi --info hidden --prompt ' ' --border rounded --color light |
awk '{print $2}'
)
[[ -n "$commit" ]] || return
file=$(git show --name-only --pretty=format: "$commit" | _fzf)
git show "$commit" "$file"
}
f-cargo-test() {
local test
test="$(rust-list-tests | _fzf)"
[[ -n "$test" ]] || return
echo cargo test "$test"
print -s "cargo test $test" # zsh
cargo test "$test"
}
f-cat() {
bat --style="header,grid" "$(fd . "$1" | _fzf)"
}
f-vscode() {
code "$(_fzf)"
}
f-docker-exec() {
local container
container="$(
docker ps --format '{{.ID}}\t{{.Names}}\t{{.Image}}' |
_fzf |
awk '{print $1}'
)"
docker exec -it "$container" bash
}
f-emacs() {
emacsclient -n "$(_fzf)"
}
f-git-branch() {
git-branch-by-date | rg -v '^z-' | _fzf | awk '{print $1}'
}
_f-git-log() {
git log --color=always --oneline --decorate | _fzf | awk '{print $1}'
}
f-git-checkout-branch() {
git checkout --quiet "$(f-git-branch)"
}
f-git-checkout-commit() {
git checkout --quiet "$(_f-git-log)"
}
f-git-diff() {
git diff "$(_f-git-log)"
}
f-git-rebase() {
git rebase "$(f-git-branch)"
}
f-git-rebase-interactive() {
git rebase --interactive "$(_f-git-log)"
}
f-git-reset() {
git reset "$(_f-git-log)"
}
f-git-reset-hard() {
git reset --hard "$(_f-git-log)"
}
f-git-revert() {
git revert --no-edit "$(_f-git-log)"
}
f-git-cherry-pick() {
git cherry-pick "$(f-git-branch)"
}
f-git-log-branch() {
git log --stat --decorate "$(f-git-branch)"
}
f-git-show() {
git show "$(_f-git-log)"
}
_f-hist() {
atuin history list | _fzf --no-sort --exact
}
f-hist-cp() {
_f-hist | tr -d "\n" | pbcopy
}
f-hist-x() {
eval "$(_f-hist)"
}
f-kill() {
kill "$@" "$(ps | _fzf | awk '{print $1}')"
}
f-open() {
local app
app="$(command fd -d 1 '.+\.app' /Applications /System/Applications /System/Applications/Utilities |
rg -r '$2$1' '^(.*/([^/]+)\.app)/?$' |
_fzf '--with-nth' 1 '-d' / |
sed -E 's,[^/]+/,/,')"
[ -n "$app" ] && open "$app"
}
f-wormhole-open() {
local project
project="$(command fd -d 1 . ~/src ~/src/temporalio |
rg -r '$2$1' '^(.*/([^/]+))/?$' |
_fzf '--with-nth' 1 '-d' / |
sed -E 's,[^/]+/,/,')"
echo "$project"
[ -n "$project" ] && wormhole-open "$project"
}
f-preview-jq() {
# https://github.com/pawelduda/fzf-live-repl
local file="$1"
echo | _fzf --print-query --preview "jq '{q}' < '$file'" --preview-window "top:95%"
}
f-preview-regex-python() {
local input="$1"
echo | _fzf --print-query --preview-window up --preview "python -c \"
import re
print('Input: $input\n')
m = re.match({q}, '$input')
print(m.groups() if m else '<no match>')\""
}
f-preview-regex-sed() {
local input="$1"
echo | _fzf \
--print-query \
--preview-window up \
--preview "printf \"$input\n\n\n─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n\n\n\"; echo \"$input\" | sed -E 's/\x1b\[[0-9;]*[mK]//g' | sed -E '{q}'"
}
f-rg2() {
local INITIAL_QUERY=""
local RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
FZF_DEFAULT_COMMAND="$RG_PREFIX '$INITIAL_QUERY'" \
fzf --bind "change:reload:$RG_PREFIX {q} || true" \
--ansi --query "$INITIAL_QUERY" \
--height=100% --layout=reverse
}
fzf-set-environment-variables() {
export FZF_DEFAULT_COMMAND="fd --type file --color=always"
export FZF_DEFAULT_OPTS="--ansi"
}
f-tmux() {
if [ -n "$1" ]; then
local next_window="$1"
else
local next_window
next_window="$(
tmux list-panes -a -F '#{session_name},#{window_index},#{pane_title}' |
xsv table |
_fzf --exact |
awk '{print $1":"$2}'
)"
fi
tmux display-message -p -F '#{session_name}:#{window_index}' >/tmp/tmux-last-window
tmux switch-client -t "$next_window"
}
tmux-back() {
f-tmux "$(cat /tmp/tmux-last-window)"
}