-
Notifications
You must be signed in to change notification settings - Fork 3
/
autocomplete-tmux
executable file
·70 lines (57 loc) · 1.93 KB
/
autocomplete-tmux
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
#!/bin/bash
fifo_in=$(mktemp -u --suffix=".autocomplete-tmux.in")
fifo_out=$(mktemp -u --suffix=".autocomplete-tmux.out")
trap "rm $fifo_in $fifo_out" EXIT
mkfifo "$fifo_in" "$fifo_out"
candidates=("$(
tmux ls -F '#S' \
| xargs -n1 tmux capture-pane -pt \
| grep -wPo '[\w/:.@~_-]{5,}' \
| grep -vPx '\d\d[:/-]\d\d([:/-]\d\d)?' \
| grep -vPx '[a-z]{5}' \
| grep -P '^[\w/~]' \
| grep -vPx '[A-Z][a-z]+' \
| grep -vP '@conference' \
| grep -vP '\.xmpp' \
| grep -P '[\w/]$' \
| sort \
| uniq \
| {
tee \
>(grep -Px '[\w_-]{7,}') \
>(grep -P '^[\w.]+@') \
>(grep '^/') \
>(grep '^~') \
>(grep -P '://') \
>(grep -Px '([\da-f]+:)+[\da-f]+') \
>(grep -Po '(\d{1,3}\.){3}\d{1,3}/\d{1,4}') \
>(grep -Po '(\d{1,3}\.){3}\d{1,3}') \
>/dev/null
}
)")
window_pid=$(xdotool getactivewindow getwindowpid)
window_command=$(ps -o comm= "$window_pid")
filter_command=("modal")
paste_command=("xdotool type --clearmodifiers --delay 1 --file -")
if [ "$window_command" = "urxvt" ]; then
read terminal_command_pid terminal_command \
<<< "$(ps -o pid=,comm= --ppid="$window_pid")"
if [[ "$terminal_command" =~ ^tmux.* ]]; then
pane_id=$(
tmux list-clients -F '#{client_pid} #{pane_id}' \
| awk "\$1 == $terminal_command_pid { print \$2 }"
)
filter_command=(
"tmux split-window -h -p 40 -t $pane_id"
'"' "bash -c 'fzf > $fifo_out < $fifo_in'" '"'
"&& cat $fifo_out &"
"tee $fifo_in >/dev/null"
)
paste_command=('tmux send "$(cat)"')
fi
fi
choice=$(
{ eval "${filter_command[@]}"; } <<< "${candidates[@]}" \
| head -n1
)
eval "${paste_command[@]}" <<< "$choice"