tmux = terminal multiplexer
I first saw tmux when I was doing pair programming with a friend who did just about everything in the terminal. At first I was slightly annoyed at his seemingly obsessive endeavour — I’m a full on GUI guy and I just wanted to get stuff done with the least amount of effort. Every time it was my turn to drive I’d get stuck in some mysterious command line software that he was running. “What does this accomplish other than impressing other geeks?!” I thought.
But, I completely changed my mind when I started hosting websites on a VPS. I saw the huge merit in being able to use tmux proficiently when all you have is a CLI after connecting to, say, a linux server for instance.
I decided to learn tmux.
Get Ready
First you have to install tmux. If you’re on a mac like me, use Homebrew. If you’re on a different OS, use the appropriate package manager.
brew install tmux
Once you have it installed, open a terminal and type tmux
to start.
Now that you have tmux, let’s learn some useful commands to tackle(impress) those(your) servers(friends)! 😏
First thing you need know is that by pressing ctrl+b
in tmux you will get it to listen on the next key input and treat it as a tmux command. You will see a lot of ctrl+b
from here on.
Vertical Split
ctrl+b %
Being able to use multiple panes is extremely useful. This will save you the hassle of opening multiple ssh connections to your server when you want to run more than just a single process at the same time.
You can press ctrl+b
and then %
to split the current pane you are focused on vertically into two.
Horizontal Split
ctrl+b "
Similarly, you can split horizontally by pressing ctrl+b
followed by "
.
Switch Pane
ctrl+b ↑
ctrl+b ↓
ctrl+b ←
ctrl+b →
Once you have multiple panes open, you can switch between them by pressing different arrow keys following ctrl+b
.
Maximize Pane
ctrl+b z
If you want to enlarge and focus on a pane, you can maximize the currently selected pane by pressing ctrl+b
followed by z
. Once maximized, the letter Z
will be attached to the tab that the maximized pane belongs to.
If a pane is already maximized, he same command will un-maximize the pane and take you back to the split pane view.
Kill Pane
ctrl+b x
If you want to kill a pane, the proper command is ctrl+b
followed by x
. However, if you simply type exit
to end the process running in the pane, tmux will also kill the pane.
Create Tab
ctrl+b c
You might have noticed by now that on the bottom left corner of tmux there is a label that says [0] 0:bash*
. This means that you are currently looking at tab zero of tmux window zero.
Now, if your window is getting too crowed with many panes, you can create a new tab to hold more panes by pressing ctrl+b
followed by c
. In this image I did that twice so now I have three tabs open.
The *
indicates which tab I’m currently on, which is tab number two.
The -
indicates the tab I was previously on, which is tab number one.
Switch Tab
ctrl+b n
ctrl+b p
ctrl+b l
Once you have multiple tabs open, you can press ctrl+b
followed by n
to move the focus to the next tab. Similarly, ctrl+b
followed by p
will move the focus to the previous tab.
You can also do ctrl+b
with l
to go to the last tab you were on, which is marked with the -
sign.
ctrl+b 0
If you follow ctrl+b
with a number, you will switch to a tab that has that number as the index. In this case the command will take us to tab zero.
Detach
ctrl+b d
You can leave a tmux window but keep it running in the background by detaching the window with the command ctrl+b
followed by d
. After doing this you will be back to your normal terminal.
After detaching, you can run tmux ls
to see all the tmux windows that are currently running. In this case there is only one.
Attach
tmux a
tmux a 3
tmux attach
tmux attach 3
You can use tmux attach
(or tmux a
) to re-attach the window and resume where you left off. If you give it a number such as tmux a 3
you will be attaching to the tmux window that has that number as its index.
These are the basic commands for tmux. It should be enough for you to get started. If you want more extensive documentation you can go take a look at the tmux repo on GitHub.
Congratulations! You have now graduated from the tmux school of terminal wizardry! Now you can go forth and practice(showoff) your newly learnt command line magic tricks! ✨