screen is an excellent tool that allows you to have multiple shell sessions on a single physical terminal.

It’s very helpful when working with remote servers, if you start a screen session on the remote server, whatever you were doing won’t be lost if your connection to the server fails, just connect again to the server, resume the previous screen session and you can continue with your tasks.

Here are some useful screen commands:


DETACH SCREEN

To start screen just type screen and then you’ll be in your new terminal. When ready to detach, hit CTRL+a and then press d.


REATTACH SCREEN

If only one screen is open:

$ screen -R

If more than one screen is open:

1. Get the terminal_id

$ screen -list
There are screens on:
	29143.pts-0.server1	(06/11/2016 10:13:07 AM)	(Detached)
	29122.pts-0.server1	(06/12/2016 11:11:34 AM)	(Detached)
2 Sockets in /var/run/screen/S-root.

2. Reattach the screen

$ screen -R 29122.pts-0.server1

RECOVER FROM A DISCONNECTED SESSION

If the connection lost when in a screen session, then when you try to connect to that screen session you get the message: There is no screen to be resumed.

To recover the session:

$ screen -d -r 29721.pts-6.bugs

where 29721.pts-6.bugs is the terminal id returned by the command: ‘screen -list’


WINDOW MANAGEMENT

Create a new window: CTRL+a c
Kill the current window: CTRL+a k
Kill all windows: CTRL+a \
Switch to the next window: CTRL+a n
Switch to the previous window: CTRL+a p
List available windows: CTRL+a “
Start/stop logging the current window to screenlog.X: CTRL+a H

REGION MANAGEMENT

Split the current region: CTRL+a S
Kill the current region: CTRL+a X
Switch the focus to the next region: CTRL+a TAB
Delete all regions but current one: CTRL+a Q

SCROLLBACK BUFFER

To configure, add the following in $HOME/.screenrc:

defscrollback N

where N is the number of lines to keep

To enter scrollback mode: CTRL+a [
To exit from scrollback mode: Esc
To move around use VI navigation keys, like h, j, k, G, etc
To search text in scrollback buffer: / (search forward) or ? (search backward)