mkaz solutions log » home

Unix Screen Utility

Date: May 1, 2008

screen is a “window” manager for the terminal. Similar in concept to tabbed browsing in Mozilla, but without the GUI. It allows you to have multiple terminal sessions open while only taking up one on your desktop. Also, the screen utility allows to detach and run in the background. So you could exit the terminal; log back in; and reattach exactly how you left it, running processes and all. Great for remote server work.

Basics

Start a screen: $ screen

Once you’re in a screen, you can issue commands using ctrl-a (default), or you can map another key. I use ctrl-a often in the bash shell so I map ctrl-w as my screen command key.

Create ~/.screenrc

  startup_message off
  escape "^Ww"
  defscrollback 5000

Commands

ctrl-w ctrl-c Create New Window
ctrl-w ctrl-n Next Window
ctrl-w ctrl-p Previous Window
ctrl-w 0-9 Switch to Window #
ctrl-w d Detach from current screen (it's still there)

Detach and Reattach

You can detach from a screen session using ctrl-w d ; All your screens will still exist, with all running processes. You can even close your terminal sesssion, ie. disconnect if you were logged in remotely.

You can reattach using: $ screen -r

After reattaching, everything will be as you left it. All screens available and running processes.


Scrolling

Screen is its own window manager and operateas outside of the system windowing, which makes scrolling a bit more difficult. First make sure you define a good chunk of lines to scrollback in your screenrc. Using defscrollback 5000

Next to enter scrollback mode use ctrl-w [ and then to control scrolling it is standard vim commands.

  h,j,k,l - cursor movement
  ctrl-b  - back a page
  ctrl-f  - forward a ag
  /       - search forward
  ?       - search backward

Split Windows

ctrl-w S Split screen horizontally
ctrl-w | Split screen vertically
ctrl-w tab Navigate between splits
ctrl-w X Remove current split
ctrl-w Q Remove all other splits
ctrl-w :resize Resize Screen (prompts for lines)

Tips and Tricks

Sharing Interactive Session

You can use screen to share interactive sessions, allowing people to see and type in the same shared terminal. This could be a nice way to do XP pair-programming remotely.

Try the following:

  1. Start up to two terminal sessions, A and B, via ssh, telnet or even local.

  2. In Terminal-A, start up screen: $ screen

  3. In Terminal-B, connect to screen already established: $ screen -x

  4. Now whatever you type in either terminal will show up on both. They are actual the same screen shared.

This sharing can be done a few different ways with multiple people. At work, since most of us have admin rights, we simply “su” to the other person and connect to the screen as them.

Another way is to set the permissions on your pts/tty session, location is usually in /dev somewhere depending on your unix environment. Or use $ mesg y to set your tty session as writable.


Additional Resources