Managing Work Context with tmux

tmux is an amazing tool for managing terminal sessions. I use it to manage entire project contexts constently.

Let’s talk about tmux!

tmux is a terminal multiplexer: it enables a number of terminals (or windows), each running a separate program, to be created, accessed, and controlled from a single screen.

It essentially provides a new context for you to manage multiple terminal windows. I find this useful for grouping terminal windows that relate to a single project, allowing me to switch entire project contexts quite easily.

I’m assuming you know a bit about how tmux works. If that’s not true, take a look at some tutorials and come on back!

You can switch normal tmux sessions (what I’m calling my project contexts) by hitting mod+s, then selecting the session you want. However, I want to be able to recreate my sessions if they don’t already exist. To do this, you can create shell scripts that create tmux sessions just as you might by using the shortcuts.

tmux Scripting

Let’s create a shell script that will create a context for a project called facile.

Now, we can just run this script to create and attach to our session! But, we can do better.

Context Switcher

Let’s create a context switcher that has the following properties:

  • only creates the session if it doesn’t already exist
  • attaches to the session we ask for
  • allows us to easily setup new session creation scripts

The simple bash scripting here could be better, but it achieves all of our goals. First, it uses tmux a -t $1 to try to attach to a session with the given name. If that fails, the || clause triggers and executes ~/.tmux/setup_$, which looks for a session creation script in that location prefixed by setup_. If that fails, we create a new blank session with the given name by executing tmux new-session -s $1.

Using It

Now, we can simply call tm facile to attach/create our facile session. We can call tm new-thing to create a blank session for our new-thing project. We can go on to create a new session creation script at ~/.tmux/ so that future executions of the command tm new-thing executes that script instead!