Managing Tmux Sessions
Tmux is a favorite tool at Hashrocket. In this post, I'll share some commands for managing your Tmux sessions.
Hashrocket is known as a Vim shop, and Vim pairs well with Tmux, the open-source terminal multiplexer. Combining these tools creates an environment similar to an IDE, but in the terminal, which has advantages.
Sessions are the workspaces of Tmux. Keeping them alive and separate is like having a woodshop with a different bench for each project you're working on, ready to go.
Managing these sessions is a meta layer of Tmux, something we think about a lot at Hashrocket. The tips below are a collection of some of our favorite commands, culled from Today I Learned and conversation.
Throughout this list, I'll be using <prefix> +
to reference the Tmux leader and whatever comes afterward. Tmux's default leader is CTRL + b
; at
Hashrocket we've mapped it to CTRL + z
. For more information on Hashrocket's
Tmux configuration, check out our tmux.conf
dotfile.
Create a Named Tmux Session
Creating new Tmux session from the command line grants a default name of 0
to the session:
$ tmux new
We can take this opportunity to pick a good name via the -s
flag:
$ tmux new -s tiny-rust-app
Rename Your Tmux Session
Didn't follow the previous tip? That's okay; we can rename our session.
Pretty much every Tmux command has a long version and a short version. Like many tools, the long version has a human-readable name like 'list-sessions', and the short version is just a symbol or number. I think you can coast for a long time just knowing the long version, or guessing at the long version. Memorizing the short version makes sense in some cases.
That said, here's the short command to rename your session from inside Tmux:
<prefix> + $
Replace the existing name with the desired name, tiny-rust-app
for example,
and hit enter.
List All Tmux Sessions
List your Tmux sessions with:
<prefix> + s
From here, you can navigate the list with j
and k
, and switch by hitting enter.
Jumping Between Tmux Sessions
Was the previous tip too slow for you? I sympathize. Tmux also provides the
<prefix> + )
and <prefix> + (
bindings as a way of jumping to the next and
the previous session, respectively.
Boot Other Users from Inside Tmux
When other people attach to your Tmux session, the window shrinks to fit the smallest screen, filling in the gaps for everyone else with dots. Less real estate makes reading and writing code more difficult. While your friends are out purchasing bigger displays, kick them off your session:
<prefix> + D
This opens an interactive list of all connections to the current session,
and their dimensions. You want to
kick off the users with smaller dimensions. Navigate to your selection and hit enter, or q
to back out.
Load Tmux and Boot Other Users
An alternative to the previous command is to load the session while forcing all
other sessions to detach, via the -d
flag:
$ tmux attach-session -d -t tiny-rust-app
To quote Josh Branchaud:
By detaching all other sessions, you are ensuring that your machine's dimensions are the ones that Tmux uses when drawing the window. This is a great quick fix if you're working on your own, but probably not what you want to do if you are in a pair programming situation.
Kill Your Current Tmux Session
<prefix> + :kill-session
Kill All Other Tmux Sessions
Okay, you can kill your current Tmux session, or kill it from the command line via $ tmux
kill-session -t tiny-rust-app
. Did you know you can add a flag to kill every
session other than your target? Here's that command.
$ tmux kill-session -a -t tiny-rust-app
$ tmux kill-session -at tiny-rust-app # short version
All sessions but the target will die.
Kill All Tmux Sessions
Here's great command when you want to go nuclear. Kill all sessions with:
$ tmux kill-server
Conclusion
Managing sessions can transform your Tmux experience into something sublime. I hope you found a new tip or two here, and please let us know your favorite Tmux session commands on Twitter. We want to know how you Tmux!
Special thanks to Dorian Karter, Josh Branchaud, Josh Davey, Chris Erin, and Andrew Vogel for sharing these insights with the team via Today I Learned.
Photo by Jeff Sheldon on Unsplash