Tmux is ... so underrated.
Oh boy! My world will never be the same after this!
I was browsing YouTube when I started watching a guy talk about tmux. I thought, “What is this?” A whole new world began to unfold before my eyes! Holy moly—the video was absolutely fascinating. After that, I just plunged down a deep rabbit hole.
Before I discovered tmux, I would open new tabs and windows for every task I needed at the moment. Setting them up and switching between them was time-consuming, because I never knew which terminal had my project open—so I’d cycle through each tab one by one. With tmux, I just press the prefix
, then b
, and select the terminal I want. It’s that simple!
Here’s what I’ve learned:
Tmux’s architecture is designed to run in the background; it uses a client-server paradigm. You start the tmux server and create a session, then attach a client (your terminal) to it.
tmux has four main concepts, in order of hierarchy:
- Server
- Session
- Window
- Panel
That’s it. Getting started:
Install tmux
- On macOS (because i use Apple), run:
brew install tmux
- Create a new session
tmux new-session
note, short version is:
tmux new
Now you’re inside a tmux session. Let’s run a long-running command:
ping google.pt
Detach from the session by pressing Ctrl+b
, then d
. You’ll return to your normal terminal, but the ping is still running on the server. To reattach, run:
tmux attach
note: short version is
tmux a
If everything went well, you’ll see the ping still running. Wow!
Yep—that’s right!
clarification, in tmux terminology, Ctrl+b is called the prefix. Whenever you see “prefix” in the docs, that’s what it means. Also you can install tmux on your remote server using tmux on local machine with ssh into your tmux on remote server, and to interact on server side tmux session you have to press two times
prefix
.
Tmux is customizable, can also be installing plugin and is highly scriptable. For plugin you have to install Tmux Package Manager, TPM for short.
So here is my visual:
Homemade Session Manager Script
I've create my own "Session Manager" that i called "workspace", where each workspace is a script inside a folder, that script create each windows and tab and give name for each one.
Here is script for load workspace:
workspace() {
workspace="/Users/jfontes/.config/tmux-workspace/$@"
if ! [ -f $workspace ]; then
echo "Workspace not found $workspace"
return 1
fi
if [ "$TERM_PROGRAM" = tmux ]; then
tmux detach
fi
sh $workspace
}
This make me ease to transit from one 'workspace' to another. What is look of my workspace script?
#!/bin/bash
# filename=jonathan
SESSION="jonathan"
SESSIONEXISTS=$(tmux has-session -t $SESSION 2>/dev/null)
if [ $? != 0 ]; then
# New session. -d mean detached and -s is session name
tmux new-session -d -s $SESSION -n 'home'
tmux send-keys -t $SESSION:home 'cd $HOME; clear' c-m
tmux new-window -t $SESSION -n 'project'
tmux send-keys -t $SESSION:project 'cd $HOME/Sites/jonathan.pt/; clear' c-m
fi
tmux a -t $SESSION
This is an example when i need to work on my personal website. Just need to press:
workspace jonathan
And that's it.