July 29, 2018
I’ve been playing with Tmux recently, and find it super helpful to manage terminal sessions, especially when doing remote things. So, if you are in the middle of training ML models on a server but have to close your laptop lid and disconnect the SSH session before the 5-hour ends, Tmux is the tool for you! Well, here’s a quick tutorial of those Tmux features that I use, hope you find it helpful!
Tmux is available on all three major platforms (including Windows via cygwin or WSL). Built for *nix users, Tmux is pretty easy to get if you are on a Mac or linux. For those who have Homebrew or Linuxbrew installed (yes, I do use Linuxbrew from time to time 😃), simply run
brew install tmux and you are good to go.
Now, enter Tmux by just running
By default, all Tmux commands in a session are invoked with a prefix key combination
Multiplexing the terminal screen is what a “terminal multiplexer” does right? Let’s see how multi-window inside a terminal works.
Let’s split the screen first. For vertical planes, go for
C-b then a
%; for horizontal splits, go for
C-b ". Note that the cursor indicates which terminal plane is currently focused, and the next split-screen will apply to the current active plane.
Great! So how do we navigate through the different planes? Just use
C-b with the arrow keys!
If you want to resize the current plane, just hit
C-b then another combo
C-<Arrow Key>, with the
<Arrow Key> being the direction you want to stretch to.
Try playing around, like, firing up a 3 planes and pretend to be leet. (All I did was just looking up how
cp command works while monitoring the resources with
top, but! 😎)
Alright, now we have 3 planes fired up inside one window. But we can create new windows too!
C-b c Creates a new “window” (note the status bar down below). Switching or cycling through the windows can be done with
C-b n for next window while
C-b p for previous window.
For me, the most useful thing Tmux does is the ability to “detach” a session so the user can come back and re-attach to it later remotely, without killing the running job or even lose the current multi-window config.
A common usage is to run everything inside Tmux over SSH, that is to SSH into a remote machine, and run Tmux from there.
In order to detach a Tmux session, you can either interrupt the SSH connection or hit
C-b d. If you run
tmux immediately though, you will enter a different session other than the one detached and running in the background. What you need here is to run
tmux attach instead, which will attach to the last Tmux session.
tmux ls lists all the sessions currently running. The default names of those sessions are just numbers counting from 0, but you can specify a name for your session too.
If you are already in a Tmux session, run
C-b $ will allow you to assign a meaningful name to the current session. You can also assign a name to a session while you are creating it - just run something like
tmux name -s my-secret-session-now-has-a-name! 😉
Now, with a list of running Tmux sessions, we can attach to any of them with
tmux attach -t <session-name>, for example,
tmux attach -t my-secret-session-now-has-a-name. Super useful!
Alright, we’ve just leveled up our remote terminal experience with Tmux! I haven’t digged into tweaking the default settings yet, but you can of course personalize Tmux to your liking by editing its “dotfile”, normally located at
From now on, if a buddy calls while you are in a supermarket that he needs you to turn on the Jupyter Notebook server on your beefy AWS GPU server to run some emergent experiments, you can just fire up the server and SSH into it with Termux on your phone, enter a Tmux session and open up the Jupyter Notebook server there. Knowing that all things running in a Tmux session doesn’t get terminated if the SSH connection drops, you have no need to worry about what happens if you lose your cellphone signal. Neat!