Skip to content

tmux

tmux is a terminal multiplexer, which allows you to have multiple terminal sessions running at the same time, and use virtual windows and panes to manage them. It's a must-have utility for working with remote servers, as it allows you to detach from a session and re-attach to it at a later time. This makes it perfect for example if you are installing updates on a remote server, as the process should complete even if your SSH client gets disconnected.

It also works great with a dockerized workflow for Django, as you can run multiple commands in the same window by splitting it into panes. I like to have one window running neovim, and another window with as many terminals as I need, which is normally around four. As you can see on the image below, clockwise from the top, I have one pane to run the Docker Compose stack, one pane that I use for git operations, one pane to run commands in the Node.js container, and one pane where I can access the shell in my Python (Django) container, so I can run commands to deal with migrations or access the Python shell.

tmux panes

Confused by Docker and containers? Don't worry, we'll cover those in a later chapter.

Installing tmux

Arch Linux

Tmux can be installed from the official Arch Linux package repository via pacman. I also recommend installing the tmuxp session manager, which we will cover later in this chapter.

$ sudo pacman -S tmux tmuxp

Ubuntu

Using tmux

To start a new session, you can simply run tmux, but that's a little boring as it will simply name it with a numeric index. I recommend always starting named sessions, which you can do with the new argument and -s <name> flag. Names should be fairly short, ideally, so acronyms may be good. For example, as I work on my Linux for Djangonauts book, I run tmux new -s lfd to name my session lfd.

At first, you may not see much difference, as it simply opens a new terminal window, but you may notice a status bar at the bottom. tmux can take certain commands from key bindings tied to certain features. Many of those are prefixed with the tmux prefix, which is Ctrl-b by default.

Panes and windows

Let's add a new pane by pressing Ctrl-b, and then %. This will split the current pane horizontally, and create a new pane on the right. Let's split it vertically by hitting Ctrl-b again, and then ". You should now have three panes, with the bottom-right one being the active one. You can type a command like echo hello pane and press enter, then jump to another pane. Press Ctrl-b again, followed by the up arrow, and you will see that you are now in the top-right pane. Press Ctrl-b followed by the left arrow, and you should be on the left-most pane.

Sometimes you want to focus on one pane. To do so, press the prefix (Ctrl-b), followed by z to zoom in on the active pane. Repeat the process (prefix, z) to zoom out.

Now, let's create a new window by pressing the prefix, and c. As you can see, you are now in a new window with only a single pane. Type echo hello window 1, press enter. You can jump to the previous and next windows by hitting the prefix, and p or n. This will cycle through all the windows in your session.

Sessions

You can detach from the current session by hitting prefix, followed by d. You should see a message saying that you detached from the session, so create a second session named test. Once more, you should be in a new shell.

Whilst in tmux, you can get a list of sessions with Ctrl-b followed by s. If you hit it now, you should see lfd and test in the list. You can use the arrow keys to navigate through the list, and press enter to switch to the selected session. Alternatively, you can press Ctrl-b followed by w to see a list of windows across all sessions. You should see two windows in the lfd session, and one more under test. Select any window, and detach from it again.

Outside tmux, you can get a list of sessions by running tmux ls. To attach to a session, simply run tmux a -t <session>, e.g. tmux a -t lfd.

Exiting tmux

tmux will automatically exit when you close all panes and windows in a session. It should let you know that you exited the session, but other sessions may still be active.

Configuration (TODO)

You can use the ~/.config/tmux/tmux.conf file to configure tmux.

tmuxp (TODO)

You can use tmuxp, a session manager for tmux, to automatically configure tmux sessions.