Quick tmux Sessions

I've been using tmux for a few years now. For every project I work on I create a tmux script that will start up a development environment for that project.

For a long time the tmux scripts I created just opened windows to run the various servers that the project required and nothing else. For a Rails project it would typically be a window for the Rails Server, another window for Redis, one for Sidekiq and sometimes one for Mailcatcher.

It was very handy to just run a script to have the whole environment for a project up and running, but I typically didn't interact much with the tmux session after that. Just checking server output from time to time. The rest of the time I was in Atom or the browser.

When I started using Neovim I began to spend more time in tmux. I updated my project specific tmux scripts to include an editor window along with the existing server windows.

I got so used to this way of working that I would find myself creating mini tmux sessions with just an editor window and a console window whenever I needed to do any kind of quick editing work outside of a full project. It is easy to setup a simple two window session, but why repeat the same process over and over?

Here is a short shell script that will create a simple two window tmux session given a directory path.

NAME=`basename $1`
cd $1
tmux new -s $NAME -n editor -d
tmux send-keys -t $NAME $EDITOR C-m
tmux new-window -n console -t $NAME
tmux select-window -t $NAME:1
tmux attach -t $NAME

This is just the main part of the script, the full script is commented and checks the input parameters. You can see the actual script here.

The script expects a single argument, a path to a directory. It extracts the final directory name in the path into the NAME variable. It uses this name as the tmux session name throughout the script.

After extracting the name the script changes directory to the provided path. After that it creates a new tmux session, and names the first window editor. Once the session is created the script sends a command to the tmux session to start the editor associated with the EDITOR environment variable. On my system that is set to nvim, but it could be any editor that can be started from the command line. Be aware, though, if you start a GUI based editor it will open outside of the Terminal and there won't be much use for this tmux editor window.

Now that the first window is created, named and the editor is running the script creates the second window. It simply names it console. I find it helpful to have a dedicated console window for working with Git or examining files or other tasks. Some of these tasks can be done in Neovim, but I like having a dedicated console window for some tasks.

Once the console window is created the script tells tmux to select the editor window and then attaches to the session.

And there it is. One simple script and you can have a mini tmux session running in a matter of seconds. I named the script qs, for quick session, and put it in my path. Now whenever I need to do a little bit of work, for instance in my dotfiles project, I can simply run:

qs ~/dotfiles

If you would like to use the script yourself just copy the code to a file, make sure the file is executable with chmod +x [file] and make sure your EDITOR environment variable is set to something appropriate, or change the $EDITOR reference in the script to whatever you like.