Iterm2 + Tmux = Awesome


If you regularly work with the terminal, especially if your work with remote servers via SSH, you must know about Tmux. It is an awesome tool that makes your work so much easier!

What is Tmux? Tmux stands for a terminal multiplexer. It basically allows you to open multiple terminal sessions inside a single terminal window or even remote terminal session (like when you SSH into a server). This may not seem like very cool to you now, but let’s look at some examples of how it works and I’m sure you’ll love it :)

First, we need to install and configure Tmux. As I also wanted to show you how awesome Tmux integrates with Iterm2, I assume you have it ╭( ・ㅂ・)و

To install Tmux just run:

$ brew install tmux

Then type tmux in your terminal, hit enter and start using it ;)

Well, you’ll probably get lost if you use it for the first time as it takes a few things to learn to get a grasp of it. So follow along and I’ll show you how things work.

As we already mentioned, Tmux allows you to open multiple terminals inside a single terminal window. But it also supports splitting panes which allows you to open multiple terminals inside a single terminal window so it looks something like this:

200x200

Tmux is developed as a client-server model which brings the concept of sessions into play. You create those new terminal windows within a session. So the first thing we need to know is how to create a session:

$ tmux # creates a new session (session number is assigned as a session's name)
$ tmux new -s <session-name> # creates a new session and assigns it a name

To list the sessions you can run:

$ tmux ls

Creating a session automatically creates a new terminal window within that session. You’ll see how your current terminal window is switched to a new terminal.

You may wonder how did I detach from the new session and get to my original terminal. Here is the command:

Crtl + b, d # don't type the comma, it is here to separate the prefix and the command

Here Crlt + b is the so called prefix which basically tells the terminal that the next thing you type will be a Tmux command. You’ll have to deal with this prefix a lot if you don’t use Iterm2, as every Tmux command should be prefixed with this combination, although you can change it.

To attach to an existing session you can use the following command:

$ tmux a -t <session-name> # "a" is short for "attach" here

Now as you got the basics, let’s look at a more advanced example with splitting panes.

“That’s all very cool, but I can do the same things with Iterm2 profiles and splitting panes” - you’ll probably say. And you’re right. I personally very rarely use Tmux locally. When Tmux becomes incredibly useful is when you work with remote servers via SSH.

Does it ever happen when you need to work with some remote server regularly via SSH? Maybe you’re a web developer and develop your new application. Or maybe you’re a system administator who is testing a new service setup and configuration. In these cases, you’ll often open up many terminals: one - for a text editor with a service configuration or code, one - for tailing logs, one for launching the application or service, etc. Your work on a server can take hours and what really sucks is when you take a break and go for a lunch and you need to keep your ssh connection open, because otherwise you’ll have to open all those windows again to be able to work.

Here is where Tmux comes into play. The greatest thing about Tmux session is that it can persist beyond SSH logout. So you can just shutdown your laptop, go play soccer, then come back, ssh into your remote server and open the terminal windows as they were when you logged out. Tmux basically makes your work independent of SSH connection.

Let’s see an example that demonstrates that Tmux sessions indeed persist beyond SSH logout.

Here I used this cool Iterm2 and Tmux integration I was talking about.

Instead of switching my terminal to a new one created by Tmux session, Iterm2 allows me to open it just like another tab in my terminal window. Moreover, I don’t have to memorize all of those Tmux commands like Crtl + b, % to split a pane, Crtl + b, o to switch a pane, but instead I can use the Iterm2 shortcuts I use every day \ (•◡•) /

To use Tmux with Iterm2, you only need to provide an extra option (-CC) for Tmux commands:

$ tmux -CC # create a new session
$ tmux -CC a -t <session-name> # attach to a session

And yeah, you can choose if you want to open Tmux session in a new tab or a new window. See Iterm2 settings:

200x200

Tmux is very lightweight and even comes preinstalled with some Linux distributions like Ubuntu. Tmux eliminates the risks of losing an SSH connection. Sometimes it’s very important like when you’re doing a manual backup of your server. In gereral, if losing SSH connection means a lot of work to you - opening a window for a text editor, for logs, for running commands - then Tmux can make your life so much easier. Let’s look at another example close to a real world use case: playing around with nginx configuration:

Hopefully, if this post didn’t make you fall in love with Tmux, at least it made you curious ⚆ _ ⚆

In the end, I want to also mention another cool feature which Tmux provides. It allows multiple users to share a terminal session which basically means two people can work in the same terminal at the same time. For example, this could be helpful for pair programming.

Also, if you really liked the idea behind Tmux, you may want to take a look at Tmuxinator which allows you to customize your work with Tmux even further.