Home > News > Command-Line Multitasking with Screen

Command-Line Multitasking with Screen

Screen.  What can I say about it?  It’s not very well known to many people I mention it to but its one of those programs that you just can’t stop using once you’ve started.  Yes, I use screen on a regular daily basis and it’s really easy to use.

I know someone who says the only reason they invented X was so that you could use more virtual terminals.  I think the real solution behind great command line multitasking is screen!

Here are some basic usage tips.  To start a command line based program within a virtual ‘screen’ prefix that command with screen, such as:

screen irssi

Once that has loaded you can detach the virtual screen for later use by pressing ctrl-a d.  To reattach that screen type screen -r (or, if the screen was somehow not completely detached previously you would use screen -dr).

Now, ok, I can start applications within screen and detach them.. that’s cool I guess but couldn’t I just background the process or something similar?  I suppose so, but here is where it gets interesting.

Create a new screen instance within screen with ctrl-a c.  Do this as many times as you might need, based on the number of apps you’re using.  To later switch between these screens use ctrl-a 0-9, ctrl-a n,p (for next or previous) or to see a visual list, use ctrl-a “.

To close a screen you no longer need you can simply type exit which will take you to the next screen or exit screen completely if it is the last one.

In my setup I’m running irssi, mutt, bash and snownews within screen 0-3 over ssh to a private server at home.  On my local machine I load screen for rtorrent and an irssi notification script.  Once I get them both going I ctrl-a d, close gnome-terminal and don’t bother with those apps until I need something. At that point I screen -r and there they are again, right back where I left them.. and having continued that whole time.  Like I said, easy multitasking on the command line.

Do you have any magic screen tips to share with the crowd?  You know where the comments go.

If this article has been helpful, please consider linking to it.

Categories: News Tags: ,

Related Posts

  1. vincent
    May 4th, 2007 at 08:44 | #1

    I really like the idea of screen, but every time I tried to use it, I gave up for a tiny and bad reason : ctrl-a.

    As a matter of fact, ctrl-A is a readline shortcut to place the cursor at the beginning of the current line. I use it a lot, and not being able to use it inside a screen session is very annoying.
    I didn’t find a quick trick to solve this problem, but if you had a solution I’d like to hear it :)

    • nibb
      April 17th, 2009 at 10:25 | #2

      dude – you just go 'ctrl-a' and the 'a', screen escapes and allows you to do the second a to get to the beginning of the line

  2. Treenaks
    May 4th, 2007 at 09:01 | #3

    I’ve set my escape (that’s what it’s called) to Ctrl+X instead of Ctrl+A.

    Just put this bit in your ~/.screenrc:

    escape ^Xx

    I also have

    startup_message off

    in there.. because I know I’m starting screen :)

  3. Treenaks
    May 4th, 2007 at 09:02 | #4

    Also, you could just send Ctrl+A using Ctrl+A a (or a Ctrl+X using Ctrl+X x in my case)

  4. Byron Clark
    May 4th, 2007 at 09:04 | #5

    I always add these lines to my .screenrc:

    vbell on
    startup_message off
    shell -bash
    caption always “%{Gb}%3n %t %=[%l | %H | %c]”

    The last one gives a nice colored status bar that shows which screen windows is currently visible along with some other useful information like hostname, time, and load average.

  5. May 4th, 2007 at 09:25 | #6

    Open terminal 1, and start a screen up
    Open terminal 2, and type: screen -x

    Both screens point to the same session. Damn useful for teaching someone CLI!

    I set screen to run on all of my servers when sshing in, that way a ssh disconnection never interrupts whatever I’m doing.

    To show multiple sessions at the same time, you can “split” (Ctrl+a S) instead of new (Ctrl+a c). To switch between splits, you can use Ctrl+a .

    Ctrl+a A – allows you to rename the session name (useful to distinguish between sessions when you’ve invoked Ctrl+a “).

    Most importantly, Ctrl+a ? for help :-)

  6. May 4th, 2007 at 09:28 | #7

    To attach to a not-detached screen session, i.e. to start up a second
    screen “view” or “mirror” into the same screen session (Multi display
    mode), where both users can “share” control of the screen session, use:

    screen -x

    The TV capture program “atscap” uses
    GNU screen:

    http://www.penlug.org/twiki/bin/view/Main/DigitalTelevisionAtscap
    http://sourceforge.net/projects/atscap

  7. Mikael Eriksson
    May 4th, 2007 at 10:45 | #8

    This is what I have in my .zshrc. It makes the title of the terminal window change to “command (user@host dir)” when a command is running and “user@host dir” when not. It also changes the title of the virtual screen inside screen.

    To use it you’ll need to make sure the ^] and ^G is real and not written out like here.

    case $TERM in
    rxvt|*term)
    preexec () { print -Pn “\e]0;$1 (%n@%m %~)^G” }
    export PS1=”%{^]]0;%n@%m %~^G%}[%n@%m %~]%# ”
    ;;
    screen)
    preexec () { print -Pn “\ek$1 (%n@%m %~)\e\\” # Set screen title
    print -Pn “\e]0;$1 (%n@%m %~)^G” # Set window title
    }
    export PS1=”%{^]]0;%n@%m %~^G^]k%n@%m %~ESC\\%}[%n@%m %~]%# ”
    *)
    export PS1=”[%n@%m %~]%# ”
    ;;
    esac

  8. ranf
    May 4th, 2007 at 12:55 | #9

    I like a neat status line at the bootom.

    $ cat .screenrc
    # Status at the bottom
    hardstatus alwayslastline
    hardstatus string ‘%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %D %Y-%m-%d %c:%s %{g}]‘

    (dunno how that will look once posted)

  9. Nesta
    May 4th, 2007 at 18:48 | #10

    Not really a screen user, found it too complicated. If you simple want to detach sessions you should try dtach. It does everything I ever wanted screen to do…detach and reattach a session :)

    Start a session:
    dtach -A /tmp/irssi irssi

    Detach session:
    Ctrl+\

    Reattach session:
    dtach -a /tmp/irssi

  10. May 5th, 2007 at 02:39 | #11

    I’ve tried to use screen a few times, but was always turned off. The drawing slowdown of vim/mutt in a local screen instance is very noticeable and unpleasant.

    However the killer bug was dropped keys when I tried to use vim from mutt from screen under ssh. When typing normally, about 1/5th of letters would be lost somewhere. The strangest thing that this happened with the combination of ssh+screen+mutt+vim. Drop any one of those programs and the problem went away.

    *shug*

    Here’s my .screenrc with the options I’ve found useful:

    startup_message off
    defscrollback 1000
    # C-A is too useful to take away.
    defescape ^bb
    escape ^bb

    # gimme back my scrollback in xterm!
    termcapinfo xterm|xterms|xs ti=\E7\E[?47l

  11. Eric
    May 20th, 2007 at 07:11 | #12

    I tried screen, but the problem is that it changes my TERM from xterm to screen.

    Then when I try mc -x (mouse support on midnight commander) it doesn’t work.

    I have not been able to find a work around to this.

  12. July 14th, 2007 at 00:00 | #13

    I have developed something for command line apps and multitasking with screen,
    Basically for my own use, but i realised many people like to use command line…

    I call it ScreenFace, ie., Screen InterFace

    You guys might wanna check it out here…
    http://flac.wordpress.com/2007/07/10/a-day-without-x-screenface-for-ubuntu/
    http://ubuntuforums.org/showthread.php?t=497602

  13. November 5th, 2008 at 05:19 | #14

    Hello
    I have make a profile in gnome-terminal, and in this profile i have put a custom-command

    –> “screen -S irssi; irssi”

    than i have make a new entry in my main-menu with an custom icon, thats start my custom gnome-terminal-profile.

    –> “gnome-terminal –geometry=1024×768+0+0 –window-with-profile=irssi-screen”

    thats all ;)

    bye

  14. May 6th, 2010 at 15:01 | #15

    Thank you! I was looking for the ctrl+a c and ctrl+a 0-9 combination :3 *hugs*

  1. May 5th, 2007 at 16:27 | #1
  2. May 17th, 2007 at 16:50 | #2
  3. May 17th, 2007 at 17:57 | #3
  4. May 22nd, 2007 at 21:40 | #4
  5. May 27th, 2007 at 13:30 | #5
  6. September 22nd, 2007 at 23:44 | #6