TMux Attach or Create: A little quality of life improvement for tmux
Published on 2023-010-20
I am coming back to my blog after a long time away because I am overjoyed with one of the highest ROI quality of life improvements I have made to my setup. I am not a bash expert and I am sure there are a hundred better ways to do this, but I couldn't be happier with how this turned out.
I call it tmac
(TMux Attach or Create). All it does is, check if a session exists with the given name, if it exists, attaches to it, otherwise creates one and attaches to it. The real magic here is that it adds autocomplete so you can browse your available sessions in the attach command. Here is what to add to your .bashrc to get it:
tmac () {
tmux has-session -t "$1" 2>/dev/null
if [ $? != 0 ]; then
tmux new-session -s "$1" -d
fi
tmux attach -t "$1"
}
_tmac_complete() {
local word=${COMP_WORDS[COMP_CWORD]}
local sessions=$(tmux list-sessions -F "#{session_name}" 2>/dev/null)
COMPREPLY=( $(compgen -W "$sessions" -- "$word") )
}
complete -F _tmac_complete tmac
And to use it:
# create a new session
tmac my-session
# after you've detached, type tmac and press tab
tmac my-session
I do a lot of ad-hoc server work, which involves kicking of an experimental long running task on a server. I run these jobs in a tmux session, then come back to attach and check out the job. I am fairly disorganized and I often forget exactly what I called the session I want. My typical workflow was to check for the command I used to create the session in my bash history (for some reason I'd rather hit the up arrow a hundred times than just typing the command). Unfortunately, when I do find the command, I can't rerun it because the command to create and attach are not the same. For a long time, I have thought it would be nice if these were just the same command but it was just a minor inconvenience so I never got around to looking into it.
Today I finally got around to trying it and with two minutes of ChatGPT I got my command (this is not a ChatGPT hype post, just got to give credit where credit is due). This is a perfect sort of thing to ask and I had regretted waiting as long as I had. Once I had the command, I realized there was a problem with it. The tmux attach -t
command allows you to supply a prefix instead of the whole session name. This whole project was an exercise in laziness and it was kind of lame that I will need to type out whole session names whenever I use this command.
I realized the perfect solution to this would be add autocomplete to my new command based on currently open tmux sessions. I honestly didn't even know that you could to set up autocomplete for bash functions before this but I got it set up in under a minute. The final command is glorious. Not only does it work with my typical bash history workflow, it allows me to essentially use tmux ls
within my attach command just by hitting tab. For whatever reason, tmux attach
allows you to attach based on a prefix but it doesn't autocomplete so if you have more than one session with the same prefix it won't let you know. The final command combines tmux ls
, tmux new-session
, and tmux attach
into a single convenient command: tmac
.
It only saves a few seconds but it just feels so much better. I am aware this is not a huge revelation, and I didn't even do the heavy lifting here but I love it and I thought other people might too.