Daniel Henning

Frontend Engineer caring about digital accessibility and good typography

Published on

Configure predefined tmux windows

For quiet some time now tmux has become one of my day-to-day core tools. With time I also adopted and refined a set of tmux windows I create everyday to keep my tools sorted (like a window for timetracking for example).

So why create these windows everytime I start tmux? There should be a way to configure these once, right? And actually there is a pretty simple way. So simple even that it made me feel bad for not having done it a long time ago.

The important part

First things first, here is the deal in a quick step by step list:

  1. Open your tmux config. If you do not have one, create it. I have mine placed in my home directoy, i.e. ~/.tmux.conf which is one of the default locations tmux looks for configuration files
  2. Add set-hook -g session-created 'run-shell "tmux new-window -t 0:1 -n timetracking; tmux new-window -t 0:9 -n diary; tmux select-window -t 0:0"' to it.
  3. Open tmux (close instances you already have open beforehand for the new config to be applied)

If you are happy with that you can stop reading. If you want to know what all of the above does, keep on reading!

Demystifying the oneliner

Let's break down what the above configuration means.

  1. set-hook -g session-created: Hooks are a way to trigger certain actions when something happens within tmux. In this case, we define that the hook should run when a tmux session has been created via -g session-created. There are a couple of options, e.g. command-error let's you do something if a command errors out, or window-renamed which executes after a tmux window has been renamed. You can look at the "HOOKS" section inside the tmux man pages to get a list of all possible hooks.
  2. run-shell: This is a tmux command which executes a shell command in the background. We use this to run tmux again which then sets up the windows. That way we can set all the windows up at once.
  3. tmux new-window: This is the command to create a new tmux window. -t n:m defines the window placement, where n is the session id and m defines the window index. In the line above, -t 0:1 places that window in session zero at index one. -n renames the window to the following string. Execute this for each window you want to be configured.
  4. tmux select-window: At last we are using this command to select a window. I like to have the first window to be selected on startup. Using the same syntax as before, we are using -t 0:0 to select the window on index 0 inside the session with id 0.

Conclusion

In the end, this configuration is pretty basic and only covers simple tmux setups, like my personal one where I am only using a single session and a couple of windows at once. It might get more complex if you rely on multiple sessions or variable session ids, but for many use cases this should be a nice little time- and keystroke saver.