For a while now, I've been using Byobu to manage my terminal sessions. It is essentially a wrapper for tmux (or alternatively
screen), allowing you to split your terminal into multiple panes, multiple windows and "detatch" from the terminal without killing the session (i.e. to leave some long-running script on a server without having to stay connected via ssh say), then reattach at some later time.
Each day I tend to setup my terminal by doing a vertical split (one pain for viewing a django template another pane for viewing a django views.py say), then I'd tab to a new window in which I'd launch the local development server. I'd have to
cd to the correct dir in each pane/window, and launch the virtualenv in each. OK admittedly, people have bigger problems :\, but this was getting old fast.
At this point I started to wonder how I could speed this process up, and was lead to the wonderful world of tmux scripting.
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
#!/bin/bash SESSION=$USER # -2: forces 256 colors, byobu-tmux -2 new-session -d -s $SESSION # dev window byobu-tmux rename-window -t $SESSION:0 'Dev' byobu-tmux split-window -v # Select 1st pane cd and venv byobu-tmux select-pane -t 0 byobu-tmux send-keys "cd /path/to/workingdir" C-m byobu-tmux send-keys "source venv/bin/activate" C-m byobu-tmux send-keys "clear" C-m # Same with 2nd pane byobu-tmux select-pane -t 1 byobu-tmux send-keys "cd /path/to/workingdir" C-m byobu-tmux send-keys "source venv/bin/activate" C-m byobu-tmux send-keys "clear" C-m # Runserver window next byobu-tmux new-window -t $SESSION:1 -n 'Runserver' byobu-tmux send-keys "cd /path/to/workingdir" C-m byobu-tmux send-keys 'source venv/bin/activate' C-m byobu-tmux send-keys "clear" C-m # Set default window as the dev split plane byobu-tmux select-window -t $SESSION:0 # Attach to the session you just created # (flip between windows with alt -left and right) byobu-tmux -2 attach-session -t $SESSION
To roughly walk through this script: first I launch a new byobu session (the name for the collection of panes and windows) named after my user handle. I then rename the current window (identified by the session name followed by
:0 to signify the first window in session) "Dev" since this is where I will do my code writing. I vertically split this window, select the zeroth pane, cd to my working dir, launch my venv and clear the output for tidyness. This is repeated then the 2nd pane, before I launch an entirely new window called "Runserver", in which, you guessed it, I will (manually) launch the django runserver.
I switch to the first window, "Dev", and finally attach to the byobu session.
Don't forget to
chmod +x this script, then give it a try.
To make it even nicer, we can put a nice little launcher and icon on the Ubuntu desktop and you can have you session with a double click.
Create a file, say
sess.desktop that contains:
1 2 3 4 5 6 7 8 9
#!/usr/bin/env xdg-open [Desktop Entry] Version=1.0 Type=Application Terminal=true Exec=bash /path/to/your_tmux_script.sh Name=LaunchSess Comment="Launch byobu sess" Icon=/path/to/pretty-icon.png
Save it and
chmod+x and there you go. You could use a little icon like