Tmux

My greatest productivity hack

I’m always looking for more tools and ways to improve my productivity. One of my favorite tools that I’ve learned about is Tmux. I’m going to walk through the simple commands I use in Tmux every day and why I think Tmux is such an incredible tool.

Put simply, Tmux is a terminal multiplexer. This means you can run a single Terminal application, but have multiple terminals controlled by a single screen. You can create multiple sessions, tabs, and panes. Additionally, you can easily leave scripts running on remote connections that will continue to run long after your client machine has disconnected or even shutdown.

Since it might not be very clear what Tmux is with this text description, lets look at a photo of Tmux in action:

tmux.png

Here we can see a single terminal application being split into three panes. The top pane is running htop, the bottom left ran cowsay, and the bottom right has a man page open. Additionally, we can see two tabs open in the bottom left, debug and dev. We’ll go over what these vocab words mean and how to get the most out of tmux.

In Tmux you have a magic keystrokes that you can use to preface Tmux commands. By default this is CTRL + b. I’m going to assume you’re using CTRL + b for this blog post, but feel free to modify this command to something that works for you!

Please note, any of the + symbols do not mean to press the plus sign, rather to type these keys at the same time. I’ll be using the word and to indicate you can release your hands from the first keystroke to type the next set.

When we’re using Tmux, we can create as many session as we want. I tend to organize my sessions based on trains of thoughts. For work, that means I often have one session for each client’s project I’m working on. To create a session, type:

$ tmux new -s [name-of-session]

I usually create a new tab for every sub task in each session. For example, I may have the first tab in my session be at the root of my repository and used for Git. Another tab I may have ready to go to run my tests, another with a vim session and perhaps another with a pdb session. To create a new tab:

CTRL + b and c

Now that you have multiple tabs, to navigate between them you can move left or right a tab.

To move to the left, or to the previous tab:

CTRL + b and p

To move to the right, or to the next tab:

CTRL + b and n

When you find yourself with many Tmux sessions and tabs, you may start to get convinced which tab was being used for what task. One can name the tabs to help organize everything. To do that press:

CTRL + b and ,

Then modify the buffer to be the desired name and press enter.

To disconnect from the current session simply press:

CTRL + b and d

You may find yourself with multiple sessions. To list them all type:

$ tmux ls

If you have know the name of the session you want to connect to, simply type:

$ tmux a -t [name-of-session]

A neat trick to swap between sessions, rather than disconnecting and then reconnecting manually, is to use this keystroke:

CTRL + b and w

This will give you a display of running sessions. Navigate to the one you wish and press enter to select it.

You might find yourself wanting to create a split, either vertically or horizontally, and use your screen real estate for one than one task.

To split horizontally:

CTRL + b and "

To split vertically:

CTRL + b and %

Note that you must be holding down the shift key to type " and %

And to move between splits press:

CTRL + b and arrow key

If you’re working with multiple splits, but want the currently active split pane to be full screen simply press

CTRL + b and z

Undo this full screen mode by pressing the keystroke again.

While one can use the previous trick to make a split pane temporarily full screen and copy like normal, one can also use built in Tmux commands. From this SO post:

  1. CTRL + b and | to enter copy mode
  2. Move to the start/end of text to highlight
  3. CTRL + space (if this doesn’t work, try just space) to start highlighting
  4. Move to the opposite end of the text
  5. Alt + w copies the selected text into the Tmux clipboard (if this doesn’t work try Enter) (if on a ma, try ESC + w)
  6. Move your cursor to a different pane where you want to paste
  7. CTRL + b and ] to paste from the tmux clip board

To copy to the system clipboard, in order to paste into say a text editor or web browser, replace step 5 with CTRL + b and yto yank/copy into the system clipboard. Then just paste as you normally would using right click options or CTRL + v.

One of the most powerful ways to use Tmux is in a nested session. Like I mentioned before, I often use a session locally for each client project that I’m working on. Often times, I’ll find myself connecting to remote servers over SSH. What I’ll end up doing is starting a second Tmux session on the remote server, thus creating a nested session. The benefits to doing this are numerous:

  1. I can use Tmux on the remote server to run multiple sessions, tabs, etc just like one would do locally
  2. My client machine can experience a connection hiccup, go to sleep, restart, shutdown and the remote session will still be running. This is incredibly useful for long running processes.

To interact with nested sessions, you simply press CTRL + b one more time. So think of all the commands we went over as translating to:

for the 1st session do…

To interact with the first nested session you’d run CTRL + b and CTRL + b. Think of this as translating to

for the 2nd session do…

So those are the basic Tmux commands that I use constantly throughout the day. There are a lot more powerful commands you can run with Tmux, but starting here will make you feel like you have a super power with the terminal!