Skip to content

Instantly share code, notes, and snippets.

@alexander-bauer
Created April 23, 2013 20:35
Show Gist options
  • Save alexander-bauer/5447150 to your computer and use it in GitHub Desktop.
Save alexander-bauer/5447150 to your computer and use it in GitHub Desktop.
work is a small script which attaches to a particularly named tmux session if available, and creates it if not.
#!/bin/sh
## work is written by Alexander Bauer and released under GPL 3+, the
## full text of which can be found in /usr/share/common-licenses.
#
# The purpose of this script is to attach to a tmux session of a
# particular name if it exists, or create it if it does not.
#
# SESSION is the name of the tmux session.
SESSION="$USER-work"
# If possible, simply reattach.
tmux has-session -t "$SESSION"
if [ $? -eq 0 ]; then
echo "Attaching to \"$SESSION\""
sleep 1
tmux attach -t "$SESSION"
exit 0
fi
# Because we'll need to create a number of panes, define a function to
# do it.
# Arguments:
# command to run in the new pane (will close the pane when terminated)
# pane to split from (of the form "1" or "2")
# direction in which to split ("h" or "v")
addpane() {
tmux split-window -$3 -t "$SESSION":1.$2 "$1"
}
# The first pane is created along with the session, and can be given a
# command. It is important to note that if the command terminates, so
# too will the session. Leave it blank for a shell.
# Any additional panes will be added with the "addpane" command as
# defined above. The arguments to this are, in order, "command",
# "pane", and "direction." Note that when the command terminates, the
# pane will be removed. Leaving it blank will spawn a shell. The
# "pane" argument is the number of the pane to split from, such as 0
# or 1. Finally, the direction is either "h" or "v", which are
# "horozontal" and "vertical" respectively.
# 1 - emacs
#tmux new-session -d -s "$SESSION" #"command"
tmux new-session -d -s "$SESSION" "emacs -nw"
# 2 - zshp
#addpane "command" [0123...] [hv]
addpane "" 1 h
# 3 - irc
addpane "weechat-curses" 2 v
# Now all of the windows should be set up. Now, attach.
echo "Created and attaching to \"$SESSION\""
sleep 1
tmux select-window -t "$SESSION:0"
tmux attach -t "$SESSION"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment