GNU Screen - Dynamic Window Titles, etc - tmux after this


So in one of my previous posts I told you about terminal multiplexing and also mentioned how cool it was. Now is the time to introduce you to GNU Screen. The wikipedia page is a good introduction already. But I should talk about it from my point of view, about how I use it. To be honest, if I knew of the abilities of tmux at the time I got to know about screen, I would have just started using tmux. It isn't too hard to switch though. It would just take a day or two of getting used to the new short cuts. Or you could go ahead and manually configure your shortcuts to keep you happy.

The point of using screen is to have multiple terminal sessions running at the same time on a particular host. At work, I was working with a team that had their database update systems running on Linux and they had several different boxes that we might have to log into through out the day. We have the following sets of boxes:
  • development box
  • a staging box
  • several production boxes
So if I were on-call for supporting the systems for maintenance activities then I would have to frequently switch between boxes. Mostly development and staging boxes were my home. I would develop new stuff on the development box, get them working there and promote them to staging manually with the help of our code repository's label features and test it out there with a larger set of data. That was the ideal scenario. But it is not just me who is doing the development. We had a team of 3 developers, so we worked together but mostly independently on different projects with rare occasions of pair programming. And our on-call rota was for a week. So I didn't want to log in to every box in a different Putty window every day. I also didn't want to lose history of some commands that I frequently use, mostly for testing something I've just written. So I had to find a way to solve this annoying problem. Too many Putty windows are just too much to handle. And what is even worse is the fact that we work on laptops and carry it along with us for meetings. When we undock them from our docking station, we lose connection and all Putty windows lose their connection, all sessions lost, any unsaved work, doomed.

And screen came to the rescue. It is pretty easy to start using screen. Run the command screen to see if you have it. It should straight away start a new screen session. Without a .screenrc file in your home directory, you wouldn't even know if you started screen. You would think that after the initial splash screen, you just got back to your prompt. I wouldn't blame you if you thought that because I did when I first started as well. That is something a little bad about screen. It doesn't seem useful at all without a little bit of configuration and this could be a reason why tmux got more popular. Not sure though, just one of the reasons people prefer tmux over screen. The presence of a .screenrc could make a whole lot of difference though! No, there is no point if it is empty. You need to put in something in there so that screen can display something useful. This configuration file, if written well, could be configured to start a few named terminal windows by default for you and could bind a command to a particular key, etc. Did that turn you off? If not keep reading. To start with, let me give you a sample .screenrc file.
term xterm-color
# use my current shell as my shell in every new screen window
# also the - means that I want it to be a login shell, so that I get my beautiful prompt
# in every window.
shell -$SHELL

startup_message off

# dynamically set my window title. To be used in combination with PS1 and PROMPT_COMMAND
#PROMPT and PROMPT_COMMAND settings given below commented:
####################################################################
# Setting up your PROMPT
# Character sequences that correspond to colors may be different
# for different terminal clients that you use. And could also be
# mistakenly left unescaped, So always use tput to the get the
# appropriate character sequence from the terminfo database.
# Use the following man pages to learn more about it:
# * man tput
# * man terminfo
# Escaping color sequences using \[colorCodeGoesHere\]
####################################################################
# Using bash's prompt customization back-slash escaped characters:
# \u - the user name of the current user
# \h - hostname upto the first '.'
# \w - the current working directory, with $HOME abbreviated
#      with a tilde (uses the value of the PROMPT_DIRTRIM variable)
# for more information use:
# * man bash
# and go to section named PROMPTING
####################################################################
# redColor=$(tput setaf 1)
# blueColor=$(tput setaf 4)
# cyanColor=$(tput setaf 6)
# colorReset=$(tput sgr0)
# PS1="\[$redColor\]\u\[$blueColor\]@\[$cyanColor\]\h\[$colorReset\] \w > "
# PROMPT_COMMAND='echo -n -e "\033k\033\0134"'

# after prompt and prompt command is set to what is above set shelltitle
# to the following and dsiaplay hardstatus always
shelltitle '> |bash'
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}]'

#use backtick instead of C+a so that it doesn't conflict with emacs controls
escape ``

#define the scrollback history to 10000 lines
defscrollback 10000

I hope my comments on each line helped you understand what each of them meant. More about the .screenrc file from its documentation pasted below:
Commands in these files are used to set options, bind commands to keys, and to automatically establish one or more windows at the beginning of your screen session. Commands are listed one per line, with empty lines being ignored. A command's arguments are separated by tabs or spaces, and may be surrounded by single or double quotes. A ‘#’ turns the rest of the line into a comment, except in quotes. Unintelligible lines are warned about and ignored. Commands may contain references to environment variables. The syntax is the shell-like $VAR or ${VAR}. Note that this causes incompatibility with previous screen versions, as now the '$'-character has to be protected with '\' if no variable substitution is intended. A string in single-quotes is also protected from variable substitution. Two configuration files are shipped as examples with your screen distribution: etc/screenrc and etc/etcscreenrc. They contain a number of useful examples for various commands.
It took me hours to figure out how to get that dynamic shell title working. The feature allows you to set the title of your new terminal window with whatever command you ran on the prompt. This is something I find cool, because I might have several windows and thus some useful name would help me figure out which one to switch to. If you didn't follow what I typed (because it took me a while to understand that myself), just do as mentioned keep your settings as described in the ~/.screenrc file and read the comments and do the necessary settings in your ~/.profile file. That should help you understand all that bit. I would also strongly recommend going through the example screenrc files as mentioned in the documentation (pasted above). It gives you a lot more options.

If you have started screen already after setting up the ~/.screenrc file mentioned earlier, then you should be able to see something pretty. Screenshot below of how it looks for me (you won't get the pretty house, that is mine):


Now that you have screen setup for basic use, you can start creating multiple sessions from the same host. Remember your screen sessions will live on the host till it is taken down for maintenance or shut down. So till then you can keep everything safe in your screen sessions. At my workplace we have a screen server, a box just to start screen sessions on. It doesn't come under the normal upgrade patch list so your screen sessions live longer on them. Maybe you could do something like that at your workplace too. It is surely a great idea. Now for using screen efficiently you would need to memorize some basic commands. You have to know how to exit a window, create a new window, name a window, traverse among windows, search screen history, etc. I don't want to duplicate information that is already available on the internet, so I'll let you check out the following links which might be useful to get up to speed:

Comments