How to Use `tmux` or `screen` for Terminal Multiplexing on FreeBSD

This article explains how to use tmux or screen for terminal multiplexing on FreeBSD.

In the world of system administration and development, efficiency is key. One of the most powerful tools at your disposal for maximizing productivity in a terminal environment is terminal multiplexing. Terminal multiplexers like tmux and screen allow you to manage multiple terminal sessions within a single window, making it easier to multitask, manage long-running processes, and maintain persistent sessions. This is particularly useful on Unix-like operating systems such as FreeBSD, where the terminal is often the primary interface for system management.

In this blog post, we’ll explore how to use tmux and screen for terminal multiplexing on FreeBSD. We’ll cover installation, basic usage, advanced features, and tips for getting the most out of these tools. Whether you’re a seasoned sysadmin or a developer looking to streamline your workflow, this guide will help you harness the power of terminal multiplexing.


What is Terminal Multiplexing?

Terminal multiplexing is the ability to run multiple terminal sessions within a single terminal window. This is achieved using tools like tmux and screen, which allow you to:

  • Split your terminal into multiple panes.
  • Run multiple programs simultaneously.
  • Detach and reattach sessions, even after disconnecting from the server.
  • Share sessions with other users for collaboration.

These features are invaluable for managing remote servers, running long processes, or simply organizing your workflow.


Why Use tmux or screen on FreeBSD?

FreeBSD is a powerful and versatile operating system often used for servers, networking, and development. Its lightweight and efficient design makes it an excellent choice for terminal-based workflows. By using tmux or screen on FreeBSD, you can:

  1. Persist Sessions: Keep your terminal sessions running even if you disconnect from the server.
  2. Multitask Efficiently: Work on multiple tasks simultaneously without opening multiple terminal windows.
  3. Improve Productivity: Use keyboard shortcuts to navigate between sessions, panes, and windows.
  4. Collaborate: Share your terminal session with others for pair programming or troubleshooting.

Installing tmux and screen on FreeBSD

Before diving into usage, you’ll need to install tmux and screen on your FreeBSD system. Both tools are available in the FreeBSD ports collection and can be installed using the pkg package manager.

Installing tmux

To install tmux, run the following command:

sudo pkg install tmux

Installing screen

To install screen, run the following command:

sudo pkg install screen

Once installed, you can verify the installation by checking the version:

tmux -V
screen -v

Getting Started with tmux

tmux (short for “terminal multiplexer”) is a modern and feature-rich tool for managing terminal sessions. It uses a client-server model, allowing you to detach and reattach sessions seamlessly.

Starting a tmux Session

To start a new tmux session, simply run:

tmux

This will create a new session with a single window. You can also name your session for easier identification:

tmux new -s mysession

Basic tmux Commands

tmux uses a prefix key to trigger commands. By default, this is Ctrl+b. After pressing the prefix key, you can use the following shortcuts:

  • Create a new window: Ctrl+b c
  • Switch to the next window: Ctrl+b n
  • Switch to the previous window: Ctrl+b p
  • Split pane horizontally: Ctrl+b "
  • Split pane vertically: Ctrl+b %
  • Switch between panes: Ctrl+b o
  • Detach from the session: Ctrl+b d
  • List sessions: tmux ls
  • Reattach to a session: tmux attach -t mysession

Customizing tmux

tmux is highly customizable. You can configure it by editing the ~/.tmux.conf file. For example, to change the prefix key to Ctrl+a, add the following line:

unbind C-b
set-option -g prefix C-a
bind C-a send-prefix

Getting Started with screen

screen is an older but still widely used terminal multiplexer. It’s lightweight and straightforward, making it a good choice for simpler workflows.

Starting a screen Session

To start a new screen session, run:

screen

You can also name your session:

screen -S mysession

Basic screen Commands

screen uses the Ctrl+a prefix key for commands. Here are some essential shortcuts:

  • Create a new window: Ctrl+a c
  • Switch to the next window: Ctrl+a n
  • Switch to the previous window: Ctrl+a p
  • Split screen horizontally: Ctrl+a S
  • Detach from the session: Ctrl+a d
  • List sessions: screen -ls
  • Reattach to a session: screen -r mysession

Customizing screen

You can customize screen by editing the ~/.screenrc file. For example, to enable a status bar, add the following lines:

hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][ %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'

Advanced Features

Session Management

Both tmux and screen allow you to detach and reattach sessions. This is particularly useful for remote work, as you can disconnect from a server without interrupting your processes.

  • In tmux, use Ctrl+b d to detach and tmux attach -t mysession to reattach.
  • In screen, use Ctrl+a d to detach and screen -r mysession to reattach.

Window and Pane Management

Both tools allow you to split your terminal into multiple panes and windows. This is useful for running multiple commands side by side.

  • In tmux, use Ctrl+b " for horizontal splits and Ctrl+b % for vertical splits.
  • In screen, use Ctrl+a S for horizontal splits.

Scripting and Automation

tmux and screen can be scripted to automate repetitive tasks. For example, you can create a tmux session with predefined windows and panes using a shell script:

tmux new-session -d -s mysession
tmux send-keys -t mysession "htop" C-m
tmux split-window -v -t mysession
tmux send-keys -t mysession "tail -f /var/log/messages" C-m
tmux attach -t mysession

Sharing Sessions

Both tools support session sharing, which is useful for collaboration. In tmux, you can share a session by attaching to the same session ID. In screen, you can use the -x flag to share a session:

screen -x mysession

Choosing Between tmux and screen

While both tools are excellent for terminal multiplexing, they have some differences:

  • tmux: More modern, with better customization and scripting support. Ideal for complex workflows.
  • screen: Lightweight and simpler, with a smaller learning curve. Suitable for basic use cases.

Your choice will depend on your specific needs and preferences.


Conclusion

Terminal multiplexing is an essential skill for anyone working with FreeBSD or other Unix-like systems. By mastering tmux or screen, you can significantly improve your productivity and streamline your workflow. Whether you prefer the modern features of tmux or the simplicity of screen, both tools offer powerful capabilities for managing terminal sessions.

To get started, install tmux or screen on your FreeBSD system, experiment with the basic commands, and explore the advanced features. With practice, you’ll find that terminal multiplexing becomes an indispensable part of your toolkit.