›INDEX
Last Updated:

A Guide to Screen and Tmux

When working with remote servers or running long-running tasks, keeping your terminal sessions active and organized can be a challenge. Tools like screen and tmux help you manage multiple terminal sessions, detach and reattach to running processes, and organize your workspace with ease.

In this guide, we’ll explore what screen and tmux are, explain their core features, and show you how to use them to make your remote work more efficient and resilient. By the end, you'll be able to keep your workflows running uninterrupted, even when you disconnect from the terminal.

tmux has a lot of configuration that can really help it become a great productivity tool. I use it on all my terminals and I have multiple plugins to make it even more powerful!

Why Use screen or tmux?

Without screen or tmux, if you disconnect from your SSH session, any running commands or processes will terminate, potentially interrupting important tasks. These tools allow you to detach from sessions and leave processes running in the background. When you reconnect, you can simply reattach to your existing sessions and pick up right where you left off.

Screen — The Basic Terminal Multiplexer

screen is a utility that provides the ability to run multiple shell sessions within a single SSH connection. It’s simpler than tmux, making it a good starting point if you’re new to terminal multiplexers.

Basic screen Commands

  1. Starting a New Screen Session
screen

This creates a new screen session and opens a shell prompt within it. Once inside, you can run commands as usual.

  1. Detaching from a Screen Session

Press Ctrl + a followed by d. This detaches you from the screen session, leaving the processes inside running in the background.

  1. Listing Screen Sessions
screen -ls

This shows a list of active screen sessions and their IDs.

  1. Reattaching to a Screen Session
screen -r <session-id>

Use the session ID from the screen -ls output to reattach to a running session. If only one session exists, you can simply run screen -r without the ID.

  1. Creating a Named Screen Session

If you often have multiple sessions, it’s helpful to name them:

screen -S <session-name>

Replace <session-name> with something descriptive, like project-build. This makes it easier to identify and reconnect to specific sessions later.

  1. Killing a Screen Session

To terminate a screen session:

screen -X -S <session-name> quit

This command forcefully ends the session specified by its name or ID.

Useful Screen Shortcuts

  • Ctrl + a, c: Create a new window within the screen session.
  • Ctrl + a, n: Switch to the next window.
  • Ctrl + a, p: Switch to the previous window.
  • Ctrl + a, ": List all windows.

Practical Example: Using Screen for Long-Running Scripts

Suppose you want to run a long data processing script on a remote server, but you don’t want it to terminate if your SSH session drops:

  1. Start a screen session:
screen -S data-processing
  1. Run your command:
python data_script.py
  1. Detach from the session using Ctrl + a, d.

  2. Later, if you want to check on the process, reattach using:

screen -r data-processing

This way, your script keeps running even if you disconnect from the server.

Tmux — The Modern Terminal Multiplexer

tmux is a more feature-rich alternative to screen with additional capabilities for window management, session management, and scripting. It allows you to create multiple terminal windows within a single session, split panes within windows, and even synchronize inputs across multiple panes.

Basic tmux Commands

  1. Starting a New Tmux Session
tmux

This starts a new session. You’ll see a status bar at the bottom indicating you’re inside a tmux session.

  1. Creating a Named Tmux Session
tmux new -s <session-name>

Replace <session-name> with something meaningful, like development. This is useful for managing multiple sessions.

  1. Detaching from a Tmux Session

Press Ctrl + b followed by d. This detaches from the current tmux session, leaving processes running in the background.

  1. Listing Tmux Sessions
tmux ls

This command lists all active tmux sessions.

  1. Reattaching to a Tmux Session
tmux attach -t <session-name>

Use the session name to reattach to a specific tmux session. If only one session is running, you can omit the -t <session-name> part.

  1. Killing a Tmux Session
tmux kill-session -t <session-name>

This command terminates a session by name.

Tmux Window and Pane Management

Unlike screen, tmux allows you to split your terminal into multiple panes, making it perfect for multitasking.

  • Creating a New Window: Ctrl + b, c

  • Splitting Horizontally: Ctrl + b, "
    (Splits the current window into two horizontal panes)

  • Splitting Vertically: Ctrl + b, %
    (Splits the current window into two vertical panes)

  • Navigating Between Panes: Ctrl + b followed by an arrow key

  • Resizing Panes: Ctrl + b followed by : and then use the commands like resize-pane -L 10 to resize left by 10 units.

Example: Organizing Your Workflow with Tmux

Let’s say you’re working on a web development project. You can set up a tmux session with separate windows for editing code, running a web server, and monitoring logs:

  1. Start a new session:
tmux new -s webdev
  1. Create multiple windows:
  • Use Ctrl + b, c to create a window for editing code (vim or nano).
  • Create another window (Ctrl + b, c) to start your server (python manage.py runserver).
  • Create a third window to monitor logs (tail -f /var/log/server.log).
  1. Switch between windows:
  • Use Ctrl + b, n for the next window.
  • Use Ctrl + b, p for the previous window.

Sharing and Collaboration with Tmux

One powerful feature of tmux is its ability to share sessions. If you’re collaborating with someone remotely, you can both attach to the same tmux session and see each other’s inputs.

  1. Create a shared tmux session:
tmux new -s shared
  1. Have another user on the same server attach to the session:
tmux attach -t shared

Now, both users can type and see each other’s actions, making it perfect for pair programming or troubleshooting.

Decking Out Tmux

If you'd like a fully decked out tmux with colors, icons, and custom keybinds, then you can watch this video where Dreams Of Code goes over his tmux configuration: Dreams Of Code | YouTube.

You'll get sometime like this as a result:

tmux-demo-first-figure

tmux-demo-second-figure

Choosing Between screen and tmux

  • Use screen if you need a simple tool for keeping terminal sessions active and have no need for complex layouts or scripting.

  • Use tmux if you want advanced features like split panes, window management, and session sharing.

Summary

Both screen and tmux are powerful tools for managing terminal sessions, but tmux offers more flexibility and features. Knowing how to use these tools will help you maintain long-running processes, organize your terminal windows, and enhance your productivity on remote systems.

  • screen: Easy to use, good for beginners, simple session management.
  • tmux: Feature-rich, supports panes and windows, ideal for complex workflows.

With these tools, you can now work confidently on remote systems, never losing your place or disrupting your workflows!

Enjoy the notes on this website? Consider supporting me in this adventure in you preferred way: Support me.