[http://www.gnu.org/s/screen/ GNU Screen] is a wrapper that allows separation between the text program and the shell from which it was launched. This allows the user to, for example, start a text program in a terminal in X, kill X, and continue to interact with the program. Here are a couple of tips and tricks you may be interested in.

[http://www.gnu.org/s/screen/ GNU Screen] is a wrapper that allows separation between the text program and the shell from which it was launched. This allows the user to, for example, start a text program in a terminal in X, kill X, and continue to interact with the program. Here are a couple of tips and tricks you may be interested in.

−

−

If you are looking for a tutorial, the gentoo wiki contains a nice one: http://en.gentoo-wiki.com/wiki/Screen

== Installation ==

== Installation ==

−

GNU Screen can be found in the extra repository:

+

GNU Screen can be [[pacman|installed]] using the {{pkg|screen}} package found in the [[Official Repositories|official repositories]].

−

# pacman -S screen

+

== Usage ==

+

Commands are entered pressing {{keypress|ctrl+a}} and then the key binding.

* {{keypress|ctrl+a}} {{keypress|Q}} Close all regions but the current one

+

* {{keypress|ctrl+a}} {{keypress|X}} Close the current region

+

* {{keypress|ctrl+a}} {{keypress|d}} Detach from the current screen session, and leave it running. Use {{ic|screen -r}} to resume

−

== Basics ==

+

== Tips and tricks ==

−

Commands are entered pressing Control A and then the key binding. The escape key can be changed with the ''escape'' option in ~/.screenrc. IE:

+

=== Change the escape key ===

−

<pre>

+

The escape key can be changed with the {{ic|escape}} option in {{ic|~/.screenrc}}. For example:

−

escape ``

+

{{bc|escape ``}}

−

</pre>

−

sets the escape key to `

−

=== Common Commands ===

+

sets the escape key to {{Keypress|`}}.

−

C-a ?

−

:Displays commands and it's defaults (VERY important)

−

C-a "

−

:Window list

−

C-a 0

−

:opens window 0

−

C-a A

−

:Rename the current window

−

C-a c

−

:Create a new window (with shell)

−

C-a S

−

:Split current region into two regions

−

C-a <TAB>

−

:Switch the input focus to the next region

−

C-a C-a

−

:Toggle between current and previous region

−

C-a <ESC>

−

:Enter Copy Mode (use enter to select a range of text)

−

C-a ]

−

:Paste text

−

C-a Q

−

:Close all regions but the current one

−

C-a d

−

:Detach from the current screen session, and leave it running. Use screen -r to resume

−

== Start at window 1 ==

+

=== Start at window 1 ===

−

By default, the first screen window is 0. If you'd rather never have a window 0 and start instead with 1, put something like the following in your ~/.screenrc:

+

By default, the first screen window is 0. If you'd rather never have a window 0 and start instead with 1, add the following lines on your configuration:

−

<pre>

+

{{hc|$HOME/.screenrc|

bind c screen 1

bind c screen 1

bind ^c screen 1

bind ^c screen 1

bind 0 select 10

bind 0 select 10

screen 1

screen 1

−

</pre>

+

}}

−

+

=== Nested Screen Sessions ===

−

== Nested Screen Sessions ==

+

It is possible to get stuck in a nested screen session. A common scenario: you start an ssh session from within a screen session. Within the ssh session, you start screen. By default, the outer screen session that was launched first responds to {{keypress|ctrl+a}} commands. To send a command to the inner screen session, use {{keypress|ctrl+a}} {{keypress|a}}, followed by your command. For example:

−

It's possible to get stuck in a nested screen session. A common scenario: you start an ssh session from within a screen session. Within the ssh session, you start screen. By default, the outer screen session that was launched first responds to C-a commands. To send a command to the inner screen session, use C-a a, followed by your command. For example:

Add some entries to /etc/inittab to indicate what should happen on runlevel 4, substituting your user name for <user>:

+

Add some entries to {{ic|/etc/inittab}} to indicate what should happen on runlevel 4, substituting your user name for <user>:

−

<pre>

+

{{bc|# GNU Screen on runlevel 4

−

# gnu screen on rl4

scr2:4:respawn:/sbin/mingetty --autologin <user> tty1 linux

scr2:4:respawn:/sbin/mingetty --autologin <user> tty1 linux

−

</pre>

+

}}

−

The line uses mingetty to [[automatically login some user to a virtual console on startup]]. You will need to install the [http://aur.archlinux.org/packages.php?ID=13793 mingetty package] (AUR). The inittab line segments are separated by colons. The first part (scr*) is simply an id. The second part is the runlevel: This should only happen on runlevel 4 (which isn't used in any default setup - 3 is by default for a tty login and 5 is for X). 'Respawn' causes init to repeat the command (i.e. autologin) if the user logs out.

−

We'll need to see that nothing else happens on virtual console 1 when we use runlevel 4, so remove '4' from the the first of the agetty lines:

−

<pre>c1:235:respawn:/sbin/agetty -8 38400 vc/1 linux</pre>

+

The above line uses mingetty to [[automatically login some user to a virtual console on startup]]. You will need to install the {{pkg|mingetty}} package from the [[Official Repositories|official repositories]]. The inittab line segments are separated by colons. The first part (scr*) is simply an id. The second part is the runlevel: This should only happen on runlevel 4 (which is not used in any default setup - 3 is by default for a tty login and 5 is for X). 'Respawn' causes init to repeat the command (i.e. autologin) if the user logs out.

+

We will need to see that nothing else happens on virtual console 1 when we use runlevel 4, so remove {{ic|4}} from the the first of the agetty lines:

−

Once logged in we want to ensure that screen is started. Add the following to the end of your .bash_profile:

+

{{bc|c1:235:respawn:/sbin/agetty -8 38400 vc/1 linux}}

−

<pre>

−

vico="$(tty | grep -oE ....$)"

−

case "$vico" in

−

tty1) TERM=screen; exec /usr/bin/screen -R arch;;

−

esac

−

</pre>

+

Once we are logged in, we want to ensure that screen is started. Add the following to the end of your {{ic|~/.bash_profile}}:

−

This checks for the current runlevel and will launch a screen session immediately after the autologin if the runlevel is 4.

+

{{bc|<nowiki>

+

vico="$(tty | grep -oE ....$)"

+

case "$vico" in

+

tty1) TERM=screen; exec /usr/bin/screen -R arch;;

+

esac

+

</nowiki>}}

+

This checks for the current runlevel and will launch a screen session immediately after the automatic login if the runlevel is 4.

−

This can also be adapted to run screen on a virtual console next to X, simply checking for the current tty instead of the current runlevel. This check to see if we're on virtual console 3:

+

This can also be adapted to run screen on a virtual console next to X, simply checking for the current tty instead of the current runlevel. This checks to see if we are on virtual console 3:

−

<pre>

+

{{bc|<nowiki>

−

vico="$(tty | grep -oE ....$)"

+

vico="$(tty | grep -oE ....$)"

−

case "$vico" in

+

case "$vico" in

−

vc/3) TERM=screen; exec /usr/bin/screen;;

+

vc/3) TERM=screen; exec /usr/bin/screen;;

−

esac

+

esac

−

</pre>

+

</nowiki>}}

−

Set inittab/mingetty to automaically log in to vc/3 on runlevel 5 and you're set.

+

Set inittab/mingetty to automatically log in to vc/3 on runlevel 5 and you are set.

−

== Fix Midnight Commander hard hang when starting in screen ==

+

===Setting a different bash prompt while in screen===

−

In some cases (need deeper inspection) [https://bugzilla.redhat.com/show_bug.cgi?id=168076 old gpm bug] gets alive. So, then you try to run mc inside screen, you get a frozen screen's window. Try to kill gpm daemon before starting mc and/or disable it in ''/etc/rc.conf''.

+

If you want a different bash prompt when in a screen session, add the following to your .bashrc:

In some cases (need deeper inspection) [https://bugzilla.redhat.com/show_bug.cgi?id=168076 old gpm bug] gets alive. So, then you try to run mc inside screen, you get a frozen screen window. Try to kill gpm daemon before starting mc and/or disable it in {{ic|/etc/[[rc.conf]]}}.

+

+

=== Fix for residual editor text ===

+

When you open a text editor like nano in screen and then close it, the text may stay visible in your terminal. To fix this, put the following:

* [[Xpra|Xpra - An utility to detach/reattach X programs, in a way similar as screen does for command line based programs]]

Revision as of 14:10, 6 May 2013

zh-CN:GNU ScreenGNU Screen is a wrapper that allows separation between the text program and the shell from which it was launched. This allows the user to, for example, start a text program in a terminal in X, kill X, and continue to interact with the program. Here are a couple of tips and tricks you may be interested in.

Tips and tricks

Change the escape key

Start at window 1

By default, the first screen window is 0. If you'd rather never have a window 0 and start instead with 1, add the following lines on your configuration:

$HOME/.screenrc

bind c screen 1
bind ^c screen 1
bind 0 select 10
screen 1

Nested Screen Sessions

It is possible to get stuck in a nested screen session. A common scenario: you start an ssh session from within a screen session. Within the ssh session, you start screen. By default, the outer screen session that was launched first responds to Template:Keypress commands. To send a command to the inner screen session, use Template:KeypressTemplate:Keypress, followed by your command. For example:

The above line uses mingetty to automatically login some user to a virtual console on startup. You will need to install the mingetty package from the official repositories. The inittab line segments are separated by colons. The first part (scr*) is simply an id. The second part is the runlevel: This should only happen on runlevel 4 (which is not used in any default setup - 3 is by default for a tty login and 5 is for X). 'Respawn' causes init to repeat the command (i.e. autologin) if the user logs out.
We will need to see that nothing else happens on virtual console 1 when we use runlevel 4, so remove 4 from the the first of the agetty lines:

c1:235:respawn:/sbin/agetty -8 38400 vc/1 linux

Once we are logged in, we want to ensure that screen is started. Add the following to the end of your ~/.bash_profile:

Setting a different bash prompt while in screen

Troubleshooting

Fix Midnight Commander hard hang when starting in screen

In some cases (need deeper inspection) old gpm bug gets alive. So, then you try to run mc inside screen, you get a frozen screen window. Try to kill gpm daemon before starting mc and/or disable it in /etc/rc.conf.

Fix for residual editor text

When you open a text editor like nano in screen and then close it, the text may stay visible in your terminal. To fix this, put the following: