Linux Screen
Linux Screen
Unless you’re
in a remote environment where nothing else is available and/or you can’t
install another multiplexer, you should probably not be using screen. One
reason is that it’s not actively maintained anymore, and another is that it’s
not very flexible and lacks a number of features modern terminal
multiplexers have.
tmux
tmux is a flexible and rich terminal multiplexer that you can bend to your
needs. As you can see in Figure 3-8, there are three core elements you’re
interacting with in tmux, from coarse-grained to fine-grained units:Figure 3-8. The tmux elements:
sessions, windows, and panes
Sessions
A logical unit that you can think of as a working environment dedicated
to a specific task such as “working on project X” or “writing blog post
Y.” It’s the container for all other units.
Windows
You can think of a window as a tab in a browser, belonging to a session.
It’s optional to use, and often you only have one window per session.
Panes
These are your workhorses, effectively a single shell instance running.
A pane is part of a window, and you can easily split it vertically orhorizontally, as well as
expand/collapse it (think: zoom) and close panes
as you need them.
Just like screen, in tmux you have the ability to attach and detach a
session. Let’s assume we start from scratch, let’s launch it with a session
called test:
$ tmux new -s test
With the preceding command, tmux is running as a server, and you find
yourself in a shell you’ve configured in tmux, running as the client. This
client/server model allows you to create, enter, leave, and destroy sessions
and use the shells running in it without having to think of the processes
running (or failing) in it.
tmux uses Ctrl+b as the default keyboard shortcut, also called prefix or
trigger. So for example, to list all windows, you would press Ctrl+b and
then w, or to expand the current (active) pane, you would use Ctrl+b and
then z.
TIP
In tmux the default trigger is Ctrl+b. To improve the flow, I mapped the trigger to an
unused key, so a single keystroke is sufficient. I did this by first mapping the trigger to
the Home key in tmux and then mapping that Home key to the Caps Lock key by
changing its mapping in /usr/share/X11/xkb/symbols/pc to key <CAPS> { [ Home ]
};.
This double-mapping was a workaround I needed to do. Depending on your target key
or terminal, you might not have to do this, but I encourage you to map Ctrl+b to an
unused key you can easily reach since you will press it many times a day.
You can now use any of the commands listed in Table 3-4 to manage further
sessions, windows, and panes. Also, when pressing Ctrl+b+d, you can
detach sessions. This means effectively that you put tmux into the
background.When you then start a new terminal instance or, say, you ssh to your
machine from a remote place, you can then attach to an existing session, so
let’s do that with the test session we created earlier:
$ tmux attach -t test
Attach to existing session called test. Note that if you want to detach
the session from its previous terminal, you would also supply the -d
parameter.
Table 3-4 lists common tmux commands grouped by the units discussed,
from widest scope (session) to narrowest (pane).T
a
b
l
e
3
-
4
.
t
m
u
x
r
e
f
e
r
e
n
c
e
TargetTaskCommand
SessionCreate new:new -s NAME
SessionRenametrigger + $
SessionList alltrigger + sSessionClosetrigger
WindowCreate newtrigger + c
WindowRenametrigger + ,
WindowSwitch totrigger + 1 … 9
WindowList alltrigger + w
WindowClosetrigger + &
PaneSplit horizontaltrigger + "
PaneSplit verticaltrigger + %
PaneToggletrigger + z
PaneClosetrigger + x
Now that you have a basic idea of how to use tmux, let’s turn our attention
to configuring and customizing it. My .tmux.conf looks as follows:
unbind C-b
set -g prefix Home
bind Home send-prefix
bind r source-file ~/.tmux.conf \; display "tmux config reloaded :)"
bind \\ split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
bind X confirm-before kill-session
set -s escape-time 1
set-option -g mouse on
set -g default-terminal "screen-256color"
set-option -g status-position topset -g status-bg colour103
set -g status-fg colour215
set -g status-right-length 120
set -g status-left-length 50
set -g window-status-style fg=colour215
set -g pane-active-border-style fg=colour215
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-restore 'on'
run '~/.tmux/plugins/tpm/tpm'
This line and the next two lines change the trigger to Home.
Reload config via trigger + r.
This line and the next redefine pane splitting; retain current directory of
existing pane.
Adds shortcuts for new and kill sessions.
No delays.
Enable mouse selections.
Set the default terminal mode to 256-color mode.
Theme settings (next six lines).
From here to the end: plug-in management.
First install tpm, the tmux plug-in manager, and then trigger + I for the
plug-ins. The plug-ins used here are the following:
tmux-resurrect
Allows you to restore sessions with Ctrl+s (save) and Ctrl+r (restore)
tmux-continuum
Automatically saves/restores a session (15-minute interval)
Figure 3-9 shows my Alacritty terminal running tmux. You can see the
sessions with the shortcuts 0 to 9, located in the left upper corner.Figure 3-9. An example tmux
instance in action, showing available sessions
While tmux certainly is an excellent choice, there are indeed other options
than tmux, so let’s have a peek.
Other Multiplexers
Other terminal multiplexers you can have a look at and try out include the
following:
tmuxinator
A meta-tool allowing you to manage tmux sessions
Byobu
A wrapper around either screen or tmux; it’s especially interesting if
you’re using the Ubuntu- or Debian-based Linux distros
Zellij
Calls itself a terminal workspace, is written in Rust, and goes beyond
what tmux offers, including a layout engine and a powerful plug-insystem
dvtm
Brings the concept of tiling window management to the terminal; it’s
powerful but has a learning curve like tmux
3mux
A simple terminal multiplexer written in Go; it’s easy to use but not as
powerful as tmux
With this quick review of multiplexer options out of the way, let’s talk
about selecting one.