108

I've just introduced a friend to GNU Screen and they're having a hard time getting used to it. That makes me think about the essential things he needs to know about the excellent Screen utility, the same things that you'd think worthwhile to teach someone, a beginner, from the ground up. What are some analogies and handy tips for remembering binds, etc.?

It would be awesome.

3

18 Answers 18

96

I've been using Screen for over 10 years and probably use less than half the features. So it's definitely not necessary to learn all its features right away (and I wouldn't recommend trying). My day-to-day commands are:

^A ^W - window list, where am I
^A ^C - create new window
^A space - next window
^A p - previous window
^A ^A - switch to previous screen (toggle)
^A [0-9] - go to window [0-9]
^A esc - copy mode, which I use for scrollback

I think that's it. I sometimes use the split screen features, but certainly not daily. The other tip is if screen seems to have locked up because you hit some random key combination by accident, do both ^Q and ^A ^Q to try to unlock it.

8
  • 1
    ^A k should be added to the list to close a window. Apr 30, 2009 at 21:26
  • 7
    I don't use ^A k because of the possibility of accidentally killing more than I intend. Instead, I just exit out of the shell in a window using ^D (or exit). The screen window automatically closes after exiting the last shell in a window. Aug 6, 2009 at 2:13
  • 9
    "^A esc - copy mode, which I use for scrollback" You are a god amongst men, sir.
    – Rob Howard
    Feb 2, 2011 at 8:21
  • 4
    ^A [ does exactly the same thing.
    – atx
    Mar 11, 2011 at 7:39
  • Thanks for ^A ^D trick... I was stuck and couldn't get back to my session. In case this helps someone I wrote an article a while back which explains the basic of screens like here but has a few more tricks geekpad.ca/blog/post/… Oct 4, 2011 at 16:39
38

I couldn't get used to screen until I found a way to set a 'status bar' at the bottom of the screen that shows what 'tab' or 'virtual screen' you're on and which other ones there are. Here is my setup:

[roel@roel ~]$ cat .screenrc
# Here comes the pain...
caption always "%{=b dw}:%{-b dw}:%{=b dk}[ %{-b dw}%{-b dg}$USER%{-b dw}@%{-b dg}%H %{=b dk}] [ %= %?%{-b dg}%-Lw%?%{+b dk}(%{+b dw}%n:%t%{+b dk})%?(%u)%?%{-b dw}%?%{-b dg}%+Lw%? %{=b dk}]%{-b dw}:%{+b dw}:"

backtick 2 5 5 $HOME/scripts/meminfo
hardstatus alwayslastline "%{+b dw}:%{-b dw}:%{+b dk}[%{-b dg} %0C:%s%a %{=b dk}]-[   %{-b dw}Load%{+b dk}:%{-b dg}%l %{+b dk}] [%{-b dg}%2`%{+b dk}] %=[ %{-b dg}%1`%{=b dk} ]%{-b dw}:%{+b dw}:%<"

sorendition "-b dw"
[roel@roel ~]$ cat ~/scripts/meminfo
#!/bin/sh
RAM=`cat /proc/meminfo | grep "MemFree" | awk -F" " '{print $2}'`
SWAP=`cat /proc/meminfo | grep "SwapFree" | awk -F" " '{print $2}'`
echo -n "${RAM}kb/ram ${SWAP}kb/swap"
[roel@roel ~]$
2
  • 3
    Check out byobu (screen-profiles), too: launchpad.net/byobu May 17, 2009 at 17:00
  • This is amazing, thank you. I was struggling to figure out what was actually happening when I was trying to split my terminal. I didn't realize I needed to run screen first in order to get any of these commands to work, silly, I know.
    – Ogaday
    Nov 3, 2015 at 11:49
30

Ctrl+A ? - show the help screen!

1
  • I completely agree with this. This is much more useful than other answers, since you can deduct the other commands by it, but you cannot do it vice versa. Also it is a lot easier to remember the thing. Nov 5, 2011 at 11:28
26

If your friend is in the habit of pressing ^A to get to the beginning of the line in Bash, he/she is in for some surprises, since ^A is the screen command key binding. Usually I end up with a frozen screen, possibly because of some random key I pressed after ^A :-)

In those cases I try

^A s and ^A q block/unblock terminal scrolling

to fix that. To go to the beginning of a line inside screen, the key sequence is ^A a.

5
  • 1
    It's easy to change the escape character from ^A to something else. I've put the details in my answer. Sep 16, 2008 at 10:22
  • 1
    @agnul +1 for you for ^A a I'm a user of ^A in a normal bash session and I can't use it inside a screen session. Thanks for the great tip! :)
    – icasimpan
    Aug 13, 2012 at 5:26
  • Thank you 100 times to you for telling me how to get ctrl A working inside screen. Been looking for this all my life.
    – user674669
    May 16, 2013 at 12:47
  • Is the 'A' and 'a' case sensitive here?
    – Aditya M P
    May 20, 2013 at 16:46
  • thank you for beginning of line key seq! super helpful.
    – JPC
    Apr 3, 2015 at 18:36
12

You can remap the escape key from Ctrl + A to be another key of your choice, so if you do use it for something else, e.g. to go to the beginning of the line in bash, you just need to add a line to your ~/.screenrc file. To make it ^b or ^B, use:

escape ^bB

From the command line, use names sessions to keep multiple sessions under control. I use one session per task, each with multiple tabs:

  screen -ls         # Lists your current screen sessions
  screen -S <name>   # Creates a new screen session called name
  screen -r <name>   # Connects to the named screen sessions

When using screen you only need a few commands:

  ^A c          Create a new shell
  ^A [0-9]      Switch shell
  ^A k          Kill the current shell
  ^A d          Disconnect from screen
  ^A ?          Show the help

An excellent quick reference can be found here. It is worth bookmarking.

2
  • I use nested screen sessions. I start the outer one with screen -e^oo so I can use Ctrl+O for that and then start the inner ones with screen -m so they start new sessions.
    – staticsan
    Aug 10, 2012 at 0:50
  • 1
    You can change the escape key on the fly. In a running screen session, type ^A : followed by \Bb followed by Enter to set the escape key to ^B. In general, ^A : lets you evaluate command that your .screenrc understands.
    – malana
    Oct 17, 2013 at 12:53
8

Some tips for those sorta familiar with screen, but who tend to not remember things they read in the man page:

  • To change the name of a screen window is very easy: ctrl+A shift+A.
  • Did you miss the last message from screen? ctrl+a ctrl+m will show it again for you.
  • If you want to run something (like tailing a file) and have screen tell you when there's a change, use ctrl+A shift+m on the target window. Warning: it will let you know if anything changes.
  • Want to select window 15 directly? Try these in your .screenrc file:
bind  ! select 11
bind  @ select 12
bind \# select 13
bind  $ select 14
bind  % select 15
bind \^ select 16
bind  & select 17
bind  * select 18
bind  ( select 19
bind  ) select 10

That assigns ctrl+a shift+0 through 9 for windows 10 through 19.

7

Ctrl+A is the base command

Ctrl+A N = go to the ***N***ext screen

Ctrl+A P = go to the ***P***revious screen

Ctrl+A C = ***C***reate new screen

Ctrl+A D = ***D***etach your screen

6

http://www.debian-administration.org/articles/34

I wrote that a couple of years ago, but it is still a good introduction that gets a lot of positive feedback.

1
  • The link is (effectively) broken: $VAR1 = \'Can\'t connect to local MySQL server through socket \'/var/run/mysqld/mysqld.sock\' (111 "Connection refused") at ../lib/Singleton/DBI.pm line 95. '; Jun 9, 2020 at 16:34
6

I "must" add this: add

bind s

to your .screenrc, if You - like me - used to use split windows, as C-a S splits the actual window, but C-a s freezes it. So I just disabled the freeze shortcut.

3
  • 2
    ^-Q unfreezes if you hit ^-S
    – Plato
    Jul 24, 2013 at 22:29
  • You are right @Plato. Still I'm working in environment with users, who won't memorize almost any of the shortcuts... I needed to disable it. Jul 25, 2013 at 11:44
  • NP i just found this thread first and searched 'freeze' and didn't find what I was looking for
    – Plato
    Jul 25, 2013 at 13:50
6

Ctrl+a is a special key.

Ctrl+a d - [d]etach, leave programs (irssi?) in background, go home.

Ctrl+a c [c]reate a new window Ctrl+a 0-9 switch between windows by number

screen -r - get back to detached session

That covers 90% of use cases. Do not try to show all the functionality at the single time.

1
  • Although, when i first learned about screen I was confused about what it actually meant to detach and re-attach a screen. Quote : "...you can have long-time running tasks in separate consoles (like ssh on a remmote machine, IRC sessions, …) on a given machine, detach from the session, move from one physical location to another, connect to the machine through ssh and re-attach to the screen session to keep on working."
    – JW.
    May 27, 2011 at 12:37
4

Not really essential not solely related to screen, but enabling 256 colors in my terminal, GNU Screen and Vim improved my screen experience big time (especially since I code in Vim about 8h a day - there are some great eye-friendly colorschemes).

1
  • Thank you! That's been bothering me for a while. Dec 19, 2010 at 20:56
3

The first modification I make to .screenrc is to change the escape command. Not unlike many of you, I do not like the default Ctrl-A sequence because of its interference with that fundamental functionality in almost every other context. In my .screenrc file, I add:

escape `e

That's backtick-e.

This enables me to use the backtick as the escape key (e.g. to create a new screen, I press backtick-c, detach is backtick-d, backtick-? is help, backtick-backtick is previous screen, etc.). The only way it interferes (and I had to break myself of the habit) is using backtick on the command line to capture execution output, or pasting anything that contains a backtick. For the former, I've modified my habit by using the BASH $(command) convention. For the latter, I usually just pop open another xterm or detach from screen then paste the content containing the backtick. Finally, if I wish to insert a literal backtick, I simply press backtick-e.

4
  • brilliant. I use backticks all the time, even though I know I ought to use $(...), so now I guess I might have to change my ways. The backtick is a little out of the way, but it's nice to have a single key without modifiers needed. But why "e", and how do you remember that?
    – iconoclast
    Jun 23, 2010 at 5:58
  • I just tested 'escape ``' and it works nicely and seems easier to remember. If you hit backtick and don't get what you are expecting, just hit it again.
    – iconoclast
    Jun 23, 2010 at 6:04
  • I have done this, but is there a way to actually type a backtick while in screen? Sometimes, it's necessary to do this, and I hate having to detach to do it...
    – Guillochon
    Aug 29, 2012 at 20:49
  • @Guillouchon: the previous comment explains exactly that.
    – tripleee
    Oct 17, 2013 at 6:59
3

There is some interesting work being done on getting a good GNU screen setup happening by default in the next version of Ubuntu Server, which includes using the bottom of the screen to show all the windows as well as other useful machine details (like number of updates available and whether the machine needs a reboot). You can probably grab their .screenrc and customise it to your needs.

The most useful commands I have in my .screenrc are the following:

shelltitle "$ |bash" # Make screen assign window titles automatically
hardstatus alwayslastline "%w" # Show all window titles at bottom line of term

This way I always know what windows are open, and what is running in them at the moment, too.

3

I use the following for ssh:

#!/bin/sh
# scr - Runs a command in a fresh screen
#
# Get the current directory and the name of command

wd=`pwd`
cmd=$1
shift

# We can tell if we are running inside screen by looking
# for the STY environment variable.  If it is not set we
# only need to run the command, but if it is set then
# we need to use screen.

if [ -z "$STY" ]; then
        $cmd $*
else
        # Screen needs to change directory so that
        # relative file names are resolved correctly.
        screen -X chdir $wd

        # Ask screen to run the command
        if [ $cmd == "ssh" ]; then
                screen -X screen -t ""${1##*@}"" $cmd $*
        else
                screen -X screen -t "$cmd $*" $cmd $*
        fi
fi

Then I set the following bash aliases:

vim() {
        scr vim $*
}

man() {
        scr man $*
}

info() {
        scr info $*
}

watch() {
        scr watch $*
}

ssh() {
        scr ssh $*
}

It opens a new screen for the above aliases and iff using ssh, it renames the screen title with the ssh hostname.

1
  • You want "$@"instead of $* throughout.
    – tripleee
    Oct 24 at 7:50
2

I like to set up a screen session with descriptive names for the windows. ^a A will let you give a name to the current window and ^a " will give you a list of your windows. When done, detach the screen with ^a d and re-attach with screen -R

1

I like to use screen -d -RR to automatically create/attach to a given screen. I created bash functions to make it easier...

function mkscreen
{
    local add=n

    if [ "$1" == '-a' ]; then
        add=y
        shift;
    fi

    local name=$1;
    shift;
    local command="$*";

    if [ -z "$name" -o -z "$command" ]; then
        echo 'Usage: mkscreen [ -a ] name command

    -a          Add to .bashrc.' 1>&2;
        return 1;
    fi

    if [ $add == y ]; then
        echo "mkscreen $name $command" >> $HOME/.bashrc;
    fi

    alias $name="/usr/bin/screen -d -RR -S $name $command";

    return 0;
}

function rmscreen
{
    local delete=n

    if [ "$1" == '-d' ]; then
        delete=y
        shift;
    fi

    local name=$1;

    if [ -z "$name" ]; then
        echo 'Usage: rmscreen [ -d ] name

    -d          Delete from .bashrc.' 1>&2;
        return 1;
    fi

    if [ $delete == y ]; then
        sed -i -r "/^mkscreen $name .*/d" $HOME/.bashrc;
    fi

    unalias $name;

    return 0;
}

They create an alias to /usr/bin/screen -d -RR -S $name $command. For example, I like to use irssi in a screen session, so in my .bashrc (beneath those functions), I have:

mkscreen irc /usr/bin/irssi

Then I can just type irc in a terminal to get into irssi. If the screen 'irc' doesn't exist yet then it is created and /usr/bin/irssi is run from it (which connects automatically, of course). If it's already running then I just reattach to it, forcibly detaching any other instance that is already attached to it. It's quite nice.

Another example is creating temporary screen aliases for perldocs as I come across them:

mkscreen perlipc perldoc perlipc
perlipc # Start reading the perldoc, ^A d to detach.
...
# Later, when I'm done reading it, or at least finished
# with the alias, I remove it.
rmscreen perlipc 

The -a option (must be first argument) appends the screen alias to .bashrc (so it's persistent) and -d removes it (these can potentially be destructive, so use at own risk). xD

Append:

Another bash-ism that I find convenient when working a lot with screen:

alias sls='/usr/bin/screen -ls'

That way you can list your screens with a lot fewer keystrokes. I don't know if sls collides with any existing utilities, but it didn't at the time on my system so I went for it.

0

^A A switches back to the screen you just came from.

1
  • 2
    ^A ^A switches back, ^A A allows rename window's title. Jul 17, 2012 at 9:12
0

Ctrl + A is a great special character for Unix people, but if you're using screen to talk to OpenVMS, then not being able to ^A is going to make you bald prematurely.

In VMS, if you're editing a DCL command prior to execution from the history buffer, Insert mode is off (it has to be for a few reasons I won't get into here) ... to turn it on so you don't over-type your command rather than space things out, you have to hit `^A.

2
  • I use screen -e ^Ll for similar reasons; I simply cannot unlearn to type ctrl-A to go to beginning of line in Emacs. Fortunately, ctrl-L is a keystroke I rarely need, and also it IMHO connects nicely with its general meaning (redraw/clear screen).
    – tripleee
    Oct 17, 2013 at 6:57
  • @tripleee: My .screenrc has escape ^@^@, which uses the null character rather than control-A. On most systems, you can enter the null character as control-space, which is very easy to type. (I have a nested screen session running inside a window in my main session; for that one I have escape ^]^].) Nov 10, 2013 at 21:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.