-
Notifications
You must be signed in to change notification settings - Fork 18
/
fzf_bash.sh
686 lines (619 loc) · 18.7 KB
/
fzf_bash.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# shellcheck disable=SC2148,SC2317
# Note: this also uses several rust binaries: fd, rg, bat, lsd and devicon-lookup
# Setup {{{
# Set base dir and copy command
base_pkg_dir='/usr'
if [[ "$OSTYPE" == 'darwin'* ]]; then
if type "brew" > /dev/null 2>&1; then
base_pkg_dir=$(brew --prefix)
else
base_pkg_dir='/usr/local'
fi
COPY_CMD='pbcopy'
else
COPY_CMD='xsel --clipboard'
fi
# Enable completions
if [[ $- == *i* ]]; then
if [[ "$OSTYPE" == 'darwin'* ]]; then
completion_base_dir="$base_pkg_dir/opt/fzf/shell"
else
completion_base_dir="$base_pkg_dir/share/fzf"
fi
# shellcheck disable=SC1091
. "$completion_base_dir/completion.bash" 2> /dev/null
fi
# }}}
# Options {{{
# Change default options and colors
export FZF_DEFAULT_OPTS='
--border="bottom"
--border-label-pos=50
--height 15
--inline-info
--prompt=" "
--pointer=">"
--info="inline: "
--marker=" "
--no-separator
--preview-window="border-left"
--bind=ctrl-space:toggle+up,ctrl-d:half-page-down,ctrl-u:half-page-up
--bind=alt-v:toggle-preview,alt-j:preview-down,alt-k:preview-up
--bind=alt-d:preview-half-page-down,alt-u:preview-half-page-up
--color=bg+:#282c34,bg:#24272e,fg:#abb2bf,fg+:#abb2bf,hl:#528bff,hl+:#528bff
--color=prompt:#c678dd,header:#566370,info:#5c6370,pointer:#c678dd
--color=marker:#d19a66,spinner:#e06c75,border:#282c34,label:#566370
'
# Override FZF stock commands (ctrl-t,alt-c) and their options
export FZF_DEFAULT_COMMAND="fd --type f --hidden --follow --exclude .git \
--color=always --strip-cwd-prefix"
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_CTRL_T_OPTS="
--border-label='Find Files'
--multi
--ansi
--bind 'ctrl-y:execute-silent(echo -n {+2} | $COPY_CMD)+abort'
--preview='
if file --mime-type {2} | grep -qF image/; then
kitty icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --place=\${FZF_PREVIEW_COLUMNS}x\${FZF_PREVIEW_LINES}@0x0 {2}
else
bat --line-range :200 {2}
fi'
--expect=tab,ctrl-t,ctrl-o,alt-c,alt-p,alt-f,alt-g
--header='enter=edit, tab=insert, C-t=fzf-files, C-o=open, A-c=cd-file-dir, \
A-p=parent-dirs, A-f=ranger, A-g=grep, C-y=yank'
"
export FZF_ALT_C_COMMAND='fd --type d --hidden --follow --exclude .git --strip-cwd-prefix'
FZF_ALT_C_OPTS_BASE="
--border-label='Find Dirs'
--no-multi
--expect=ctrl-o,ctrl-t,alt-c,alt-p,alt-f,alt-g
--header='enter=fzf-files, C-o=cd, A-c=fzf-dirs, A-p=parent-dirs, \
A-f=ranger, A-g=grep, C-y=yank'
"
export FZF_ALT_C_OPTS="$FZF_ALT_C_OPTS_BASE
--bind 'ctrl-y:execute-silent(echo -n {2..} | $COPY_CMD)+abort'
--preview 'lsd -F --tree --depth 2 --color=always --icon=always {2} | head -200'
"
# Completions
export FZF_COMPLETION_TRIGGER='jk'
complete -F _fzf_path_completion -o default -o bashdefault v o dog
# }}}
# Bindings/Functions {{{
# Helpers {{{
if [[ -o vi ]]; then
# Bind unused key, "\C-x\C-a", to enter vi-movement-mode quickly and then
# use that thereafter
bind '"\C-x\C-a": vi-movement-mode'
# Define other bindings to be used in mappings
bind '"\C-x\C-e": shell-expand-line'
bind '"\C-x\C-r": redraw-current-line'
bind '"\C-x^": history-expand-line'
else
# To refresh the prompt after fzf
bind '"\er": redraw-current-line'
bind '"\e^": history-expand-line'
fi
# }}}
# File and dirs {{{
# ls-like {{{
FZF_LL_OPTS="
--ansi
--bind 'ctrl-y:execute-silent(echo -n {-1} | $COPY_CMD)+abort'
--header='enter=open, C-y=yank'
"
ll() {
cmd="lsd -F -lah --color=always"
# shellcheck disable=SC2294
out="$(eval "$cmd" "$@" |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_LL_OPTS" fzf)"
key=$(head -1 <<< "$out")
res=$(head -2 <<< "$out" | tail -1)
if [[ -z "$res" ]]; then
return 1
else
curr_dir="$(realpath "$PWD")"
res=$(echo "$res" | rev | tr -s ' ' | cut -d ' ' -f 1 | rev)
path="$curr_dir/$res"
if [[ -d "$path" ]]; then
cmd="cd"
else
cmd="${EDITOR:-nvim}"
fi
fi
case "$key" in
**)
eval "$(printf "%s %s" "$cmd" "$path")"
;;
esac
}
# }}}
# Files {{{
# Custom Ctrl-t mapping (also Alt-t to ignore git-ignored files)
__fzf_select_custom__() {
local cmd dir
cmd="$FZF_CTRL_T_COMMAND"
if [[ "$1" == 'no-ignore' ]]; then
cmd="$cmd --no-ignore-vcs"
fi
if [[ "$2" ]]; then
cmd=${cmd//$"--strip-cwd-prefix"/} # Path doesn't work with strip-cwd-prefix
cmd="$cmd . $2" # use narrow dir
fi
out=$(eval "$cmd" | devicon-lookup --color |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" fzf)
key=$(head -1 <<< "$out")
mapfile -t _files <<< "$(tail -n+2 <<< "$out")"
if [ ${#_files[@]} -eq 1 ] && [[ -z "${_files[0]}" ]]; then
return 1
else
files=()
for f in "${_files[@]}"; do
files+=("${f#* }")
done
fi
printf -v files_str "%s " "${files[@]}"
case "$key" in
tab)
printf '%q ' "${files[@]}"
;;
ctrl-t)
__fzf_select_custom__ "no-ignore" "$(dirname "${files[0]}")"
;;
ctrl-o)
printf 'open %q' "${files[0]}"
;;
alt-c)
printf 'cd %q' "$(dirname "${files[0]}")"
;;
alt-p)
__fzf_cd_parent__ "$(dirname "${files[0]}")"
;;
alt-f)
printf 'ranger --selectfile %q' "${files[0]}"
;;
alt-g)
ig "$(dirname "${files[0]}")"
;;
*)
printf '%s %s' "${EDITOR:-nvim}" "$files_str"
;;
esac
}
fzf-file-widget-custom() {
local selected=""
selected="$(__fzf_select_custom__ "$1" "$2")"
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=$((READLINE_POINT + ${#selected}))
}
# Note this will insert output to the prompt and there is no way to choose to
# execute it instead: https://github.com/junegunn/fzf/issues/477
bind -m emacs-standard -x '"\C-t": "fzf-file-widget-custom"'
bind -m emacs-standard -x '"\et": "fzf-file-widget-custom no-ignore"'
bind -m vi-command -x '"\C-t": fzf-file-widget-custom'
bind -m vi-command -x '"\et": fzf-file-widget-custom no-ignore'
bind -m vi-insert -x '"\C-t": fzf-file-widget-custom'
bind -m vi-insert -x '"\et": fzf-file-widget-custom no-ignore'
# }}}
# Dirs {{{
# Helper that defines actions for keys in directory-like maps
__fzf_cd_action_key__() {
out="$1"
key=$(head -1 <<< "$out")
dir=$(head -2 <<< "$out" | tail -1)
if [[ -z "$dir" ]]; then
return 1
else
dir="${dir##* }"
fi
case "$key" in
ctrl-o)
printf 'cd %q' "$dir"
;;
alt-f)
printf 'ranger %q' "$dir"
;;
alt-c)
__fzf_cd_custom__ "no-ignore" "$dir"
;;
alt-p)
__fzf_cd_parent__ "$dir"
;;
alt-g)
ig "$dir"
;;
*)
__fzf_select_custom__ "no-ignore" "$dir"
;;
esac
}
# Custom Alt-c maps (also Alt-d to ignore git-ignored dirs)
__fzf_cd_custom__() {
local cmd dir
cmd="$FZF_ALT_C_COMMAND"
if [[ "$1" == 'no-ignore' ]]; then
cmd="$cmd --no-ignore-vcs"
fi
if [[ "$2" ]]; then
cmd=${cmd//$"--strip-cwd-prefix"/} # Path doesn't work with strip-cwd-prefix
cmd="$cmd . $2" # use narrow dir
fi
out=$(eval "$cmd" | devicon-lookup |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" fzf)
__fzf_cd_action_key__ "$out"
}
if [[ -o vi ]]; then
# shellcheck disable=SC2016
bind '"\ec": "\C-x\C-addi`__fzf_cd_custom__`\C-x\C-e\C-x\C-r\C-m"'
bind -m vi-command '"\ec": "i\ec"'
# shellcheck disable=SC2016
bind '"\ed": "\C-x\C-addi`__fzf_cd_custom__ no-ignore`\C-x\C-e\C-x\C-r\C-m"'
bind -m vi-command '"\ed": "i\ed"'
else
# shellcheck disable=SC2016
bind '"\ec": " \C-e\C-u`__fzf_cd_custom__`\e\C-e\er\C-m"'
# shellcheck disable=SC2016
bind '"\ed": " \C-e\C-u`__fzf_cd_custom__ no-ignore`\e\C-e\er\C-m"'
fi
# Alt-h map to cd from home dir
if [[ -o vi ]]; then
# shellcheck disable=SC2016
bind '"\eh": "\C-x\C-addi`__fzf_cd_custom__ no-ignore ~`\C-x\C-e\C-x\C-r\C-m"'
bind -m vi-command '"\eh": "i\eh"'
else
# shellcheck disable=SC2016
bind '"\eh": " \C-e\C-u`__fzf_cd_custom__ no-ignore ~`\e\C-e\er\C-m"'
fi
# Alt-p mapping to cd to selected parent directory (sister to Alt-c)
__fzf_cd_parent__() {
local dirs=()
get_parent_dirs() {
if [[ -d "${1}" ]]; then
dirs+=("$1")
else
return
fi
if [[ "${1}" == '/' ]]; then
for _dir in "${dirs[@]}"; do
echo "$_dir"
done
else
get_parent_dirs "$(dirname "$1")"
fi
}
start_dir="$(dirname "$PWD")"
cmd="get_parent_dirs $(realpath "${1:-$start_dir}")"
out=$(eval "$cmd" | devicon-lookup |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" fzf)
__fzf_cd_action_key__ "$out"
}
if [[ -o vi ]]; then
# shellcheck disable=SC2016
bind '"\ep": "\C-x\C-addi`__fzf_cd_parent__`\C-x\C-e\C-x\C-r\C-m"'
bind -m vi-command '"\ep": "i\ep"'
else
# shellcheck disable=SC2016
bind '"\ep": " \C-e\C-u`__fzf_cd_parent__`\e\C-e\er\C-m"'
fi
# }}}
# Zoxide {{{
export FZF_ALT_ZOXIDE_OPTS="$FZF_ALT_C_OPTS_BASE
--bind 'ctrl-y:execute-silent(echo -n {3..} | $COPY_CMD)+abort'
--no-sort
--preview 'lsd -F --tree --depth 2 --color=always --icon=always {3} | head -200'
--border-label='Zoxide Dirs'
"
zoi() {
cmd="zoxide query --list --score 2>&1"
out="$(eval "$cmd" | devicon-lookup -r '\d +(.*)$' -s |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_ALT_ZOXIDE_OPTS" fzf |
sed 's/^\W\s[0-9,.]* *//')"
__fzf_cd_action_key__ "$out"
}
if [[ -o vi ]]; then
# shellcheck disable=SC2016
bind '"\ez": "\C-x\C-addi`zoi`\C-x\C-e\C-x\C-r\C-m"'
# shellcheck disable=SC2016
bind -m vi-command '"\ez": "ddi`zoi`\C-x\C-e\C-x\C-r\C-m"'
else
# shellcheck disable=SC2016
bind '"\ez": " \C-e\C-u`zoi`\e\C-e\er\C-m"'
fi
# }}}
# }}}
# Grep {{{
FZF_GREP_OPTS="
--border-label='Live Grep'
--header 'enter=open, alt-r=refine-search'
--multi
--ansi
--disabled
--query=''
--delimiter=:
"
ig() {
# shellcheck disable=SC2124
grep_cmd="rg --smart-case --vimgrep --no-heading --color=always --trim {q} $@"
grep_cmd+=" | devicon-lookup --color --prefix :"
# shellcheck disable=SC2016,SC1004
preview_cmd='bat --highlight-line {2} $(echo {1} | sed "s/[^ ] //")'
# shellcheck disable=SC2154
out=$(eval "true" | FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_GREP_OPTS" \
fzf --bind "change:reload:$grep_cmd || true" --preview "$preview_cmd" \
--bind "alt-r:unbind(change,alt-r)+change-border-label(Find Word)+enable-search+clear-query" \
--preview-window "+{2}-/2")
key=$(head -1 <<< "$out")
mapfile -t _files <<< "$(head -2 <<< "$out")"
if [[ ${#_files[@]} -eq 1 ]] && [[ -z "${_files[0]}" ]]; then
return 1
else
files=()
for f in "${_files[@]}"; do
# We need real path for vim to work (the first line removes devicon
# icon from the filename)
f="${f#* }"
file="$(realpath "$(echo "$f" | cut -d ':' -f 1)")"
line_nr=$(echo "$f" | cut -d ':' -f 2)
files+=("+'e +$line_nr $file'")
done
fi
printf -v files_str "%s " "${files[@]}"
# When calling this with a subdir arg we seem to need echo instead of eval
if [[ -d "$1" ]]; then
cmd="echo"
else
cmd="eval"
fi
$cmd "$(printf "${EDITOR:-nvim} %s" "$files_str")"
}
if [[ -o vi ]]; then
# shellcheck disable=SC2016
bind '"\eg": "\C-x\C-addiig\C-x\C-e\C-x\C-r\C-m"'
bind -m vi-command '"\eg": "i\eg"'
else
# shellcheck disable=SC2016
bind '"\eg": " \C-e\C-u`ig`\e\C-e\er\C-m"'
fi
# }}}
# History {{{
export FZF_CTRL_R_OPTS="
--border-label='Command History'
--bind 'ctrl-y:execute-silent(echo -n {2..} | $COPY_CMD)+abort,tab:accept'
--header 'enter=insert, tab=insert, C-y=yank'
--nth=2..,..
--tiebreak=index
--no-multi
--read0
"
__fzf_history__() {
local output
output=$(
builtin fc -lnr -2147483648 |
last_hist=$(HISTTIMEFORMAT='' builtin history 1) perl -n -l0 -e 'BEGIN { getc; $/ = "\n\t"; $HISTCMD = $ENV{last_hist} + 1 } s/^[ *]//; print $HISTCMD - $. . "\t$_" if !$seen{$_}++' |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_CTRL_R_OPTS" fzf --query "$READLINE_LINE"
) || return
READLINE_LINE=${output#*$'\t'}
if [ -z "$READLINE_POINT" ]; then
echo "$READLINE_LINE"
else
READLINE_POINT=0x7fffffff
fi
}
bind -m emacs-standard -x '"\C-r": __fzf_history__'
bind -m vi-command -x '"\C-r": __fzf_history__'
bind -m vi-insert -x '"\C-r": __fzf_history__'
# }}}
# Tmux {{{
FZF_TMUX_OPTIONS="
--border-label='Tmux Sessions'
--multi
--exit-0
--expect=alt-k,alt-r,enter
--header='enter=switch, A-k=kill, A-r=rename'
--preview='tmux_tree {} | bat --style plain'
"
tms() {
[[ -n "$TMUX" ]] && change="switch-client" || change="attach-session"
if [[ -n "$1" ]]; then
if [[ "$1" == "-ask" ]]; then
read -r -p "New tmux session name: " session_name
else
session_name="$1"
fi
tmux "$change" -t "$session_name" 2> /dev/null ||
(tmux -f "$HOME/.tmux/tmux.conf" new-session -d -s "$session_name" &&
tmux "$change" -t "$session_name")
return
fi
# If no arg is given use fzf to choose a session to switch or kill
cmd='tmux list-sessions -F "#{session_name}"'
out=$(eval "$cmd" | FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_TMUX_OPTIONS" fzf)
key=$(head -1 <<< "$out")
mapfile -t sessions <<< "$(tail -n+2 <<< "$out")"
case "$key" in
alt-k)
for s in "${sessions[@]}"; do
tmux kill-session -t "$s"
done
;;
alt-r)
for s in "${sessions[@]}"; do
read -r -p "Rename tmux session '$s' to: " new_session_name
tmux rename-session -t "$s" "$new_session_name"
done
;;
enter)
tmux "$change" -t "${sessions[0]}"
;;
esac
}
# }}}
# Bluetooth {{{
FZF_BT_OPTS="
--border-label='Bluetooth Control'
--multi
--tac
--bind 'ctrl-y:execute-silent(echo -n {2} | $COPY_CMD)+abort,tab:accept'
--expect=alt-t,alt-u,alt-p,alt-d,alt-r
--header='enter=connect, A-t=trust, A-u=untrust, A-p=pair,
A-d=disconnect, A-r=remove/unpair, C-y=yank'
--with-nth=3..
--preview 'bluetoothctl info {2} | bat --style plain -H 6 -H 7 -H 9'
"
bt() {
cmd="bluetoothctl devices"
out=$(eval "$cmd" |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_BT_OPTS" fzf |
cut -d ' ' -f 2)
key=$(head -1 <<< "$out")
mapfile -t devices <<< "$(tail -n+2 <<< "$out")"
if [[ ${#devices[@]} -eq 1 ]] && [[ -z "${devices[0]}" ]]; then
return 1
fi
case "$key" in
alt-p)
sub_cmd="pair"
;;
alt-t)
sub_cmd="trust"
;;
alt-u)
sub_cmd="untrust"
;;
alt-d)
sub_cmd="disconnect"
;;
alt-r)
sub_cmd="remove"
;;
*)
sub_cmd="connect"
;;
esac
for d in "${devices[@]}"; do
bluetoothctl "$sub_cmd" "$d"
done
}
# }}}
# Git {{{
# Forgit (git and fzf)
export FORGIT_COPY_CMD="$COPY_CMD "
export FORGIT_NO_ALIASES="1"
export FORGIT_FZF_DEFAULT_OPTS="--preview-window='right'"
# shellcheck disable=SC2016
export FORGIT_LOG_FZF_OPTS='
--header="enter=view, C-o=nvim, C-y=yank"
--bind="ctrl-y:execute-silent(echo {} | grep -Eo [a-f0-9]+ | head -1 | tr -d '"'\n'"' | $FORGIT_COPY_CMD)"
--bind="ctrl-o:execute(echo {} | grep -Eo [a-f0-9]+ | head -1 | xargs git show | nvim -)"
'
alias gcb='git-forgit checkout_branch'
alias gdb='git-forgit branch_delete'
alias gl='git-forgit log'
alias glg='FORGIT_LOG_GRAPH_ENABLE=false git-forgit log'
alias gd='git-forgit diff'
alias ga='git-forgit add'
alias gsv='git-forgit stash_show'
# }}}
# Docker {{{
FZF_DOCKER_OPTS_BASE="
--multi
--exit-0
--bind 'ctrl-y:execute-silent(echo -n {1} | $COPY_CMD)+abort'
"
FZF_DOCKER_IMAGE_OPTS="$FZF_DOCKER_OPTS_BASE
--border-label='Docker Images'
--expect=ctrl-i,alt-d
--header='enter=run, C-i=interactive, A-d=rm, C-y=yank'
"
di() {
cmd="docker image ls | tail -n +2"
out=$(eval "$cmd" |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_DOCKER_IMAGE_OPTS" fzf)
key=$(head -1 <<< "$out")
out="$(echo "$out" | awk '{print $3}')"
mapfile -t images <<< "$(tail -n+2 <<< "$out")"
if [[ ${#images[@]} -eq 1 ]] && [[ -z "${images[0]}" ]]; then
return 1
fi
case "$key" in
ctrl-i)
image_cmd="docker run --rm -ti --entrypoint /bin/bash"
;;
alt-d)
image_cmd="docker image rm --force"
;;
**)
image_cmd="docker run -td"
;;
esac
for image in "${images[@]}"; do
eval "$image_cmd $image"
done
}
FZF_DOCKER_CONTAINER_OPTS="$FZF_DOCKER_OPTS_BASE
--border-label='Docker Containers'
--expect=ctrl-a,ctrl-e,ctrl-s,ctrl-r,ctrl-b,alt-k,alt-d
--header='enter=logs, C-e=exec, C-a=attach, C-b=start, C-s=stop, C-r=restart, \
A-k=kill, A-d=rm'
"
dc() {
cmd="docker container ls -a | tail -n +2"
out=$(eval "$cmd" |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_DOCKER_CONTAINER_OPTS" fzf |
cut -d ' ' -f 1)
key=$(head -1 <<< "$out")
mapfile -t containers <<< "$(tail -n+2 <<< "$out")"
if [[ ${#containers[@]} -eq 1 ]] && [[ -z "${containers[0]}" ]]; then
return 1
fi
post_cmd=''
case "$key" in
ctrl-a)
sub_cmd="attach"
;;
ctrl-e)
sub_cmd="exec -ti"
post_cmd="/bin/bash"
;;
ctrl-s)
sub_cmd="stop"
;;
ctrl-b)
sub_cmd="start"
;;
ctrl-r)
sub_cmd="restart"
;;
alt-k)
sub_cmd="kill"
;;
alt-d)
sub_cmd="rm"
;;
**)
sub_cmd="logs"
;;
esac
for container in "${containers[@]}"; do
eval "docker container $sub_cmd $container $post_cmd"
done
}
# }}}
# Man (Search) {{{
FZF_MAN_OPTS='
--border-label="Man"
--header "enter=open"
--preview="man -Pcat {1} 2>/dev/null | bat -l man --color always --style numbers"
'
m() {
query_str="$1"
[ -n "$query_str" ] && man "$query_str" && return 0 ||
manual=$(
apropos . | grep -v -E '^.+ \(0\)' | awk '{print $1 " "$2}' |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_MAN_OPTS" fzf --query "$query_str" | awk '{print $1}'
)
[ -z "$manual" ] && return 1
man "$manual"
}
# }}}
# }}}