Tmux Create or Join

・1 min read

I have been using tmux as an alternative to screen for a couple of years so far. It works very well and I very quickly got used to the shortcuts for it - especially after changing the shortcut key to CTRL + A.

One thing that I wanted to do was ensure that I was always in a tmux session. There’s little worse than running a command that is going to take some time but realising that you’re not in tmux session and you can’t quit the SSH session until the command completes.

With the help of Termius (SSH client), I created a snippet to be run whenever I connect to certain SSH hosts.

tmux att -t session -d || tmux new-session -s session

The snippet is simple - it attaches to an existing tmux session if it exists, if not, it will make a new one.

Since putting this in place a few months ago (maybe a year ago, time flies) I have never worked outside of a tmux session. I’ve never had to cancel a job to then re-run it within a tmux session and I have always been able to jump right back in to where I left off.

If you are interested, my tmux and other dotfiles are on my Github profile.