Monday, December 7, 2009

Changing the Terminal Titlebar Text for Each Application

I have been using the awesome window manager, and it has been, well, awesome. I have also tried ratpoison, xmonad, and ion but awesome is my current favorite. Anyway, as you may have gathered from the title, window managers are not the topic of this post.

I have found myself using more terminal applications because it is easy to manage them with a tiling window manager, and I am a fan of minimizing mouse use when doing text-related tasks. However, when there are several terminal windows open, it would be nice to see what is currently running in each one, instead of having to cycle through all of the windows. I imagine that this is the case with all window managers, and it has just become more obvious with my current setup.

This technique is based on a tip found here, which is for displaying the hostname in the titlebar of an xterm window. I use rxvt-unicode as my terminal, but this technique should work with any xterm compatible terminal emulator running a bash terminal. The following is an example of the section in my .bashrc.

if [ "$TERM" = "rxvt-unicode" ] && [ "$0" = "-bash" ]
then
# Escape sequence for setting xterm title
label () { echo -ne "\e]0;$*\a"; }

# Titles for each application
alias s1='label urxvt - ${PWD#$HOME/}'
alias s2='label urxvt - vim $*'
alias s3='label urxvt - top $*'

# Commands for each application
cd_t () { "cd" $*; eval s1; }
vims_t () { eval s2; "vim" $*; eval s1;}
top_t () { eval s3; "top" $*; eval s1;}

# Assign the aliases
alias cd=cd_t
alias vim=vim_t
alias top=top_t

# Set title to home directory
eval s1
fi


Let's take a look and (hopefully) shed some light on what is going on here.

The first line is checking for the name of the terminal. Replace this with whatever terminal you are using (e.g. xterm, aterm, rxvt, etc) so that the configuration will take effect.

The next section defines a function named label that will change the title of the terminal window.
label () { echo -ne "\e]0;$*\a"; }


Next, we set up some aliases to rename the window depending on the application. The $* after each application represents the list of arguments it was called with. For example, the command vim bob.txt would name the window vim bob.txt. Shocking, no? Here we define three aliases; the first sets the window title to the name of the home directory, the second to "vim", and the third to "top", all with the accompanying arguments.
alias s1='label urxvt - ${PWD#$HOME/}'
alias s2='label urxvt - vim $*'
alias s3='label urxvt - man $*'


Now we need some functions to actually change the titles. These are defined in the next section, one for each application. One downside of this technique is that your .bashrc will become long and messy if you need to do this for many applications. The applications have been named with the scheme [application name]_t. Each function does the following:

  1. Changes the title for the current application

  2. Runs the application

  3. Resets the title to the current directory after the application closes



cd_t () { "cd" $*; eval s1; }
vim_t () { eval s2; "vim" $*; eval s1;}
top_t () { eval s3; "top" $*; eval s1;}


The final section assigns aliases for the commands actually used to the functions defined in the previous section. This allows the title window text to be changed transparently in the background. For example, when "vim" is typed, the "vim_t" function will be called.
alias cd=cd_t
alias vim=vim_t
alias top=top_t


The last line of the configuration section simply sets the title to the home directory after the terminal window is opened.

This setup has made identifying windows more convenient for me, and I imagine it would with any window manager, not just the tiling variety. Remember that this should work for any terminal application. Enjoy!