Linux

Configure GNU Screen to handle multiple terminals


GNU Screen is a fantastic terminal "window manager" in that it lets you handle multiple terminals from within one. From a purely aesthetic point of view, Screen can consolidate multiple terminal windows into one. Not only that, but what you run under Screen can be shared with other users or used remotely if you start a Screen session at home and then ssh into your home box from work and resume the Screen session.

Screen provides a number of options and features, many of which are customizable via configuration files. The default configuration file is ~/.screenrc, but you can tell Screen to load alternate configuration files.

Suppose you use Mutt for your e-mail and sit on IRC via Irssi. Both are text-mode clients and are ideal to run under Screen. You can automate the launching of both clients under Screen quite easily, without disrupting default Screen behavior.

To begin, create a shell script and save the contents below as ~/bin/start-screens:

#!/bin/sh
/usr/bin/screen -O -S screens -c ~/.screenrc-startscreens

Next, create the file ~/.screenrc-startscreens with the following contents:

startup_message off
chdir
autodetach on
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'

screen -t shell0  0
screen -t irssi         1 /usr/bin/irssi
screen -t mutt          2 /usr/bin/mutt

This configuration file will start three Screen windows: the first is a standard shell window, the second starts Irssi, and the third starts Mutt. The other options control Screen behavior; the hardstatus line is always present and shows the names of the open windows, the hostname, and the current date/time.

Now simply execute ~/bin/start-screens to begin the Screen session, which will open a terminal at screen 0, Irssi at screen 1, and Mutt at screen 2.

To switch between screens, press [Ctrl]a and the screen number; for example, pressing [Ctrl]a and then 2 will take you to the Mutt screen (Note: the letters are case-sensitive). You can cycle through screens by pressing [Ctrl]a and then [Ctrl]n to move forward in the list or [Ctrl]a and then [Backspace] to move back in the list. To move back to the previous screen, press [Ctrl]a and then [Ctrl]a -- a great way to flip between two screens.

You can also create a new screen by pressing [Ctrl]a and then c, and kill existing screens with [Ctrl]a and then k. To change the name of a screen, press [Ctrl]A and then A. In the above example, the name associated with the Mutt screen is mutt and the name associated with the initial shell is shell0. Pressing [Ctrl]A and then A will change these names, which are shown in the hardstatus line. Note that these shortcuts are case-sensitive. To send an actual [Ctrl]a to the screen, simply send [Ctrl]a and then a.

As well, the [Ctrl]a trigger can be customized; if you find [Ctrl]a is a key command not to your liking, you can modify the screenrc to change it:

escape ^gg

The above would set the trigger to [Ctrl]g, instead of [Ctrl]a.

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

7 comments
fascorp
fascorp

Anyone knows if exists this tool for the AIX platform ? It will be very useful in my daily work

mbarber
mbarber

Screen rocks. It is this type of tool that makes Windows look silly. RDP can't touch this from a resource standpoint. If you are at the local coffee shop you can connect remotely start that 2 hour compile/backup script/analysis query ..or whatever, shutdown your laptop, go home, and reconnect to the same exact session to see what the output was. Great tool...wish I had found it 5 years ago...

aureolin
aureolin

___(begin quote)_______________________ #!/bin/sh /usr/bin/screen -O -S screens -c ~/.screenrc-startscreens Next, create the file ~/.screenrc-startscreens with the following contents: startup_message off chdir autodetach on hardstatus alwayslastline hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]' screen -t shell0 0 screen -t irssi 1 /usr/bin/irssi screen -t mutt 2 /usr/bin/mutt ___(end quote)_______________________ This is a prime example of why Linux will always remain the exclusive province of the technorati and why it will not be taking over the general desktop in the foreseeable future. Nothing against Linux, but you can't sell this to the general public. If you ever want to replace windoze, then operating Linux needs to be like driving a car: operating it must be focussed at the lowest common denominator. That ain't gonna happen with Linux. Steve G.

Penguin_me
Penguin_me

From your post it seems you're saying that because of this handy little bash script for screen you're scared of Linux ? You can almost always avoid using the command line in Linux, sometimes you will need to use it, for instance if you compile and install something ("./configure" "make" "make install" are the commands normally). Take a look at Ubuntu, or Fedora, totally GUI Desktop oriented, basically "Plug-and-play" hardware support and quick easy install. Don't judge the entire OS by the fact that the command line can be intimidating, the Windows CLI can be intimidating too, but you rarely use it I'm sure.

Photogenic Memory
Photogenic Memory

//Begin rant For me creating a "simple" shell script to get monotonous chores done is becoming a regular part of my Linux life. When I look at this part in the Bash script;: hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]' I hit a brick wall. There's so many general expressions here with percent signs and curly braces; I find myself unable to fathom what's being called to do what? I know someone's laughing and thinking; they'll never get it, but it's stuff like this that makes people run in the other direction. PLEASE be considerate when writing scripts and put in comment lines so mere HUMANS will know what's going on! I hope this isn't a case of post "some cool looking code" to show how much I know while alienating potential open source adopters? If it is; this is just lame and reeks of insecurity in the poster further fueling insecurity in it's curious but much less knowledgeable readers who might need a little hand holding to get over the Linux learning curve hump! Thanks a bunch!! //End rant

blissb
blissb

... doesn't mean it is required. It sounds like you are saying that because Linux offers complicated tools for the hardcore techie to use, general users will never use Linux... The "general public" would be no more interested in using the functionality of screen than the average MS Access user would be of normalizing his/her data to forth normal form... But they could if they wanted to.