Linux Format

tmux: a modern command-line workflow

You can’t teach an old dog new tricks, but you can have fun with terminals – it just takes an extra command to run.

-

The casual Linux user isn’t going to open their terminal very often. For us administra­tors things are different. We believe in automation, which is something graphical tools aren’t good at. We often access our boxes remotely, where using command line saves us bandwidth. Moreover, it could be the only viable option on a choppy GPRS connection.

Regardless of our role, we all deserve some comfort when doing our job. Having to start over just because an SSH connection dropped is a pain. Sometimes, we want to run commands and see system logs as we go. And we certainly want to stop administer­ing a server, close our laptop lid, go home and then continue where we left.

Staring at the screen

The type of program that facilitate­s the above scenarios is often dubbed a “terminal multiplexe­r”. Typically, it would enable you to share one (pseudo)terminal among several windows, which you might call “tabs” as well. Terminal multiplexe­rs typically stay resident after logout, which means you can rejoin the session later.

A classical example of this family is GNUscreen. It’s four years older than Linux and almost certainly available in your distro’s package repositori­es. GNU screen isn’t eye candy ( itmakesmel­ookpretty–Ed), but it does the job well. It’s somewhat ubiquitous, thus could be the only way to go on a box you don’t own. So you may find it useful to learn the most basic key bindings, such as Ctrl-A C (create a window), Ctrl-A N/P (move to the next or previous window) and Ctrl-A D (detach a session). I used GNUscreen for many years but eventually switched to tmux.

The latter’s name stands for – you’ve guessed it – “terminal multiplexe­r”, and tmux should be in your distro’s repositori­es as well. tmux isn’t part of GNU, yet it’s more configurab­le and sports some advanced features such as plugins. If this sounds like it’s worth a closer look, keep reading.

Sessions and windows

Typically, you create a new tmux session with tmux . This is equivalent to tmux new . If you want to give your session a name, call it tmux new -s mysession .

Naming sessions helps if you run more than one of them in parallel. tmux attach (or tmux at for short) attaches to the last active session. To attach a specific session instead, run tmux at -t mysession . You can list available sessions with tmux ls . The listing includes session numbers that you can use to attach to a specific yet unnamed session.

Internally, tmux consist of a client and a server, both being a single binary. Server hosts sessions and processes they contain. When you call tmux attach , you start a client and attach it to one of the sessions hosted on the particular server. Servers are usually per user per (pseudo)terminal, so different tabs or windows in your terminal emulator (such as Konsole) won’t see other sessions. It’s possible to switch between sessions on the same server with Ctrl-B ( and ), though. Ctrl-B is a so-called “prefix key”, a default that you can redefine. Some might feel backtick key “`” is the better option.

Regardless of how you get into a tmux session, you are likely to see a green bar at the bottom of your screen. The exact colour and exterior may vary depending on the tmux plugins your installati­on has, but the idea is always the same. The bottom line enumerates “windows” within tmux session. Windows run interactiv­e processes, typically shells.

Most keystrokes that you do go directly to these interactiv­e processes. To call into tmux you type Ctrl-B

or any other prefix key you set. To this end, Ctrl-B c creates a new window, and Ctrl-B 0-9 switches to the window under the given number. If you happen to have more than 10 windows, typing Ctrl-B ‘ would enable you to enter the desired number, and pressing Ctrl-B w would bring the menu. To rename a window, use Ctrl-B ,. It is also possible to rename a session from within tmux with Ctrl-B $.

The most straightfo­rward way to close a window is to terminate the process running inside. If it’s a shell, exit should do. However, if this process is misbehavin­g and refuses shutdown requests, you can close the window forcibly with Ctrl-B &. In this case, tmux asks for the confirmati­on. When you close the last or the only window in a session, tmux terminates itself. You can always detach a session, leaving all your precious processes running, with Ctrl-B d.

Painless panes

So far, tmux might look pretty basic to you. Things start to get interestin­g when you learn you can further split each window into rectangula­r regions called “panes”.

Panes facilitate many use cases. For instance, you may write code and have a man page for functions that you use opened at the same time. Sometimes, it’s useful to watch service logs as you carry out configurat­ion tweaks. Some of us, including myself, prefer to group related SSH sessions in one window, although it’s largely a matter of taste.

To create a pane, you split the current window either vertically with Ctrl-B “, or horizontal­ly with Ctrl+B %. These are not the perfect mnemonics, and redefining them to Ctrl-B | and Ctrl-B - (that is, vertical and horizontal bars) is recommende­d. Ctrl-B Arrows moves you to the pane left, right, top or bottom of the current one, and you can also type Ctrl-B o and Ctrl-B ; to cycle back and forth between panes. Should you want to expand a pane into a full window, Ctrl-B ! does just that. Alternativ­ely, you can “zoom into” the current pane temporaril­y with Ctrl-B z. The first keystroke expands the pane up to a size of the window, and the second one brings it back to normal. I often use this when there’s some lengthy output in one of my SSH consoles.

When you split a window, the resulting panes will be of equal size, yet they don’t have to. Ctrl-B Ctrl-Arrow enlarges or shrinks a pane in the respective direction character by character, while Ctrl-B Meta+Arrow does the same but in greater steps (five characters). The Meta key isn’t found on most modern keyboards, but it typically maps to one of Alt keys.

Resizing aside, you may find it useful to re-arrange panes in a window. Ctrl-B Ctrl-o rotates them forward. Ctrl-B Meta-o does the same but in the opposite direction. Ctrl-B { and Ctrl-B } swap the current pane with the previous or next one, respective­ly.

Closing a pane is similar to closing a window. Either terminate a process running inside, or kill the pane itself with Ctrl-B x.

Copying and pasting

In fact, tmux isn’t just a fancy window manager. There are some hidden gems, and for starters, let’s look at the history buffer. It’s true that most graphical terminal emulators support scrolling, yet this may not work if you run tmux inside them. So tmux implements its own scrolling mechanism, as well as copying and pasting means. The latter are self-contained and don’t rely on X or in fact any other clipboard within Linux.

Try it yourself: spawn a command that produces a lot of output, such as ls -lR , then type Ctrl-B [. Lines will stop flying by, and you’ll be able to scroll the output using arrow keys, PgUp and PgDown. Actually, tmux can emulate Vi or Emacs key bindings, so the respective navigation keys (say, “hjkl”) are also available if the feature is enabled. Of course, all of these works even if you don’t have a program spitting lines of text in the background. We just needed it to generate some output to scroll through.

With Vi key bindings enabled (see below), switch to copy mode with Ctrl-B [ then type “?”. You’ll see a prompt enabling you to search the history “up” (backwards). “/” does the same but searches “down”. “n” moves to the next occurrence, “N” takes you to the previous one.

To copy some text, start selection with Space. Move around (remember, most Vi keys work!) then press Enter. If you wonder whether it’s possible to use “v” and “y” instead, the answer is “yes”, but it has to wait until the next section. Copying a selection ends copy mode; to exit without making any selection, just press q.

At your command

Now when you see tmux is like Vim, it should come as no surprise that it has its own command line. You summon it with Ctrl-B :. Command history and Tab completion are both supported. There’s no built-in help in tmux (other than Ctrl-B ? which shows current key bindings), but if you put the command wrong, tmux shows a synopsis for a split second. If you aren’t able to read it that fast, Ctrl-B ~ reveals the most recent message.

Many commands come via ~/.tmux.conf , which gets sourced at the startup. This is what a typical Vim user may have there:

set-window-option -g mode-keys vi bind-key -T copy-mode-vi ‘v’ send -X begin-selection bind-key -T copy-mode-vi ‘y’ send -X copy-selection

In this snippet, we enable Vi mode key bindings and make “v” and “y” keys to send begin-selection and

copy-selection commands to tmux when in copy mode.

Most key bindings so far were really shortcuts for the respective tmux commands.

Some commands don’t have an associated key binding by default. A well-known example is the command to swap two windows:

swap-window -s 1 -t 0

This turns the first window into the second and vice versa ( tmux counts from 0). Here, you refer to windows by their indexes. If you were to swap panes instead, you’d use a swap-pane command. Ctrl-B q (briefly) shows the pane indices you need.

This is not the only addressing mode available, though. Windows can be referred to by their names, or even shell globs ( bash* ) to match against these names. For panes, a relative location such as {left-of} , {previous} or {next} can be used. The last two work for windows as well, and can be further abbreviate­d as + or - , respective­ly, or suffixed with an offset ( +2 ). Assuming window 2 was active, the above command can be rewritten as:

swap-window -t -2

The following command swaps the current pane with its left neighbour:

swap-pane -t {left-of}

Speaking of panes, it’s sometimes convenient to arrange them in predefined layouts, such as even-

horizontal, main-horizontal, the respective vertical counterpar­ts, or tiled. There are actually key bindings for that (Ctrl-B Meta+Digit), although you may have a hard time making them work in the graphical terminal: Alt+Digit is somewhat oversubscr­ibed. So for instance, select-layout main-horizontal may save you a great deal of manual resizing. Please try these layouts yourself to better understand their behaviour.

If you spend a decent amount of time in the terminal, whether it’s graphical or text-based, tmux is certainly worth looking at. It helps to keep the workspace uncluttere­d and has many plugins to boost productivi­ty. Just find the ones which are “yours”, and very soon you’ll be surprised how you were able to manage without it.

 ??  ?? The GNU screen does its job well, but it’s not much to look at.
The GNU screen does its job well, but it’s not much to look at.
 ??  ?? At first glance, tmux isn’t all that different from a GNU screen. But at least it assumes that your monitor can handle colour!
At first glance, tmux isn’t all that different from a GNU screen. But at least it assumes that your monitor can handle colour!
 ??  ?? tmux understand­s a few dozens of commands; the tmux(1) man page lists them all, along with arguments and key bindings.
tmux understand­s a few dozens of commands; the tmux(1) man page lists them all, along with arguments and key bindings.
 ??  ?? This pane layout is dubbed “main-vertical”. Numbers are pane indexes. Note the clock in the background: I brought it up with Ctrl-B t.
This pane layout is dubbed “main-vertical”. Numbers are pane indexes. Note the clock in the background: I brought it up with Ctrl-B t.

Newspapers in English

Newspapers from Australia