Define a large GNU screen profile using .screenrc

Contents

screen, the Linux utility that enables you to start a 'screen’ inside a terminal, it is by default a little misconfigured. Learn how to create a great screen profile that loads automatically every time you use the screen.

What is it screen?

Screen is a GNU utility (GNU is a great collection of open source software, and their grouping into an operating system is commonly known as Linux) which enables you to start one or more screens within your terminal.

You may ask why I would want to start a screen within my terminal, and can set screen better / further away? Sure. Imagine you are using a remote SSH server and, suddenly, your network connection is interrupted, right in the middle of database migration or production backup, which was running inside your terminal window. It happens regularly, and the problem is that any command that was running when the connection was broken is now terminated, regardless of the state you were in.

This clearly annoying situation is one you want to avoid at all costs.. You can not only leave the system or software package in an undefined state, but all the work done so far can simply be lost.

welcome to screen, the Linux utility that enables you to start a virtual terminal session (a ‘screen‘) within your terminal session, with the main and great benefit that said screen session will not be terminated every time the network connection is interrupted, or when you close the terminal session that started the screen session (the parent procedure). You can just wait until your network connection comes back, or open another terminal if you are not connected via SSH and working only on the local machine, and just reconnect to your screen session.

Excellent? Let's install quickly screen then, if you haven't done it yet:

installing screen

Install screen on your Debian-based Linux distribution / Apt (like Ubuntu and Mint), run the following command in your terminal:

sudo apt install screen

Install screen on your RedHat-based Linux distribution / Yum (like RHEL, Centos and Fedora), run the following command in your terminal:

sudo yum install screen

Now that the screen is installed, you can start using it right away. Just run screen in the command line and you are already inside. You may need to press enter or space to go through the screen Welcome screen.

The Linux GNU Screen Welcome Screen

You will see a new command prompt. Press the key sequence CTRL+a > CTRL+d to return to the command prompt of the main shell session. Then, can execute screen -ls to view a list of active screen sessions. Write the command (in the main shell you are in now if you followed the text identically), screen -d -r will bring you back to screen session, as long as there is only one active screen session (can start many).

If there is more than one screen session running on your system (and it is his property), you can type screen -d -r NAME where NAME is the name of a screen according to the list screen -ls, as an example 367434.pts-1.roel1, even if you only specify any part before or after the point (What 367 O pts), which still uniquely identifies a particular screen session, it is also enough.

Let's see how we can now set a great screen profile, based on an excerpt from setup_server.sh, located in the Percona-QA GitHub repository (GPLv2 licensed), a script created to configure a server for QA testing. I do not recommend that you run this script, since it is outdated, is used for reference only and, as usual, run manually step by step. The only section we will use is the .screenrc code, reproduced below.

Defining a large .screenrc configuration

the .screenrc The file is a configuration file hidden in your home directory. To edit it (it probably doesn't exist yet, especially if you just installed screen, just use your favorite text editor and open the file ~/.screenrc. If you have experience with Vim, you can simply type in your terminal prompt vi ~/.screenrc. For more information about Vi / Vim, consider reading our post Establishing a Great Vim Profile Using .vimrc.

Once you have opened ~/.screenrc, just add the following block of text:

# General vbell configuration in vbell_msg '! Bell!'  autodetach en startup_message off defscrollback 10000 # Termcapinfo para xterm termcapinfo xterm * Z0 = E[?3h:Z1=E[?3l:is=E[rE[mE[2JE[HE[?7hE[?1;4;6l   # Do not resize window
termcapinfo xterm* OL=1000                                                    # Increase output buffer for speed

# Remove various keyboard bindings
bind x    # Do not lock screen
bind ^x   # Idem
bind h    # Do not write out copy of screen to disk
bind ^h   # Idem
bind ^   # Do not kill all windows/exit screen
bind .    # Disable dumptermcap

# Add keyboard bindings
bind } history
bind k kill

Our .screenrc template

If you do not want to use a text editor, or want to somehow script the installation of this .screenrc file into many systems, you could use the following code instead, which will – whenever pasted, or executed from another script, create the file ~/.screenrc for the user who is executing the script. Let’s call our script make_screenrc.sh or similar.

#!/bin/bash
# Create screen script ~/.screenrc
touch ~/.screenrc
if [ -With "$(cat ~/.screenrc|grep 'termcapinfo xterm')" ];  después cat << EOF> ~ / .screenrc # General configuration vbell on vbell_msg '! Bell!' autodetach on startup_message off defscrollback 10000 # Termcapinfo for xterm termcapinfo xterm * Z0 = E[? 3h: Z1 = E[? 3l: is = E[r E[m E[2J E[H E[ ? 7h E[? 1; 4; 6l # Do not resize the termcapinfo xterm window * OL = 1000 # Increase the output buffer for speed # Remove multiple keyboard links bind x # Do not block screen link ^ x # Idem enlazar h # Don't write a screen copy to disk link ^ h # Idem enlazar ^  # Don't kill all the windows / exit screen connection.  # Deshabilitar dump term cap # Add keyboard bind links} history bind k kill EOF fi

The script presets a series of settings, and most of the ones shown here are self explanatory. As an example, startup_message off just turn off the splash screen we saw earlier. defscrollback 10000 sets our backward scrolling to 10000 lines (can enter scroll mode / editing by pressing the key sequence CTRL+a > CTRL+ESC and you can therefore exit by pressing ESC once or twice).

We also activate a virtual bell (since we cannot hear the beep from the speaker of a remote server) using the vbell on and vbell_msg '!Bell!' commands that configure the virtual bell message in '!Bell!'. Despite this, I have found that the virtual hood often does not work.

Then, we establish some termcapinfo codes to expand the output buffer and adjust the window size. Additionally we removed several potentially annoying keybindings and added some useful keyboard settings.

Everything we will do to activate this new .screenrc setting is to open a new screen session. Now we will benefit from the new configuration established.

Ending

Defining a big flaw .screenrc the file helps when you use screen much. Turn off the splash screen, increasing the backward scrolling and improving the output buffer for speed are just a few of the things we did in our .screenrc template. Enjoy using the screen and the .screenrc template!!

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.