I have been struggling with not user-friendly tmux’s CLI. I’m shortcuts guys, I like automating things, and I prefer learning some keybindings to simplify different things.
Last year I have started using Vim, and it boosted my productivity a lot, and I don’t like when something slows me down. Using tmux ls
and then tmux attach -t <session-name>
was causing me pain, because it takes a lot of text to just start a session, so I wanted to keep it simpler.
tmx() {
if [ -z "$TMUX" ]; then
echo "Active sessions: "
echo ""
tmux ls
echo ""
read inputTmuxSessionName"?tmux session name: (default | skip - to omit tmux initialization) "
if [ $inputTmuxSessionName ! "" ]; then
tmuxSessionName="default"
else
tmuxSessionName=$inputTmuxSessionName
if [ $inputTmuxSessionName = "skip" ]; then
echo "Skipping"
return
fi
fi
echo "Attaching to $tmuxSessionName"
tmux attach -t $tmuxSessionName || tmux new -s $tmuxSessionName
fi
}
I have created a zsh function to make it easier. All it does is show you active Tmux sessions and ask which one to pick. If you don’t specify the session name it will use “default”.
The second thing I have encountered here is that <session-name
in tmux attach
doesn’t need to be the whole session name, it can be just the first letter.
After calling tmx
in console output looks like this:
Active sessions:
default: 1 windows (created Wed Feb 23 20:27:48 2022)
second: 6 windows (created Mon Feb 21 10:14:31 2022) (attached)
tmux session name: (default | skip - to omit tmux initialization)
In this case, the last line is asking for input. You can put the whole session name, the first letter, or omit it. It’s passed to tmux attach -t <here>
There is skip
keyword which will just decline initialization.
Hope you will like it and find it useful. 💪🏻
You can find it on github.
Tags: automating bash terminal tmux zsh