Screen is a window manager that allows to have several virtual terminals, several sessions and programs in text mode executing simultaneously in the same console. In this tutorial we will see how to use it.
For this tutorial, Linux Mint 18 has been used.
If you don’t have installed Screen, you can install it easily with
sudo apt-get update sudo apt-get install screen
2. Creating a screen
To create a screen, we just have to execute the
We will see an informative screen about the software itself. Just hit return to exit.
At this moment, a new screen will be generated inside the terminal window. You may have noticed it, since the buffer has been cleared.
And that’s it!
This has created a pipe file for this session in
/var/run/screen/S-<username>/ directory. The username I’m working with is named
julen, so, the directory is
In my case, the name for the session is
screen generates the sessions with the following format:
You may be wondering how to exit the screen. You can obviously close the terminal, but that would leave the session alive. If you want to kill it, you can execute the following command inside the session you want to kill:
You will see that you return to the “original” shell, and that
screen has told that, effectively, you have exited its screen:
[screen is terminating]
If you check the directory where
screen saves the sessions, you will notice that the previous session has disappeared.
2.1. Creating windows inside screens
We have seen that executing
screen in the host computer, creates a new session, which is saved in
/var/run/screen/S-<username>/ directory. If we would execute
screen n times in the host, we would have n sessions.
Inside each session, we can have other several screens, named windows. The windows are created, again, executing the
screen command. So, the following:
screen # We are know inside a screen session screen
Would only create a session in
This is because, as we have said, executing
screen inside a screen session will create a window inside that screen, not another session.
In the next sections we will see the commands available for these screens.
3. Screen commands options
screen command has many inline options. The following table shows and explains them.
|Create screen||Creates a session, as we have already seen.|
|Create screen with name||Creates a session with a specific name. This is, actually, the recommended way for creating sessions, for having identified each session with a proper name.|
|Create detached screen||This is for creating a screen that will be detached from its creation, for which |
|Show screens||Shows the screen sessions. This can also be done looking at the directory where there are stored, as we have seen before. But using this option (actually, one of them, |
|Reattach screen||s||Reattaches a detached session. If there’s only one session detached, it’s not necessary to specify the name/id; |
|Try to reattach screen, create new one if impossible||Tries to reattach a detached screen. If the screen couldn’t be reattached, a new session will be created. If no |
|Specify a shell for the screen||Creates a session with the specified shell. By default, the sessions are created with |
|Create a window with name||By default, the created windows inside a session are named |
|Delete screen||Kills the screen session. This would be equivalent to removing the pipe file from |
|Delete all screens||Not actually a |
4. Key bindings
When we are inside a screen and its windows, we have available some special key combinations to perform some actions, e.g., navigating through different windows.
These key bindings consist on pressing Ctrl + a, and then pressing the key(s) in question. Not at the same time: first, Ctrl + a; then, release them; and finally, press the key in question.
The following table shows the most used key bindings.
|Detach screen||Ctrl + a d||Detaches the screen.|
|Clear window||Ctrl + a C||Clears the window, just like with |
|Create window||Ctrl + a c||Creates a new window in the screen.|
|Show number and title of current window||Ctrl + a N||Shows a message with the window number and title like: This is window n (title).|
|Lock window||Ctrl + a Ctrl + x||Locks the window. Just like locking the host. The password to unlock the window is, of course, the password of the user where the session is running in.|
|List windows||Ctrl + a w||Shows a list of the windows of the current screen, with the number and the name of each one.|
|Rename current window||Ctrl + a A||Shows a menu for introducing a new name for the current window.|
|Go to window n||Ctrl + a n (0…9)||Navigates to the window.|
|Go to the next window||Ctrl + a n||Navigates to the next window.|
|Go to the previous window||Ctrl + a backspace||Navigates to the previous window (the opposite of the previous command).|
|List windows and select where to go||Ctrl + a “||Shows a menu of the current windows, allowing to select to which window navigate to.|
|Select the window to go to||Ctrl + a ‘||Prompts a menu for entering the window number to navigate to.|
|Kill the current window||Ctrl + a k||Kills the current window, asking for confirmation.|
|Kill all the windows, and terminate the session||Ctrl + a \||Kills all the window and the screen session, asking for confirmation.|
|Show help||Ctrl + a ?||Shows the key bindings.|
5. Customizing Screen
screen allows us to configure it as we want. It looks for the configuration in a file named
.screenrc in the user’s home directory; or system wide, in
5.1. Disabling startup message
You may already be tired of the startup message when you create a screen, that one about the version, copyright, etc. This can be disabled with the following line in the configuration file:
5.2. Setting default windows for each session
One of the most interesting possibilities is defining a set of default windows for every
screen session. For that, we just have to write the commands we would execute, in the configuration file.
For example, let’s say that for every
screen session we want, apart from a shell, a Python and PHP console. We could write the following in the configuration file:
screen -t Shell /bin/bash screen -t Python /usr/bin/python3ve screen -t PHP /usr/bin/php -a # Interactive mode. select 0 # After creating the windows, go to the first one.
Now, for the configuration shown above, you would have, for each
screen session, those three windows.
5.3. Key bindings
The remaining customization that is worth mentioning is the configuration of key bindings, for having a better experience.
A usual practice is to use the function keys (F1 – F12). For example, we could use them for accessing windows:
bindkey "^[[[A" select 1 # F1 -> window 1 bindkey "^[OQ" select 2 # F2 -> window 2 bindkey "^[OR" select 3 # F3 -> window 3 bindkey "^[OS" select 4 # F4 -> window 4 bindkey "^[[15~" select 5 # F5 -> window 5 bindkey "^[[17~" select 6 # F6 -> window 6 bindkey "^[[18~" select 7 # F7 -> window 7 bindkey "^[[19~" select 8 # F8 -> window 8 bindkey "^[[20~" select 9 # F9 -> window 9 bindkey "^[[21~" select 10 # F10 -> window 10 bindkey "^[[23~" prev # F11 -> previous window bindkey "^[[24~" next # F12 -> next window
Note: if you want to know which key code corresponds to each keystroke, you can use
And then press a key to know its code.
In this tutorial we have seen how to use the Screen utility, for managing several virtual windows in the same terminal. We have seen the command’s inline options, and also the special key combinations for when we are inside the virtual windows. To end up with the tutorial, we have shown how we can customize Screen with the configuration file.