screen is to me one of the most useful features on Linux. It allows you to create virtual tabs into one and the same terminal. Instead of having 10 terminals open in the end and not finding anymore what you were looking for, screen nicely organises this for you.
There's even a possibility to "preload" a screen session with a layout of your choice. You simply add the layout description in a file called ~/.screenrc and each time screen is started it will load that layout.
Another very big advantage of screen is that the screen server runs on the host (e.g. Raspberry Pi) and if you run an SSH session on the PC and launch some activities in the screen application, the application continues to run on the host if you close down the PC since you're only breaking the client connection but the host continues to run.
After you restart your PC you can attach to the running screen session on the host and continue with what you were busy as you have never been away.
Below is a screen layout template that will create 3 virtual tabs in a terminal and will also add a status bar with the hostname on the left, the 3 virtual tabs in the middle and the date and time on the right like shown below this section.
The script to achieve this is given below:
#!/bin/bash
setenv TERM xterm
# The * next to xterm makes it possible to just scroll up and downwards with the mouse, no need to do
# the cumbersome Ctrl-A Esc button combination.
termcapinfo xterm* ti@:te@
# termcapinfo xterm ks@:ke@
defmonitor on
activity "%"
defutf8 on
nonblock on
# how a statusline with: terminals, date and time
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m %{W}%c %{g}]'
# Avoid the "splash" screen.
startup_message off
# Avoid screen flicker during auto-completion
vbell off
# shell
# shell -bash
# Change the default scrollback lines from 100 to 100000
defscrollback 100000
# Bind some keys
bind ',' prev
bind '.' next
# bind key F11 (!!!NOT!!! F1) to the "prev" command
bindkey -k F1 prev
# bind key F12 (!!!NOT!!! F2) to the "next" command
bindkey -k F2 next
# Default screens
screen -t "HomeControl" 0 /bin/sh -c "cd ~/;/bin/bash"
screen -t "openHAB" 1 /bin/sh -c "cd ~/;/bin/bash"
screen -t "General" 2 /bin/sh -c "cd ~/;/bin/bash"
# Start default with session 4
select 0
The comments in the code explain themselves, no need to give any further explanation.
Ctrl-a | : Splits the screen vertically in two parts. The new part does NOT contain a workable screen yet. Therefore, you have to create a new screen inside the new split screen using Ctrl-a c
To switch between the two parts, use Ctrl-a TAB.
To create another split within the split: Ctrl-a c
Ctrl-a S : Splits the screen horizontally in two parts. The new part does NOT contain a workable screen yet. Therefore, you have to create a new screen inside the new split screen using Ctrl-a c
To close a split screen, make sure the part you want to close is inactive. Then, issue the command Ctrl-A Q to get rid of the split screen part.
If you have a running screen session and you want to temporarily detach it so that you can see the original command window, you can use the key combination Ctrl-A + d (all together).
This will detach your screen session.
To re-attach to the screen session run the command screen -r on the command line. You will be brought back to were you left off before you detached the session.
If you modified the content of the ~/.screenrc file there's no need to quit screenrc and restart again. You can actualise the content of the modified ~/.screenrc file like so:
Ctrl-A :
source ~/.screenrc
To see if more than one screen session is active, run the command screen -ls
The result could be something like this:
ubuntu@ubuntu:~$ screen -ls
There are screens on:
10784.pts-4.ubuntu (08/07/21 08:38:43) (Detached)
7964.pts-0.ubuntu (08/07/21 08:11:09) (Detached)
2 Sockets in /run/screen/S-ubuntu.
To get rid of one of the sessions, run the command screen -X -S <session_name> quit
Example: screen -X -S 10784.pts-4.ubuntu quit
In fact, for this case typing the number 10784 is enough, no need to type in the whole part of the session.
To kill all screens, just kill screen. To do this, run the command killall screen.