GNU Screen Utility

GNU Screen is a “window” manager for the terminal. Similar in concept to tabbed browsing in Firefox, but without the GUI, all command-line. It allows you to have multiple terminal sessions open while only taking up one on your desktop.

The screen utility also allows you to detach and leave the session running in the background. So you can exit the terminal; log back in; and reattach exactly how you left it, running processes and all. Great for remote server work, or if you accidentally disconnect or close the wrong window.


The GNU screen command is simply: $ screen

Once you’re in a screen, you can issue commands using ctrl-a (default), or you can map another key. Emacs and bash use ctrl-a, so you may want to map the escape key to a different key. To map to Ctrl-w use escape "^Ww" (see .screenrc below)

Screen Commands

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

Detach and Reattach

You can detach from a screen session using ctrl-a 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. See the screen man page for multiple options on reattaching, I find using -RR works best.

Navigating and Scrolling

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

Next to enter scrollback mode use ctrl-a [ 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 page
/       - search forward
?       - search backward

Starting Screen Automatically

I have the following set up to start gnu sreen automatically when I log in to my server. You should place it in your shell startup (.bashrc or .bash_profile), this checks to make sure your not running screen and then starts it. When you detach from screen using ctrl-a d you will also disconnect if logged in to a remote server.

if [ $SSH_TTY ] && [ -z $STY ]; then
    exec /usr/bin/screen -d -RR -A

Using Multiple Windows

My typical usage is to have multiple full screen remote windows open using screen, for example one used as a file browser and run commands, another for vim open and editing, another tailing logs, etc… I then switch between the screens using ctrl-a ctrl-a which is really just holding down ctrl and hitting “a” twice this walks through the screens.

You can map keys to navigate between the different screens, here’s how to set the key bindings:

# bind F5 and F6 to previous and next screen window
bindkey -k F5 prev
bindkey -k F6 next

Using Titles for Your Windows

I setup my .screenrc file to display all open windows in the bottom status bar, see my config below. So using this I can set names for each of my windows to navigate through. To set a title use ctrl-a : which enters command mode and then type title YourWindowTitle

Split Windows

Also, using gnu screen you can split the screen up into different panels, which is not really part of my workflow, since I do in vim, but an incredibly useful feature. Here’s how to split windows in screen and navigate between:

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

Collaborative Coding – Window Sharing

You can use GNU screen to share interactive sessions, allowing people to see and type in the same shared terminal. This could be a nice way to do pair-programming remotely or walk through a command-line demo.

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.

Background Color in Vim

I had some problems with the background color in vim not highlighting and working properly when in a screen session. I was able to fix it by adding
term screen-256color to my .screenrc and adding the following to my .vimrc file

set t_Co=256
highlight Normal ctermbg=NONE
highlight nonText ctermbg=NONE

My screenrc config

# screenrc
startup_message off
escape "^Aa"
defscrollback 5000

# add status lines
caption always "%{= bb}%{+b w}Screen: %n | %h %=%t %c"
hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"

term screen-256color

TMUX – Terminal Multiplexer

There is a new improved window manager in town, I’ve been trying out TMUX as a replacement for screen. It is a bit more refined and easier to configure and use, but I’ve run into a few issues so far. Plus not a default in most distributions so may not be everywhere you login to. See my notes on TMUX.

Additional Resources

One thought on “GNU Screen Utility

Comments are closed.