-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtmux_sessions.sh
More file actions
executable file
·49 lines (40 loc) · 986 Bytes
/
Copy pathtmux_sessions.sh
File metadata and controls
executable file
·49 lines (40 loc) · 986 Bytes
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
#!/usr/bin/env bash
set -euo pipefail
PROJECTS_DIR="${PROJECTS_DIR:-$HOME/projects}"
fzf_opts=(
--border-label ' projects '
--delimiter /
--with-nth -1
--prompt 'open project > '
)
switch_or_attach() {
local session_name=$1
if [[ -n ${TMUX:-} ]]; then
tmux switch-client -t "=$session_name"
else
tmux attach-session -t "=$session_name"
fi
}
if [[ $# -eq 1 ]]; then
selected=$1
else
selected=$(
find "$PROJECTS_DIR" -mindepth 1 -maxdepth 1 -type d -not -path '*_archived*' |
sort -df |
fzf "${fzf_opts[@]}"
) || exit 0
fi
if [[ -z $selected ]]; then
exit 0
fi
selected_name=$(basename "$selected" | sed 's/[^A-Za-z0-9_-]/_/g')
if tmux has-session -t "=$selected_name" 2> /dev/null; then
switch_or_attach "$selected_name"
else
if [[ -z ${TMUX:-} ]]; then
tmux new-session -s "$selected_name" -c "$selected"
else
tmux new-session -ds "$selected_name" -c "$selected"
tmux switch-client -t "=$selected_name"
fi
fi