-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmuxz
executable file
·52 lines (46 loc) · 1.69 KB
/
tmuxz
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
#!/usr/bin/env bash
# Exit with message, if no variable is given
if [ -z $1 ]; then
echo 'Given no session or directory name to attach to.' >&2
exit 1
fi
# Query directory path, save directory name and check if session with that name exists
zoxide_result_path="$( zoxide query $1 )"
# If zoxide query can't find a directory or gets any other error, script exits
if [ -z $zoxide_result_path ]; then
exit 1
fi
# Save directory name and check if session with that name exists
zoxide_result="$( echo "${zoxide_result_path}" | awk -F / '{print $NF}' )"
session_exists="$( tmux ls -F \#S | grep -x "${zoxide_result}" )"
# Tmux doesn't allow session names with dot in the beginning.
# If trying to get to dot directory, replace dot with underscore.
if [[ $zoxide_result = .* ]]; then
zoxide_result="${zoxide_result/./_}"
fi
# If no session exists, create one, and if in session already, switch to it.
if [ -z $session_exists ]; then
# Shows the path session would be created in and asks confirmation
echo "You are about to open a tmux session in ${zoxide_result_path}."
read -p 'Do you want to continue? [Y/n]' -n 1 -r
echo
if [[ "$REPLY" =~ ^[Yy]$ ]] || [ -z $REPLY ]; then
cd $zoxide_result_path
tmux new-session -ds $zoxide_result
if [ -z $TMUX ]; then
tmux attach-session -t $zoxide_result
else
tmux switch-client -t $zoxide_result
fi
else
echo 'Aborted session creation.'
exit 0
fi
# If session exists, attach to it, and if in session already, switch to it.
else
if [ -z $TMUX ]; then
tmux attach-session -t $zoxide_result
else
tmux switch-client -t $zoxide_result
fi
fi