Automatically Setting Hostname as GNU Screen Window Title

I’m starting to put this in ~/.bashrc of all my servers now:

1
2
3
4
5
6
# Set screen window title
case "$TERM" in
screen)
  PROMPT_COMMAND='echo -ne "\033k$HOSTNAME\033\\"'
  ;;
esac

When I log into the machine, the screen window title (which displays on the hard status line, and in turn my urxvt window title bar) automatically changes to the hostname of the machine I’m logged into. I used to set this manually, but now I don’t have to, yay!

If your distro doesn’t set up your environment so ~/.bashrc is sourced upon login, you’ll want to put this into ~/.bash_login instead.

To get this code on a lot of servers at once, save the above snippet to a file called “prompt-command.sh” and execute:

1
for i in hostname1 hostname2 hostname3; do cat prompt-command.sh | ssh $i 'cat >> ~/.bashrc'; done

Of course, change “hostname1”, “hostname2”, etc… to the real hostnames of your machines.

Cool, huh?

Comments