[ /howto/tmux ]

tmux is pretty awesome. I wish I'd found it sooner.

tmux is a terminal multiplexer. It gives you far more control over your terminal than just using tabs. It also lets you detach and reattach a terminal session any time you want. Even better, it's highly configurable.

If you don't use your terminal, you won't have any use for tmux, though, you may be able to appreciate how well it addresses the UI shortcomings of interacting with your system via the terminal. I never open multiple tabs in my terminal anymore. Instead, I prefer to run in full screen mode, with tmux allowing me allocate exactly sized portions of my screen to certain processes.

If you're running a Debian derivative like Ubuntu, you can install tmux with sudo apt-get install tmux. It's also available via Brew, pacman, and most other OS/distro-specific package managers.

Basic Usage

Once it finishes installing, start a session by running tmux. You'll notice it looks just almost identical to your normal terminal. That's true, but there are things you need to be aware of when using tmux. Because you need to be able to send commands to the multiplexer itself, as well as the programs that are contained within it, you need some way to distinguish commands. tmux accomplishes this through the use of a prefix command.

The default prefix command is ctrl-b, that is, hitting the b key while holding the ctrl key. Once you've done that, the next key that you enter will trigger whatever tmux command has been bound to that key.

For the duration of this article, I'll substitute λ for the prefix command, such that λ! will mean hold ctrl and press b, then release ctrl and enter an exclamation mark ("!").

Give it a try!

  1. To split your screen into panes, try running λ" or λ% for a horizontal and vertical panes respectively.
  2. Use λo and λ<arrow-key> to navigate between panes.
  3. Use λ{ or λ} to move panes around within the current window.

You should see something like this:

tmux screenshot

tmux show -g | cat > ~/.tmux.conf # to generate a new user conf.
Editing this file will change your preferences for new sessions.

I started with https://github.com/prurigro/darkcloud-tmuxconfig.git and modified it to suit my preferences.

Sessions

tmux lets you create sessions, within which you can create windows. Any processes that you start within a session have that session as their parent process. In simpler terms, that means that if for some reason you lose your terminal (like if your terminal window crashes), those processes don't crash with it. All processes within your tmux session will continue to run in the background.

You can detach from a session by running λd, and reattach to that session by running tmux a from any terminal on the same system. My favourite use case for this is to keep processes running on my servers. I connect to them via ssh, reattach, and check in on what has happened since I last attached. This is especially convenient for applications like irssi (a command line irc client). My server rarely reboots, so I'm always connected to my favourite irc networks. Even though irc does nothing to retrieve backlog when you client reconnects, this workaround means I never miss a thing.

irssi running in tmux

tmux and ssh also make it easy to use lots of lightweight tools together on remote computers. I like to be able to pick up my laptop and move around, but that's difficult when I have hard drives attached. Using tmux and ssh, I can use vim to write code on another computer on the same network,but move around the house (usually to go make coffee or snacks). When I'm within hearing range of my main computer, I often use it to play music. Many modern music players boast about their ability to let you control them remotely from other devices. I achieve the same behaviour using command line utilities cmus and alsamixer.

I still have to use a compatible device (one which has ssh), but I'm not locked into a particular hardware or software vendor. Additionally, control of my music player is limited to devices which can access my sshd, namely those which carry authorized public keys. That means strong security, and I don't have to remember any passwords.

cmus and alsamixer in tmux

You may also find Lynx (The premier text-only browser) useful. The more you use tmux, the more you'll want to use command line applications. It has very nearly replaced my desktop environment for all purposes.

lynx in tmux

The following commands are meant to be used from the command line, and are geared towards session information:

tmux new -s myname # start new with session name  
tmux a  #  (or at, or attach)
tmux a -t myname # attach to named
tmux ls # list sessions
tmux kill-session -t myname # kill session

These commands are meant to be used from within your tmux session, but perform similar functions:

λs # list sessions
λ$ # name session

Windows

Within each tmux session, you can have multiple windows. A window functions similarly to a conventional terminal tab. It occupies the full size of your terminal window, but belongs to a session. You can create vast numbers of windows, and group related applications together.

λc # new window
λw # list windows
λf # find window
λ, # name window
λ& # kill window

Panes

Panes are the smallest sensible unit within the tmux architecture. Panes belong to windows. They can be subdivided vertically or horizontally, and reorganized within a window.

λz # toggle full screen mode for the current pane
λ! # move the current pane into a new window 
λ{arrow-key} # navigate between panes using directional controls