-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtmux-sessionizer
executable file
·65 lines (58 loc) · 1.63 KB
/
tmux-sessionizer
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
#!/usr/bin/env bash
# DESCRIPTION: Tmux session management. Create a new session or switch to the
# existing session by giving a session name and/or a start
# directory, or selecting a start directory via fzf.
# SYNOPSIS: tmux-sessionizer [DIRECTORY]
# tmux-sessionizer [SESSION_NAME]
# tmux-sessionizer [DIRECTORY] [SESSION_NAME]
# tmux-sessionizer [SESSION_NAME] [DIRECTORY]
# List all the parent directories that contain the projects here
read -r -d '' parent_dirs << EOM
$HOME/oj/
$HOME/projects/*/
EOM
# Other scattered project directories
read -r -d '' other_dirs << EOM
$HOME/.config
EOM
if [[ $# -eq 1 ]]; then
if [[ -d $1 ]]; then
directory=$1
else
session_name=$1
directory=$(pwd)
fi
elif [[ $# -eq 2 ]]; then
if [[ -d $1 ]]; then
directory=$1
session_name=$2
else
directory=$2
session_name=$1
fi
else
project_dirs=$(fd --type d --exact-depth 1 . $parent_dirs)
combined_dirs=$(echo -e "$project_dirs\n$other_dirs")
directory=$(echo "$combined_dirs" | fzf --no-multi)
[[ -z $directory ]] && exit 0
fi
if [[ -z $directory || $directory = . ]]; then
directory=$(pwd)
fi
if [[ -z $session_name ]]; then
session_name=$(basename "$directory" | tr . _)
fi
if ! tmux has-session -t="$session_name" 2> /dev/null; then
if [[ -z $TMUX ]]; then
tmux new-session -s "$session_name" -c "$directory"
else
tmux new-session -ds "$session_name" -c "$directory"
tmux switch-client -t "$session_name"
fi
else
if [[ -z $TMUX ]]; then
tmux attach-session -t "$session_name"
else
tmux switch-client -t "$session_name"
fi
fi