Get Hands-on With Tmux
Last week I introduced tmux, a handy "terminal multiplexer." This week, we'll take a look at basic tmux usage and configuration.If you need to manage multiple sessions in the same terminal, consider Tmux, a handy "terminal multiplexer." This week, we take a look at its basic usage and configuration.
To start tmux, just run
tmux new-session if you're feeling verbose. Then you'll have a session with one window and your usual login shell. To add a second window, run
Ctrl-b c. (Same thing if you want a third, fourth, and so on.)
If you'd like to split your window into panes, each with its own shell, run
Ctrl-b " to split the window into two panes. That will split your
tmux session horizontally. If you prefer vertically, you can use
Ctrl-b %. To move between panes use
Now, all of the windows that are running in
tmux are in one session. If you start a second instance of tmux in another xterm or virtual console on the same machine, you've got a second session. You can see all of the sessions that are running by using
Ctrl-s. This will give a menu of all sessions and show how many windows are running in each session.
You may also run into a tiny quirk. Let's say you have one session running in an xterm that's been maximized, and one's running something like 80 columns by 50 rows. If you switch to the smaller session in the maximized window, tmux will respect the size of the original window and draw dots or something to consume the additional space. If you switch to the larger session in a smaller window, it will shrink the windows to fit. Not only will it shrink them to fit in the smaller window -- it will shrink them in the larger window as well. But if you switch back to the smaller session in the smaller window, tmux will resize the larger session to its original dimensions.
If you want to detach a session, that is leave tmux running in the background, it's just
Ctrl-b d. You can reattach a session by running
At the bottom of the tmux screen you'll see the windows and the processes running in them. For example, the bottom of my tmux session right now shows:
 0:top 1:[tmux]- 2:[vim]*
As with many *nix-related utilities, tmux starts its counting-up with 0 instead of 1. The first window is running
top, the next a tmux session without a process, and the next running Vim.
You probably will want to switch between sessions at some point, so here's how you do that: Go directly to a window by using
Ctrl-b n, where n is the number of the session. (So,
Ctrl-b 2 to get to Vim, in this example.) Or you could use
Ctrl-b l to move to the last window, and
Ctrl-b n to move to the next window.
Another way to switch between windows is to use
Ctrl-b w, which provides a list of all the windows that are running. Just use the arrow keys to highlight the session you want and hit Enter.
Copy and Paste
Those are the basics, but here's something interesting with tmux -- you can copy and paste from the session. To enter copy mode, use
Ctrl-b [. Now you can move the cursor over your screen to place it where you want to start selecting text. Then hit
Ctrl-Space to start copying, and
Ctrl-w to copy the text. When you want to paste the text, just hit
Like Screen, tmux is very configurable. I won't go into all the configuration options here, but it's worth noting that you can set a lot of options in your
~/.tmux.conf. For example, let's say you want to switch tmux's keybindings from Emacs-style to Vi-style, add this to the
setw -g mode-keys vi
Basically, this tells tmux to set the window options globally (
-g) using vi-style mode keys. Note that you can set this while running tmux by using the tmux command line:
Ctrl-b : will bring up command mode, and then type the command
setw -g mode-keys vi. Or
setw -g mode-keys emacs to return to the emacs style.
If you're running Debian, Ubuntu, or another Debian derivative you can find several tmux.conf examples under
/usr/share/doc/examples. See also the man page for
tmux, which provides oodles of options.
For a quick reference, I've listed the shortcuts that you'll want to use most often:
Ctrl-bthe prefix that sends a keybinding to tmux instead of to the shell or program running in tmux.
Ctrl-b ccreate a new window.
Ctrl-b "split the window horizontally.
Ctrl-b %split the window vertically.
Ctrl-b slist sessions.
Ctrl-b ddetach a session.
Ctrl-b [start copy.
Ctrl-wcopy text from selection.
Ctrl-b wselect from windows.
Ctrl-b llast window.
Ctrl-b nnext window.
Ctrl-b :command mode.
Like GNU Screen, tmux is instantly useful but can take a great deal of time to learn all the little nooks and crannies of the application and become really masterful. Spend a few weeks using tmux and I think you'll find it quite useful indeed.
Joe 'Zonker' Brockmeier is a freelance writer and editor with more than 10 years covering IT. Formerly the openSUSE Community Manager for Novell, Brockmeier has written for Linux Magazine, Sys Admin, Linux Pro Magazine, IBM developerWorks, Linux.com, CIO.com, Linux Weekly News, ZDNet, and many other publications. You can reach Zonker at email@example.com and follow him on Twitter.