Virtual Terminal Sessions with Screen

Updated on 09/23/2020

Not so long ago, a co-worker and friend of mine introduced me to the UNIX application, screen. While I was really excited to learn of something so useful, I was also deeply saddened in realizing that it was right under my nose all this freaking time. In this short post, I’m going to show you how you can take advantage of this clever little utility.

So what is this screen thing? Screen is a UNIX utility that allows you to create/access multiple separate terminal sessions within a single terminal window or remote session. So, in English, this gives you the ability to fire up a terminal process, detach it and let it run and then re-attach it later on. When a detached session is reconnected, it picks back up where the process has continued to – not where you left off when you detached. This is an especially cool concept if you need to SSH into a remote machine and run some processes. You can detach your screen, disconnect your terminal, go do something else and when you reconnect later on, it’s as if you never left in the first place. What I really like about this utility though is that it is really easy to manage and is installed by default on UNIX machines.

So let’s see how it works. Fire up a new terminal window and test out the following commands:

# Start up a new screen session (no spaces allowed)
screen -S [session_name]

# Detach current screen session
Control + a + d

# List running screen processes
screen -ls

# Detach a screen process running in another terminal window
screen -d [session_name]

# Reattach an existing process
screen -r [session_name]

# Quit running session
screen -X -S [session_name] quit

Additionally, you can open and close new windows in any given screen session:

# Open a new window in the current screen socket
Control + a + c

# Close current window in the current screen socket
Control + a + k

To switch between open windows within a screen session you can either jump to the desired window number (ordered starting with 0) or cycle through the open windows:

# Jump to the first window in the screen session
Control + 0

# Jump to the third window in the screen session
COntrol + 2

# Toggle between the current and the previous open window (note this will not cycle through all open windows)
Control + a, Control + a

# Cycle through all open windows
Control + a, Space

or use Control + a + a to cycle through

Those are the main commands that I use most often. For example, when I am working on a web application, I will likely have at least one process constantly running – wether that be a server, compiling Sass, or multiple tasks managed with Grunt. It’s really nice to be able to set that process up on a detached screen session and forget about it until I’m ready to move on to something else.

For a full list of commands, run screen --help from the Terminal or check out this handy Quick Reference. I hope you found this as useful as I did and, as always, thanks for reading!

← Back to all notes

Copyright © 2021 Daniel W Robert. All rights reserved.