Showing posts with label GNU screen. Show all posts
Showing posts with label GNU screen. Show all posts

Friday, December 21, 2012

screen Quick Reference

— SkyHi @ Friday, December 21, 2012

Getting in

start a new screen session with session namescreen -S 
list running sessions/screensscreen -ls
attach to a running sessionscreen -r
… to session with namescreen -r 
the “ultimate attach”screen -dRR (Attaches to a screen session. If the session is attached elsewhere, detaches that other display. If no session exists, creates one. If multiple sessions exist, uses the first one.)

Escape key

All screen commands are prefixed by an escape key, by default C-a (that's Control-a, sometimes written ^a). To send a literal C-a to the programs in screen, use C-a a.

Getting out

detachC-a d
detach and logout (quick exit)C-a D D
exit screen“C-a : quit” or exit all of the programs in screen.
force-exit screenC-a C-\ (not recommended)

Help

See helpC-a ? (lists keybindings)
The man page is the complete reference, but it's very long.

Window Management

create new windowC-a c
change to last-visited active windowC-a C-a (commonly used to flip-flop between two windows)
change to window by numberC-a  (only for windows 0 to 9)
change to window by number or nameC-a '
change to next window in listC-a n or C-a
change to previous window in listC-a p or C-a
see window listC-a " (allows you to select a window to change to)
show window barC-a w (if you don't have window bar)
close current windowClose all applications in the current window (including shell)
kill current windowC-a k (not recommended)
kill all windowsC-a \ (not recommended)
rename current windowC-a A

Split screen

split display horizontallyC-a S
split display verticallyC-a | or C-a V (for the vanilla vertical screen patch)
jump to next display regionC-a tab
remove current regionC-a X
remove all regions but the current oneC-a Q

Scripting

send a command to a named sessionscreen -S  -X 
create a new window and run ping example.comscreen -S  -X screen ping example.com
stuff characters into the input buffer
using bash to expand a newline character
(from here)
screen -S <name> [-p <page>] -X stuff $'quit\r'
a full example
# run bash within screen
screen -AmdS bash_shell bash
# run top within that bash session
screen -S bash_shell -p 0 -X stuff $'top\r'
 
# ... some time later
 
# stuff 'q' to tell top to quit
screen -S bash_shell -X stuff 'q'
# stuff 'exit\n' to exit bash session
screen -S bash_shell -X stuff $'exit\r'

Misc

redraw windowC-a C-l
enter copy modeC-a [ or C-a  (also used for viewing scrollback buffer)
pasteC-a ]
monitor window for activityC-a M
monitor window for silenceC-a _
enter digraph (for producing non-ASCII characters)C-a C-v
lock (password protect) displayC-a x
enter screen commandC-a :
SHIFT+PGUP to scroll up in buffer

REFERENCES
http://aperiodic.net/screen/quick_reference
http://www.saltycrane.com/blog/2008/01/how-to-scroll-in-gnu-screen/

Thursday, December 6, 2012

GNU screen Kill detached screen session

— SkyHi @ Thursday, December 06, 2012

== ISSUE THIS COMMAND
[xxx@devxxx ~]$ screen -ls


== SCREEN RESPONDS
There are screens on:
        23487.pts-0.devxxx      (Detached)
        26727.pts-0.devxxx      (Attached)
2 Sockets in /tmp/uscreens/S-xxx.


== NOW KILL THE ONE YOU DONT WANT
[xxx@devxxx ~]$ screen -X -S 23487.pts-0.devxxx kill
or
[xxx@devxxx ~]$screen -S 23487 -X quit
== WANT PROOF? [xxx@devxxx ~]$ screen -ls There is a screen on: 26727.pts-0.devxxx (Attached) 1 Socket in /tmp/uscreens/S-xxx.

REFERENCES
http://stackoverflow.com/questions/1509677/kill-detached-screen-session

Tuesday, October 18, 2011

Use terminal scrollbar with tmux and scrollback

— SkyHi @ Tuesday, October 18, 2011

This is possible in both Screen and in tmux and the workaround is similar: to fool the multiplexers into thinking that the terminal has no "alternate screen" mode (such as that used by pico, mutt, etc). This is accomplished by setting termcap commands for the session. For GNU screen, put this in your .screenrc:
termcapinfo xterm*|xs|rxvt|terminal ti@:te@
and for tmux, add this to your .tmux.conf:
set -g terminal-overrides 'xterm*:smcup@:rmcup@'
The 'xterm*' part of the command should be set to whatever your terminal-emulator is declared as. Some form of xterm is a good guess, but you can check your on most sane *nix systems with:
termname
and this can usually be set in the preferences of your terminal program (ie: For Apple's Terminal.app, it's in Settings->Advanced->Emulation->"Declare terminal as"
The end result is that the overflow ends up in the terminal's scrollback buffer instead of disappearing. Of course, since this is one static buffer, things will get messy as you switch between screen or tmux windows, but this is handy for quickly flicking up to see the output of an ls command or the such.



Add the following to your ~/.screenrc:
termcapinfo xterm ti@:te@
termcapinfo xterm-color ti@:te@
This will let you use the Terminal.app scrollbar instead of relying on screen's scrollback buffer.


Scrollback for tmux
Ctrl-A + PageUp or PageDown

REFERENCES

Tuesday, May 24, 2011

How to Use GNU Screen

— SkyHi @ Tuesday, May 24, 2011
On UNIX, GNU Screen is a utility that i cannot live without. I know many console users share the same point of view with me.
What is GNU Screen? it is a terminal multiplexer and you can run multiple console-based applications simultaneously. The best part of it is that you can leave it running on remote machines and come back to pick up your console sessions. It’s like VNC or Remote Desktop but for UNIX console. As you know that on UNIX if you run something on a shell and you got disconnected from it then your sessions will also stop. All work will be gone.
With that feature GNU Screen is indispensable when it comes to do console sessions on low speed connection. If you got disconnected then all you have to do is reconnect and continue with your work. Or if you do something that takes a long time to finish, say maybe archiving and backing up many files, then simply launch it from GNU Screen session then detach from it and disconnect. Later on you can come back and reattach again to check the progress.
Anyway, here are some most useful features of GNU Screen that i use most of the time.
GNU Screen Most Used Commands
Commands What It Does
Ctrl-a ? (question mark)
GNU Screen's help pagehelp page
Open the help page. Also remember that “man” and “info” is your friend. By typing “man screen” and “info screen” you’ll get a lot of informations regarding Screen.
Ctrl-a d
resuming GNU Screen's sessionresuming session
Detach from the current Screen session and leave the process running in the background. Use “screen -r” to resume the session. If there are several Screen sessions running then add the session ID to the end of it. Ex: “screen -r 1088“. What really nice is that means you can login from anywhere to start your Screen session then later on continue the session again from different place.
Ctrl-a k
trying to kill a window in gnu screenkill a window
Kill current window. If you have another window active then Screen will move on to that window. If this is the only window to it then Screen will kill the window and exit, therefore ended the session.
Ctrl-a c Create a new window. By default it creates a new shell instance according your system setup (usually 90% of the time this is either bash or tcsh).
Ctrl-a A (capital A)
set title of current window in gnu screenset title
Rename current window. If you have so many windows running then i would suggest you name them so you won’t be lost.
Ctrl-a ” (double quote)
gnu screen showing list of active windows in current sessionlist of active windows
Show list of active windows in the current session.
Ctrl-a n, Ctrl-a p If you have multiple windows running then this command will do the flipping between the next window (using “Ctrl-a n”) and the previous window (using “Ctrl-a p”).
Ctrl-a h
gnu screen telling that it's logging to a file01. logging to a file
log file from a gnu screen window02. the log file
Create a running log (history). It will says “Screen image written to …” (see image 01). The file actually resides in your home directory (see image 02)
Ctrl-a ESC, Ctrl-a ]
entering gnu screen's copy modeA. entering copy mode
highlight the selection before copy it into gnu screen's bufferB. highlight the selection
gnu screen copied text into bufferC. copied into buffer
pasting text from gnu screen's bufferD. pasting text
Press “Ctrl-a ESC” will makes you temporary enter the copy buffer mode (see image A). You can tell when it says “Copy Mode …”.
In copy buffer mode you can move the cursor freely using directional keys. In this example i’m “cat”-ing the content of the Message of the Day (/etc/motd) in my Ubuntu machine. Next step is to highlight the block that you want to copy to the buffer. Move your cursor to the beginning of the block that you want to copy then press enter. That will mark the start point of the block. Then move your cursor toward the end of the block that you want to copy (see image B). In this example i’m copying the text “System Information …” from Ubuntu’s MOTD
Press enter again to copy the highlighted block into the buffer. This will end the “Copy Mode” and put you back to the active screen. You can tell when it says “Copied # characters into buffer” (see image C)
Use “Ctrl-a ]” to paste whatever is in the buffer (see image D). As you can see i’m pasting the text “System Information …” from the Screen’s copy buffer.

REFERENCES

 http://www.phacks.net/how-to-use-gnu-screen/

Thursday, March 3, 2011

Password protect GNU screen sessions

— SkyHi @ Thursday, March 03, 2011
Or, How do i get a crypted password for .screenrc ?
Here is a quick tip, hopefully it helps someone, I struggled with this for some time…
I have been using GNU screen program since a long time. This program is like a window manager for terminals and is especially useful for ISP and home shell accounts. You can have multiple shells running under the same session and can easily detach the sessions and reattach them from a different computer later etc.
I usually password protect my sessions by using the password command (ctrl A :password). But I had a hard time figuring out how to set that password in .screenrc file. The file obviously does not store it in plaintext and uses a crypted version of password. To get the crypted password, simply set your password in the screen session (ctrl A :password and then enter the password twice), then the crypted version gets automatically saved to screen’s copy buffer! All you need to do is have a line in your .screenrc which looks like this
password DTWxS2voQWkgI
After this all your new screen sessions will be automatically password protected (meaning you will need the password to reattach a session.)


REFERENCES
http://amit.chakradeo.net/2009/02/04/password-protect-gnu-screen-sessions/

Monday, June 14, 2010

Install screen on a CentOS system

— SkyHi @ Monday, June 14, 2010

screen is an application that can allow you to create multiple sessions with one terminal connection.

Install screen on a CentOS system.

yum install screen

Install on Debian or Ubuntu

sudo apt-get install screen

Start the application by using the command “screen”.

[Press Space or Return to end.]

Command Options

 Command key:  ^A   Literal ^A:  a
break ^B b license , removebuf =
clear C lockscreen ^X x reset Z
colon : log H screen ^C c
copy ^[ [ login L select '
detach ^D d meta a silence _
digraph ^V monitor M split S
displays * next ^@ ^N sp n suspend ^Z z
dumptermcap . number N time ^T t
fit F only Q title A
flow ^F f other ^A vbell ^G
focus ^I pow_break B version v
hardcopy h pow_detach D width W
help ? prev ^H ^P p ^? windows ^W w
history { } quit \ wrap ^R r
info i readbuf <>
kill K k redisplay ^L l xoff ^S s
lastmsg ^M m remove X xon ^Q q

^] paste .
" windowlist -b
- select -
0 select 0
1 select 1
2 select 2
3 select 3
4 select 4
5 select 5
6 select 6
7 select 7
8 select 8
9 select 9
I login on
O login off
] paste .
| split -v
:kB: focus up

Here is a list of common commands, all starting with Ctrl+A

Ctrl+A t
9:02:46 May 19 m67 1.02 0.97 0.70

Create a new window.
Ctrl+A c

List the window number, starts with “0”.
Ctrl+A N
This is window 1 (bash).

Move to previous screen.
Ctrl+A p

Quit using screen.
Ctrl+A \
Really quit and kill all your windows [y/n]

Clear screen of all text including prompt.
Ctrl+A C

List Available windows.
Ctrl+A “
Num Name Flags

0 bash $
1 bash $

Check version.
Ctrl+A v
screen 4.00.03jw4 (FAU) 2-May-06

Split Window
The split window with screen is a handy feature. It allows you to connect to a remote server and split the window so that you can run two different applications and watch both.

Once you have screen running use this command to create a vertical split.
Ctrl+A S

You can move between screens using this command.
Ctrl+A tab

When you move to the new window create a new screen with:
Ctrl+A c

Create vertical split window.
Ctrl+A |

You can move between screens using this command.
Ctrl+A tab

When you move to the new window create a new screen with:
Ctrl+A c

You can terminate one window with this command:
Ctrl+A k

Lock a window.
Ctrl+A x

Close a window.
Ctrl+A X

REFERENCES

http://beginlinux.com/blog/2010/05/screen/

Friday, June 11, 2010

Customize the BASH PS1 command prompt

— SkyHi @ Friday, June 11, 2010
The PS1 environment controls the appearance of the BASH command line prompt. There are a variety of default prompts but they usually include username, hostname, and working directory. You can easily customize your prompt to display information important to you as well as add color and style formatting.
Here we can see the default prompt is “username@hostname working_directory $”.
ryan@workstation ~ $
You can echo the PS1 environment variable and see the character codes which are used to create the output.
ryan@workstation ~ $ echo $PS1
\u@\h \w \$
Here is a list of all the special character codes you can use to build your PS1 command prompt.

Special Character Codes

\a – an ASCII bell character (07)
\d – the date in “Weekday Month Date” format (e.g., “Tue May 26″)
\D{format} – the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
\e – an ASCII escape character (033)
\h – the hostname up to the first `.’
\H – the hostname
\j – the number of jobs currently managed by the shell
\l – the basename of the shell’s terminal device name
\n – newline
\r – carriage return
\s – the name of the shell, the basename of $0 (the portion following the final slash)
\t – the current time in 24-hour HH:MM:SS format
\T – the current time in 12-hour HH:MM:SS format
\@ – the current time in 12-hour am/pm format
\A – the current time in 24-hour HH:MM format
\u – the username of the current user
\v – the version of bash (e.g., 2.00)
\V – the release of bash, version + patchelvel (e.g., 2.00.0)
\w – the current working directory
\W – the basename of the current working directory
\! – the history number of this command
\# – the command number of this command
\$ – if the effective UID is 0, a #, otherwise a $
\nnn – the character corresponding to the octal number nnn
\\ – a backslash
\[ - begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\] – end a sequence of non-printing characters
Lets try a simple change. Make sure you use double quotes so the escape characters get passed on to the variable and not expanded in the shell.
ryan@workstation ~ $ PS1="My Prompt: $ "
My Prompt: $
As you can see the prompt will immediately change to reflect the changes.
We can add the date to the prompt.
ryan@workstation ~ $ PS1="\d \u@\h \w \$ "
Sun Oct 19 ryan@workstation ~ $
Now lets add some color to our original prompt.
ryan@workstation ~ $ PS1="\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] "
Let go over this in detail.
A style block will display all text that follows with the style it defines. The block contains 3 elements.
Lets look at the first block.
“\[\e[1;32m\]”
“\[" - begin a sequence of non-printing characters
"\e[1;32m" - the semicolon separated list of style/color codes (in this case bold;green)
"\]” – end a sequence of non-printing characters
The first block will display the following username@hostname text in BOLD green.
“\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] ”
The second block will display the following working_directory text in BOLD blue.
“\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] ”
The final block resets the colors.
“\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] ”
Here is a rundown of all the color and style codes.

Color and Style Codes

Style
0 – default
1 – bold
4 – underline
7 – inverse
9 – strikeout
Foreground Colors
30 – foreground Black
31 – foreground Red
32 – foreground Green
33 – foreground Yellow
34 – foreground Blue
35 – foreground Magenta
36 – foreground Cyan
37 – foreground White
Background Colors
40 – background Black
41 – background Red
42 – background Green
43 – background Yellow
44 – background Blue
45 – background Magenta
46 – background Cyan
47 – background White
You can combine multiple codes to create the exact style you want.
“\[\e[1;4;36;47m\]”
This style block defines bold and underlined cyan text with a white background.
To make these changes permanent place your new prompt string like the one below in your ~/.bashrc file in your home directory.
PS1="\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$ \[\e[0m\]"
This file is sourced every time you start a new interactive shell.


REFERENCES
http://www.wiredrevolution.com/bash-programming/customize-the-bash-ps1-command-prompt


shell colors colorizing shell scripts

— SkyHi @ Friday, June 11, 2010
Shell scripts commonly used ANSI escape codes for color output.
Following table shows Numbers representing colors in Escape Sequences.
ColorForegroundBackground
Black 30 40
Red 31 41
Green 32 42
Yellow 33 43
Blue 34 44
Magenta 35 45
Cyan 36 46
White 37 47
The numbers in the above table work for xterm terminal.Result may vary for other terminal emulators. Use the following template for writing colored text. echo -e "\033[COLORm Sample text" The "\033[" begins the escape sequence.You can also use "\e[" instead of "\033[". COLOR specifies a foreground color, according to the table above.The "m" terminates escape sequence, and text begins immediately after that. Note: With an echo, the -e option enables the escape sequences.You can also use printf instead of echo. printf "\e[COLORm sample text\n" To print Green text echo -e "\033[32m Hello World" or printf "\e[32m Hello World" The problem with above statement is that the blue color that starts with the 32 color code is never switched back to the regular color, so any text you type after the prompt and even prompt also is still in the Green color. To return to the plain, normal mode, we have yet another sequence. echo -e "\033[0m" Now you won't see anything new on the screen, as this echo statement was not passed any string to display. But it has done its job, which was to restore the normal viewing mode. Whatever yor type now will be avoid of any fancy effects. Escape sequence also allow you to control the manner in which characters are displayed on the screen. The following table summarizes numbers representing text attributes in Escape Sequences.
ANSI CODEMeaning
0Normal Characters
1Bold Characters
4Underlined Characters
5Blinking Characters
7Reverse video Characters
Note: Blink attribute doesn't work in any terminal emulator, but it will work on the console. Combining all these Escape Sequences, you can get more fancy effect. Use the following template for writing colored text on a colored background. echo -e "\033[COLOR1;COLOR2m sample text\033[0m"; The semicolon separated numbers "COLOR1" and "COLOR2" specify a foreground and a background color.The order of the numbers does not matter, since the foreground and background numbers fall in non- overlapping ranges."m" terminates the escape sequence, and the text begins immediately after that.Although setting the colors separately also work (i.e. \033[44m\033[32m). There are some differences between colors when combining colors with bold text attribute. The following table summarises these differences.
Bold offcolorBold oncolor
0;30Balck1;30Dark Gray
0;31Red1;31Dark Red
0;32Green1;32Dark Green
0;33Brown1;33Yellow
0;34Blue1;34Dark Blue
0;35Magenta1;35Dark Magenta
0;36Cyan1;30Dark Cyan
0;37Light Gray1;30White
The following shell script prints all the colors and codes on the screen. #!/bin/bash # This script echoes colors and codes echo -e "\n\033[4;31mLight Colors\033[0m \t\t\t \033[1;4;31mDark Colors\033[0m" echo -e " \e[0;30;47m Black \e[0m 0;30m \t\t \e[1;30;40m Dark Gray \e[0m 1;30m" echo -e " \e[0;31;47m Red \e[0m 0;31m \t\t \e[1;31;40m Dark Red \e[0m 1;31m" echo -e " \e[0;32;47m Green \e[0m 0;32m \t\t \e[1;32;40m Dark Green \e[0m 1;32m" echo -e " \e[0;33;47m Brown \e[0m 0;33m \t\t \e[1;33;40m Yellow \e[0m 1;33m" echo -e " \e[0;34;47m Blue \e[0m 0;34m \t\t \e[1;34;40m Dark Blue \e[0m 1;34m" echo -e " \e[0;35;47m Magenta \e[0m 0;35m \t\t \e[1;35;40m Dark Magenta\e[0m 1;35m" echo -e " \e[0;36;47m Cyan \e[0m 0;36m \t\t \e[1;36;40m Dark Cyan \e[0m 1;36m" echo -e " \e[0;37;47m Light Gray\e[0m 0;37m \t\t \e[1;37;40m White \e[0m 1;37m" OUTPUT: Some examples: Block background and white text echo -e "\033[40;37m Hello World\033[0m" Reverse video text attribute option interchanges fg and bg colors. Bellow statement prints block on white echo -e "\033[40;37;7m Hello World\033[0m" echo -e "\033[33;44m Yellow text on blue background\033[0m" echo -e "\033[1;33;44m Bold yellow text on blue background\033[0m" echo -e "\033[1;4;33;44mBold yellow underlined text on blue background\033[0m" The "tput" command: Other than echo there is a command called tput using which we can control the way the output is displayed on the screen.But it is less flexible than ANSI escape sequences.
 
 
REFERENCES
http://bashscript.blogspot.com/2010/01/shell-colors-colorizing-shell-scripts.html

Modify Your Command Prompt

— SkyHi @ Friday, June 11, 2010

You may find that you want to modify the prompt. This can help you create visibility for special features or just modify it to something more useful. You can view the default settings for the prompt by using this command:


echo $PS1


As stated above it will show user, hostname, location and definition whether it is a normal user or root.


Create a single character


PS1=”$ ”


The space behind the $ is enforced by placing the quotes so it does not run into your text. The $ is typically used to show that it is a normal user not the root user indicated by the “#”.


Change options for the prompt


\d : the date Weekday Month Date format

\h : the hostname up to the first ‘.’

\A : the current time in 24-hour HH:MM format

\u : the username of the current user

\w : the current working directory, with $HOME abbreviated with a tilde

\$ : if the effective UID is 0, a #, otherwise a $




Create a colored prompt


You may want to create a color prompt that you can use for visibility. In this example the hostname has been dropped to make a shorter prompt and the prompt is turned red but the commands that you enter will be black. The export command will change these features.


mike@ub:~$ export PS1=’\e[0;31m[\u:\w]\$ \e[m '

[mike:~]$


This will color the prompt but not any commands that you enter.


List of Color codes

Color Code

Black 0;30

Blue 0;34

Green 0;32

Cyan 0;36

Red 0;31

Purple 0;35

Brown 0;33

Blue 0;34

Green 0;32

Cyan 0;36

Red 0;31

Purple 0;35

Brown 0;33


Replace digit 0 with 1 for a lighter color.


Make Changes permanent

All of the changes you make will be lost when you close the terminal or log out. Here are directions to make them permanent.


Ubuntu

# uncomment for a colored prompt, if the terminal has the capability; turned

# off by default to not distract the user: the focus in a terminal window

# should be on the output of commands, not on the prompt

#force_colored_prompt=yes


The .bashrc file in each user’s home directory allows you to change the default for the prompt to a color prompt by uncommenting the line:


#force_colored_prompt=yes


Unfortunately a typo in the line must also be corrected so that it should read:

force_color_prompt=yes


Ubuntu or CentOS

Place your custom prompt in the user .bashrc file with this command:


export PS1=’\e[0;31m[\u:\w]\$ \e[m ‘


REFERENCES

http://beginlinux.wordpress.com/2008/09/12/modify-your-command-prompt/

Saturday, May 22, 2010

Remove dead screens" message when session exists

— SkyHi @ Saturday, May 22, 2010
Hi Frederik,

hmmm, are you sure your screen sessions are alive?

here, when type:

$ screen -list

And have more than one screen session like this:

12:39 [EMAIL PROTECTED] ~> screen -list
There are screens on:
5815.pts-1.cronos (Attached)
6150.pts-7.cronos (Detached)
2 Sockets in /home/leslie/.screen.

and type:

[EMAIL PROTECTED] ~> screen -D -R
There are several suitable screens on:
5815.pts-1.cronos (Attached)
6150.pts-7.cronos (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
[EMAIL PROTECTED] ~>

or

[EMAIL PROTECTED] ~> screen -x
There are several suitable screens on:
5815.pts-1.cronos (Attached)
6150.pts-7.cronos (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.


I only receive the message "Remove dead screens with -wipe",

when I have dead sessions that cant be recovered.


I hope this clarify some aspects ;-)


Cheers

LEslie


REFERENCES
http://www.mail-archive.com/screen-users@gnu.org/msg00094.html

Wednesday, December 16, 2009

Screen: A "must" tool for Unix Administrators

— SkyHi @ Wednesday, December 16, 2009
GNU Screen is a simple but at the same time flexible and programmable windows manager. It is often called terminal multiplexer, but the key functionality of screen is in the area of windows management: creation, destruction and resizing of windows. For example it can split the console screen horizontally and display two windows at the same time. Screen is a Unix program (Cygwin version exists and is quite usable). For good Windows terminal emulator see Teraterm

In addition to the ability to run many shell processes in a single terminal, screen provides classic windows management capabilities -- the ability to split terminal into regions (horizontal stripes of varying length, each containing its own session. What is also important is that like VNC screen provides the ability to detach the terminal emulator from the running programs and then reattach the same or new terminal to them.

This feature is enabled by default.

An important feature of screen is that like VNC it provides the ability to detach the terminal emulator from the running programs and then reattach the same or new terminal to them.

This means screen will keep shells and programs running in all windows that you opened even if you accidentally disconnected from the remote server or closed the terminal emulator. Like VNC screen also permits to share a terminal (in read-only mode) with someone else in real time. This permits to collaborate on a project by several remote users located in different places. It also frees administrator from waiting until a particular task ends. He/she can be connected to any number of servers, start a task, detatch from the task, and the task keeps running. Then reattach later, check the status and intervene of needed. That means that you can start a job at the office and check for it completion at home.

Screen has a certain architectural affinity to the orthodox file managers (OFM ), especially deco as well as orthodox editors like vi and THE (the power of command line).

Like in vi in screen you have to hit certain prefix to get to command line. The default prefix is Ctrl-A. For example Ctrl-A,C will open window.

This availability of command line operations and related scriptability via screenrc file makes screen attractive to system administrators who value blend of power and simplicity provided by "orthodox" tools. Like in all orthodox utilities there is an extensive set of commands (mini-language) that can used to modify the behavior of screen.

Ctrl-a is called attention/command key and is similar to Esc in vi (you also can change Ctrl-A to another sequence, if you wish). This sequence presuppose that you are using Sun keyboard (with Ctrl instead of Caps lop) or switched Ctrl and CapsLock keys (in Windows you can use Install RandyRants SharpKeys -- registry hack that permits remapping CapsLock to Alt. ).

If the colon (":") is entered after it, then screen switches to command mode and you'll see a highlighted colon prompt at the bottom of the screen. After that, like in vi, you can enter commands that change the behavior of the screen and modify its current configuration. For example you can open and close windows, change size of windows, etc. For example you open new window also by typing command

Ctrl-A: screen

which is equivalent to the shortcut Ctrl-A,C that we discussed above.

So shortcut Ctrl-A,C and command Ctrl-A: screen are synonyms. This is just an association that is defined in .screenrc file - screen's dotfile. File .screenrc is a list of commands which are executed at startup. While by default screen is using the name .screenrc you can start screen using arbitrary screen's dotfile using option -c, for example

screen -c ~myhome/.screenrc_`uname`

This example demonstrated that you can have multiple .screenrc files and use the one that suits current environment (for example Linux, AIX, Solaris, etc) and the task in hand.

You can also execute predefined sequences of screen commands stored in the files (screen scripts) using the command source. One common application is the execution of the set of command creating a particular combination of windows and loading particular applications into each window. For example, you can load Perl in debugging mode on one window, vi in another and Perl POD browser in the third creating poor man IDE environment. For example

source .screenrc_perl

or

source ~myhome/perl.screenrc

That provides you with the flexibility to have several predefined sets of windows working on a particular task, the capability that improves your productivity dramatically as you can polish the arrangement and gradually connect a set of useful shortcuts as you gain experience with the environment (in the example above, working with Perl). System administrators can (and should) have a custom part of configuration encoded for particular hosts (that's the way I use screen at work) :

source ${HOME}/${HOST}.screenrc

Due to availability of command line interface and scriptability Screen can be considered to be a member of "orthodox" family of utilities which include orthodox file managers (mc, FAR, etc) and orthodox editors (vim, Xedit, THE, etc). Command line can be activated with Ctrl-A: after that you can enter any command from the screen command set to modify behavior of the screen or execute sets of commands (screen script) from particular file using command source.

A typical and probably the simplest application of this capability is to launch a predefined set of windows at screen startup., for example:

source $HOME/.screenrc # read in your normal screenrc
screen top # now start opening windows
screen -t irc epic # with the -t option it's possible to set the window title
screen -t mail 8 mutt # you can also specify the window number to launch in
screen -t messages 9 tail -f /var/log/messages

In X-windows world screen inspired several minimalist windows managers which usually are bungled with linux mini-distributions. Classic example of this genre is ratpoison (the name suggest that it is one of the few that not support mouse "out of principle" ;-).

Screen is portable and is an almost a standard utility on all flavors of Unix. It is often preinstalled by default in Linux distributions (for example, Suse 10). And it is easily installed in Solaris, AIX and HP-UX. For example Solaris 10 package can be found on sunfreeware.com:

screen-4.0.2-sol10-intel-local.gz Screen provides an ANSI/vt100 terminal emulator, which can multiplex up to 10 pseudo-terminals - installs in /usr/local. There are further configuration steps in the /usr/local/doc/screen/INSTALL. The etc and terminfo directories containing other relevant files are in /usr/local/doc/screen. The screen package may also require that you install the ncurses package.

Using it, you can run any number of console-based applications -- interactive command shells, curses-based applications, text editors, etc. -- within a single terminal. Many administrators who use screen open a dozen or even more sessions.

Screen can be used in two ways (you can use both local and remote screen instances simultaneously by changing control character in local to say Ctrl-\ via .screenrc, see below):

1. On local workstation (in this case you multiplex between terminal sessions opened in multiple screen windows). Many administrators use it as a terminal on steroids and have a window for each remote server they administer.
2. Remotely (in this case you need first to connect to remote machine using, say ssh or telnet, run screen and then multiplex windows on this remote machine). In this case screen provide the ability to have serial local terminal sessions via single ssh or telnet connection (for example one root window and one non-root window).

A nested master-slave invocation of screen is also possible, but it is unclear how practical it is. See Jason White page and my screenrc and my slave screenrc by Tim Chambers. Here is how he explained this method in Slashdot post:

You run an "outer" screen session (the "slave" session) that in turn runs an "inner" (or "master") session. You use the regular escape sequence (Ctrl-A d) to detach from the master, and you map Ctrl-^ to be the control key for the slave session. If you press Ctrl-^ while using screen this way, you'll see one process in the slave session. It's running ssh-agent. That's the key to using ssh with screen. The slave's only purpose is to run ssh-agent. The master runs as a child of that. Consequently, all shells in the master session are running under the ssh-agent. Just run ssh-add from any master shell, and then all shells have your ssh identity.

As we mentioned above, after starting screen you need to open windows using the command Ctrl-a c. Every window in screen is identified by a unique number. The first window is usually numbered 0 (you can change this to one if you do not like counting from zero). For each created windows screen starts default shell. After that user for example can open remote session to a particular server and rename it to simplify recognition after switching to another (an optimal typical number of windows for one screen instance is around a dozen). You can use multiple screen instances each with its own set of servers to connect (DMZ servers, database servers, etc).

Minimum set of commands necessary to use screen productively includes just five commands:

1. Ctrl-a c to create a new window (Ctrl-a N -- lists the number of the active windows)
2. Ctrl-a to switch between them (Ctrl-a n, Ctrl-a p or Ctrl-a Ctrl-a will work too providing previous and next windows, but are less convenient and there are two of them instead of one)
3. Ctrl-a " (windows) Show a list of active windows. You can navigate this list with the arrow keys (or vi-style, with j and k), and pick a window to activate by pressing Enter when it's highlighted. As this is one of the most often used commands you will be better off redefining this command as Ctrl-a space (see .screenrc examples on how to do that).
4. Ctrl-a d. detach from current session. You can also detach just by closing the terminal emulator that contains the session. Neither of these actually end your session. All they do is unbind your session from the current terminal. All of the programs you started running within screen are still running. To reattach type screen -r from the command prompt
5. Ctrl-a Ctrl-\ (quit) Kill all windows and terminate screen. You can close individual window by exiting shell with command exit.

There are also several more advanced features that can improve your productivity. Among them I would like first of all mention regions.

Regions can dramatically improves your productivity in pure command line environment. This feature turns screen in rudimentary windows manager able to create "strips" non-overlapping horizontal zones each of which corresponds to a separate shell window. To split the screen into two regions just press C-a S. The current window will split into two separate areas with the bottom area blank. To switch to the bottom part press C-a [Tab]. If you need to create a new shell in the new window, so press C-a c. If you want to see one of the existing sessions press Ctrl-a " and select the necessary session. You can create more regions by pressing C-a S again: each time the current "strip" will be split into two. Tabs display the active region shell name in its title bar. You can also switch between tabs being in a region, so e.g.: you have 4 tabs and 2 regions. Each region can display one of these tabs. A nice trick is that upper region can react on the change of the particular environment variable and display either file you are editing or the listing of the current directory. This arrangement is close for the OFM paradigm. You can change the size of each region by entering resize [size], where size is the number of lines.
The key additional feature of Screen that you need to master after mastring basic commands is split command.

Here is slightly more elaborate set of commands, which we will call Level 1 command set. It includes regions-related commands:

1. Ctrl-a A (title) Allow the user to enter a name for the current window (useful fo naming each window according to the purpose (for example each can represent a different directory).
2. Ctrl-a Ctrl-a (other) will switch you to whichever window you were using before the current one.
3. Ctrl-a S (split, capital s, please!). Screen can horizontally by split into regions(panels), each holding a different terminal. After you split the window you need to move to the new region using Ctrl-tab and create a session in it using Ctrl-a-c.
* To enter that newly created region we have to tab into it: Ctrl-a
(change focus switch from one panel to another in OFMs)
* To close a region type Ctrl-a X.
* A region with focus can be resized with Ctrl-a + or Ctrl-a - By default this changes the height by 3 lines.
* Typing Ctrl-a : puts Screen into command mode and the status line at the bottom changes. Screen now accepts direct commands and resize 24 makes the currently focused region exactly 24 lines high. Alternatively, you can specify the height to an absolute number by going into Screens command mode. Ctrl-a X removes the current region.

Note: It is easy to confuse with the lowercase Ctrl-a s which actually sends a command to freeze to the parent terminal. To reverse this effect one doesn't have to go as far as closing the terminal and re-attach again, but simply type Ctrl-a q.

4. Ctrl-a (focus) move the the next split window
5. Ctrl-a X (remove) Close current region
6. Ctrl-a N, where N is a number from 0 to 9, will switch to the corresponding window.

7. Ctrl-a w lists windows in one like with the active marked by asterism. This is a noninteractive list and you cannot navigate it. Interactive list is by default assigned to Ctrl-a "

8. Ctrl-a d. detach from current session. You can also detach just by closing the terminal emulator that contains the session. Neither of these actually end your session. All they do is unbind your session from the current terminal. All of the programs you started running within screen are still running. To reattach type screen -r

9. Screen remembers a configurable number of scrollback lines. You can access the scrollback buffer by entering "copy mode", which is accomplished by typing Ctrl-a [. You can mark text anywhere in the scrollback buffer and paste it with Ctrl-a ]. Screen is also capable of logging to files.

10. Screen has features that allow you to monitor a window for silence, useful, for example, for knowing when a compile has finished. To start or stop monitoring the current window for 30 seconds of silence, type Ctrl-a _; to start or stop monitoring a window for activity, type Ctrl-a M.

11. To close sessions you need to to close each of your screen windows. Exit whatever programs or shells each is running, and they will go away. When the last program running inside screen is exited, screen itself will go away. You can forcefully quit screen with Ctrl-a Ctrl-\.

As you progress you "screen vocabulary" extends and in some point you can switch from Level 1 command set to Level 2 command set which includes several additional useful commands (and first of all bindkey command):

Ctrl-a ' (select)
Prompt for a window name or number to switch to.
Ctrl-a " (windowlist -b)
Present a list of all windows for selection.
Ctrl-a 0 (select 0)
... ...
Ctrl-a 9 (select 9)
Ctrl-a - (select -)
Switch to window number 0 - 9, or to the blank window.
Ctrl-a tab (focus)
Switch the input focus to the next region.
Ctrl-a Ctrl-a (other)
Toggle to the window displayed previously.
Note that this binding defaults to the command character typed twice, unless overridden. For instance, if you use the option "-e]x", this command becomes "]]".
Ctrl-a a (meta)
Send the command character (Ctrl-a) to window. See escape command.
Ctrl-a A (title)
Allow the user to enter a name for the current window (useful fo naming each window according to the purpose (for example each can represent a different directory).
Ctrl-a b
Ctrl-a Ctrl-b (break)
Send a break to window.
Ctrl-a B (pow_break)
Reopen the terminal line and send a break.
Ctrl-a c
Ctrl-a Ctrl-c (screen)
Create a new window with a shell and switch to that window.
Ctrl-a C (clear)
Clear the screen.
Ctrl-a d
Ctrl-a Ctrl-d (detach)
Detach screen from this terminal.
Ctrl-a D D (pow_detach)
Detach and logout.
Ctrl-a f
Ctrl-a Ctrl-f (flow)
Toggle flow on, off or auto.
Ctrl-a F (fit)
Resize the window to the current region size.
Ctrl-a Ctrl-g (vbell)
Toggles screen's visual bell mode.
Ctrl-a h (hardcopy)
Write a hardcopy of the current window to the file "hardcopy.n".
Ctrl-a H (log)
Begins/ends logging of the current window to the file "screenlog.n".
Ctrl-a i
Ctrl-a Ctrl-i (info)
Show info about this window.
Ctrl-a k
Ctrl-a Ctrl-k (kill)
Destroy current window.
Ctrl-a l
Ctrl-a Ctrl-l (redisplay)
Fully refresh current window.
Ctrl-a L (login)
Toggle this windows login slot. Available only if screen is configured to update the utmp database.
Ctrl-a m
Ctrl-a Ctrl-m (lastmsg)
Repeat the last message displayed in the message line.
Ctrl-a M (monitor)
Toggles monitoring of the current window.
Ctrl-a space
Ctrl-a n
Ctrl-a Ctrl-n (next)
Switch to the next window.
Ctrl-a N (number)
Show the number (and title) of the current window.
Ctrl-a backspace
Ctrl-a h
Ctrl-a p
Ctrl-a Ctrl-p (prev)
Switch to the previous window (opposite of Ctrl-a n).
Ctrl-a q
Ctrl-a Ctrl-q (xon)
Send a control-q to the current window.
Ctrl-a Q (only)
Delete all regions but the current one.
Ctrl-a r
Ctrl-a Ctrl-r (wrap)
Toggle the current window's line-wrap setting (turn the current window's automatic margins on and off).
Ctrl-a s
Ctrl-a Ctrl-s (xoff)
Send a control-s to the current window.
Ctrl-a S (split)
Split the current region into two new ones.
Ctrl-a t
Ctrl-a Ctrl-t (time)
Show system information.
Ctrl-a v (version)
Display the version and compilation date.
Ctrl-a Ctrl-v (digraph)
Enter digraph.
Ctrl-a w
Ctrl-a Ctrl-w (windows)
Show a list of window.
Ctrl-a W (width)
Toggle 80/132 columns.
Ctrl-a x
Ctrl-a Ctrl-x (lockscreen)
Lock this terminal.
Ctrl-a X (remove)
Kill the current region.
Ctrl-a z
Ctrl-a Ctrl-z (suspend)
Suspend screen. Your system must support BSD-style job-control.
Ctrl-a Z (reset)
Reset the virtual terminal to its "power-on" values.
Ctrl-a . (dumptermcap)
Write out a ".termcap" file.
Ctrl-a ? (help)
Show key bindings.
Ctrl-a Ctrl-\ (quit)
Kill all windows and terminate
screen.
Ctrl-a : (colon)
Enter command line mode.
Ctrl-a [
Ctrl-a Ctrl-[
Ctrl-a esc (copy)
Enter copy/scrollback mode.
Ctrl-a ] (paste .)
Write the contents of the paste buffer to the stdin queue of the current window.
Ctrl-a {
Ctrl-a } (history)
Copy and paste a previous (command) line.
Ctrl-a > (writebuf)
Write paste buffer to a file.
Ctrl-a < (readbuf)
Reads the screen-exchange file into the paste buffer.
Ctrl-a = (removebuf)
Removes the file used by Ctrl-a < and Ctrl-a >.
Ctrl-a , (license)
Shows where screen comes from, where it went to and why you can use it.
Ctrl-a _ (silence)
Start/stop monitoring the current window for inactivity.
Ctrl-a * (displays)
Show a listing of all currently attached displays.

Here are some examples of keyboard bindings:

bindkey -d # Show all of the default key bindings. The application mode entries are marked
with [A].

bindkey -k k1 select 1 # Makes the "F1" key switch to window one.

bindkey -k F1 command # Makes the F11 (not F1!) key an alternative screen escape (besides `Ctrl-a').

You can also easily change Ctrl-A (the default attention/command key sequence) to another sequence. This can be done either via putting command escape into .screenrc or via a switch when you start screen.

1. -e xy specifies the command character to be x and the character generating a literal command character to y (when typed after the command character). The default is «Ctrl-a» and `a', which can be specified as "-e ^Aa". When creating a screen session, this option sets the default command character. In a multi-user session all users added will start off with this command character. But when attaching to an already running session, this option changes only the command character of the attaching user. This option is equivalent to either the commands «defescape» or «escape» respectively.

If you invoked screen by using "screen -e ^Ww" you can see that ^W became attention/command key combination. So ^W-? gets the Help screen. Ctrl-A and then ? no longer worked.

2. Adding co the .screenrc file using excape command is explained in the man page:

escape xy

Set the command character to x and the character generating a literal command character (by triggering the "meta" command) to y (similar to the -e option). Each argument is either a single character, a two-character sequence of the form "^x" (meaning "Ctrl-x"), a backslash followed by an octal number (specifying the ASCII code of the character), or a backslash followed by a second character, such as "\^" or "\\". The default is "^Aa".

For example if you add

escape ^\\

into the .screenrc you will see that ^\ became attention key sequence instead of default ^A.

It is also important to make screen more user friendly. In order better see what open windows you have you can do it by adding the following lines to .screenrc :

1 hardstatus alwayslastline
2 hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=
kw}%?%-Lw%?%{r}(%{W}%n*%f %t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}
%Y-%m-%d %{W}%c %{g}]'

In his paper Screen Tips & Tricks Łukasz Olender suggests several other variants:

But so as not to get lost in these windows, I suggest you create tabs similar to those found in web browsers. You can do this by writing to your configuration file ~/.screenrc one of the following codes:

hardstatus alwayslastline
hardstatus string '%{= mK}%-Lw%{= KW}%50>%n%f* %t%{= mK}
%+Lw%< %{= kG}%-=%D %d %M %Y %c:%s%{-}'

or

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}]'

or

hardstatus alwayslastline
hardstatus string '%{gk}[ %{G}%H %{g}][%= %{wk}%?%-Lw%?%{=b kR}
(%{W}%n*%f %t%?(%u)%?%{=b kR})%{= kw}%?%+Lw%?%?%= %{g}][%{Y}%l%{g}]
%{=b C}[ %m/%d %c ]%{W}'

With the first code each tab will have a number, a name plus time and date. The second one will give you tabs that will be created in the center and the active tab will be highlighted in red and also have the hostname displayed. The last code will additionally display the processor usage. You can create your own codes but it is a rather specialized task - I refer you to the system manuals for more information.

You can combine ideas from those into you own status line that suits your need. But in any case it is important to improve visibility of windows by using this capability of screen.

Dr. Nikolai Bezroukov

Reference: http://www.softpanorama.org/Utilities/screen.shtml