-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmux-sessionizer.sh
executable file
·61 lines (52 loc) · 1.61 KB
/
tmux-sessionizer.sh
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
#!/usr/bin/env bash
# I organize my projects in ~/Developer/org/name, and I'd like to create sessions for each of them.
# This script uses a combination of find, zoxide (so they are sort by frecency) and fzf to list and filter project folders.
# Then, it either switch to an existing tmux session for that project, or create a new one and switch to it.
#
# Stole and modified from: https://github.com/ThePrimeagen/.dotfiles/blob/master/bin/.local/bin/tmux-sessionizer
PROJECTS="$HOME/projects $(echo ~/projects/*) $HOME"
# increase frecency
increase() {
selected="$1"
if [ "$selected" = default ]; then
return 0
fi
# zoxide add "$PROJECTS/$selected"
zoxide add "$selected"
}
search() {
find $PROJECTS -maxdepth 1 -type d |
while read -r p; do
zoxide query -l -s "$p/"
done |
sort -rnk1 | # sort by score
uniq | # dedup
awk '{print $2}' | # do not actually print the score
fzf --no-sort --prompt " "
}
if [ "$#" -eq 1 ]; then
selected="$1"
else
selected=$(search)
fi
if test -z $selected; then
exit 0
fi
selected_name="$(echo "$selected" | tr . _)"
tmux_running="$(pgrep tmux)"
if test -z "$TMUX" && test -z "$tmux_running"; then
# tmux new-session -s "$selected_name" -c "$PROJECTS/$selected"
tmux new-session -s "$selected_name" -c "$selected"
increase "$selected"
exit 0
fi
if ! tmux has-session -t="$selected_name" 2>/dev/null; then
# tmux new-session -ds "$selected_name" -c "$PROJECTS/$selected"
tmux new-session -ds "$selected_name" -c "$selected"
fi
if test -z "$TMUX"; then
tmux attach -t "$selected_name"
else
tmux switch-client -t "$selected_name"
fi
increase "$selected"