Using the GNU Screen Terminal Multiplexer
Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Essentially, it’s a terminal program allowing you to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It’s particularly useful for dealing with multiple programs from the command line and for separating programs from the shell that started them.
One of the key benefits of Screen is its ability to allow you to start applications from one computer, detach, and then reconnect from a different computer to continue using the same application without having to restart it. This makes migrating between different workstations straightforward. Furthermore, Screen allows multiple computers to connect to the same session at once, which can be helpful for having one person follow along with another working on a server.
To start a basic Screen session, you simply use the screen command:
screenScreen offers several command-line flags to modify its behavior. Here are some common ones:
- -ls: Lists currently running screen sessions.
- -aS <screenname>: Creates and names a new screen session, including all capabilities.
- -r <name|pid>: Resumes a detached screen session, identifying it by name or process ID (pid).
- -x <name|pid>: Attaches to a screen session that is already attached elsewhere.
- -d <name|pid>: Detaches an attached screen session.
- -d -m $command: Starts a new screen session in detached mode and runs a specified command within it.
- -DR: Forcefully disconnects any other user attached to a session and reconnects with your user.
- -wipe: Removes all dead screen sessions, which are sessions that are unreachable.
To list your current screen sessions, you can use the -ls flag:
screen -lsThe output will show you the running sessions, typically including a process ID and status (e.g., “Attached”). For example, you might see: 26091.ttyp0.host (Attached). This indicates session 26091 is currently connected to a terminal.
If a screen session is currently attached and you need to detach it remotely, you can use the -d flag with the session’s ID or name:
screen -d 26091Be cautious when detaching sessions, especially if someone else is actively working in them, as this will end their connection to the session.
To reattach to a session that is detached, use the -r flag with the session’s ID or name:
screen -r 26091You can combine detaching and reattaching into a single command using the -d -r flags:
screen -d -r 26091To create a new screen session and assign it a specific name (e.g., “LW”), which makes it easier to reattach later, use the -S flag:
screen -S LWOnce inside a Screen session, you interact with it using default key bindings. These bindings are activated by pressing ctrl+a followed by another key.
Here are some common default key bindings:
- ctrl+a ?: Show key bindings.
- ctrl+a A: Set a title for the current window.
- ctrl+a c: Create a new window within the session.
- ctrl+a n: Switch to the next window.
- ctrl+a p: Switch to the previous window.
- ctrl+a 0-9: Switch to window number n (where n is the digit 0 through 9).
- ctrl+a w: Show a list of windows in the current session.
- ctrl+a “: Present a list of windows for selection.
- ctrl+a F: Resize the window to the current region size.
- ctrl+a [ or esc: Enter copy/scrollback mode.
- ctrl+a k: Destroy the current window.
Screen also allows you to split or tile the terminal window into multiple regions, each displaying a different window from your session.
- To split the screen vertically, use ctrl+a then | (pipe character).
- To split horizontally, use ctrl+a then S (uppercase ‘s’).
After splitting, you need to navigate into the new region using ctrl+a tab and then create a new window within that region using ctrl+a c before you can use it.
To unsplit the screen, use ctrl+a then Q (uppercase ‘q’). To switch between the different split regions, use ctrl+a then tab.
Screen is a highly versatile tool for managing your terminal environment, offering significant flexibility for handling multiple tasks and sessions remotely.