-
Notifications
You must be signed in to change notification settings - Fork 0
/
alias.tmux
executable file
·67 lines (54 loc) · 1.71 KB
/
alias.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
#!/usr/bin/env bash
# for debug
#set -x
TMUX_ALIAS_CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
TMUX_ALIAS_ORG_INDEX="$(tmux show-environment -g TMUX_ALIAS_ORG_INDEX 2>/dev/null)"
get_index() {
tmux show-options -q command-alias |\
awk 'END {gsub(/[[:alpha:][:punct:]]/, "", $1); print $1 + 1}'
}
first_init() {
tmux set-environment -g TMUX_ALIAS_ORG_INDEX "$(get_index)"
tmux set-option -qg command-alias["$(get_index)"] \
alias="set-option -g command-alias[\"#($TMUX_ALIAS_CURRENT_DIR/bin/get-index.sh)\"]"
}
reset_idx() {
local top="$(get_index)"
local bot="$((TMUX_ALIAS_ORG_INDEX + 1))"
for i in $(eval echo {$bot..$top}); do
tmux set-option -ug command-alias[$i]
done
}
# Get the absolute path to the users configuration file of TMux.
# This includes a prioritized search on different locations.
#
get_user_tmux_conf() {
# Define the different possible locations.
xdg_location="${XDG_CONFIG_HOME:-$HOME/.config}/tmux/tmux.conf"
default_location="$HOME/.tmux.conf"
# Search for the correct configuration file by priority.
if [ -f "$xdg_location" ]; then
echo "$xdg_location"
else
echo "$default_location"
fi
}
tmux_conf_contents() {
user_config=$(get_user_tmux_conf)
cat /etc/tmux.conf "$user_config" 2>/dev/null
}
alias_list() {
tmux_conf_contents |\
awk -F@ '/^[ \t ]*set(-option)? +-g +@alias/ {dump = $1 "@alias"; gsub(dump, ""); print $0}'
}
setup_alias() {
if [[ $TMUX_ALIAS_ORG_INDEX == '' ]]; then
first_init
else
reset_idx
fi
alias_list | while read -r line; do
eval tmux set-option -g command-alias["$(get_index)"] "$line"
done
}
setup_alias