-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmx
More file actions
executable file
·56 lines (48 loc) · 1.39 KB
/
mx
File metadata and controls
executable file
·56 lines (48 loc) · 1.39 KB
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
#! /usr/bin/env bash
# http://stackoverflow.com/questions/3432536/create-session-if-none-exists
# ~/bin/tmux-myproject shell script
# The Project name is also used as a session name (usually shorter)
if [ $# -lt 1 ] ; then
echo -e 'Attach to existing tmux session or create new one. Optionally run command inside the session.\n'
echo 'Usage:'
echo "$0 name [command]"
echo ''
echo 'List sessions:'
tmux list-sessions
exit 1
fi
NAME="$1"
shift
JUST_CREATED="x"
tmux has-session -t "$NAME" 2>/dev/null
if [ "$?" -eq 1 ] ; then
echo "Session not found, creating and configuring..."
tmux -2 new-session -d -s "$NAME"
echo "Applying configuration ${HOME}/.tmux.conf"
tmux source-file "${HOME}/.tmux.conf"
CONF="${HOME}/.tmux-${NAME}.conf"
if [ -f $CONF ]; then
echo "Applying configuration ${CONF}."
tmux source-file "${CONF}"
else
echo "Configuration ${CONF} not found"
fi
JUST_CREATED="yes"
else
echo "Session found"
fi
if [ $# -gt 0 ]; then
echo "Running \`$*\` inside tmux..."
if [ "$JUST_CREATED" == "yes" ]; then
tmux new-window -t "$NAME:123" $*
# kill already created window with shell
# caveat: don't assume its number is 0
tmux swap-window -t "$NAME:123" -s "$NAME:1" 2>/dev/null
tmux swap-window -t "$NAME:123" -s "$NAME:0" 2>/dev/null
tmux kill-window -t "$NAME:123"
else
tmux new-window -t "$NAME" $*
fi
fi
echo "Attaching session..."
tmux -2 attach-session -t "$NAME"