Tuesday, October 23, 2007

Mutiple Terminals in linux

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

No comments: