Setting the Terminal Title in Gnu Screen

I often have multiple terminals open and screen running in each one. I then try to group all my work for a particular project in that terminal. While I am trying to limit the amount of multitasking and concentrate on one thing at a time, I might leave a terminal/screen session up for days and when I come back I can jump right back where I started.

The downside to this is using cmd-tab (or alt-tab with Witch) gives me a list of several terminals each with the title screen - bash - username or maybe screen - vim - username… not very helpful. What I want is to set a name for each terminal and display it in the terminal’s title. If I anticipate this before launching screen, I can accomplish it with screen -t <My Title>. Once I am inside a session though, you need to change each window’s title/name for this to work.

After searching the web and a bunch of trial-and-error, I found a solution. The first part is pretty well documented. Changing the name for all windows and the default for new windows took quite a bit a tinkering.

  1. Setup screen (via .screenrc) to update your terminals title bar and include the name of the current window.

    # Add to .screenrc
    termcapinfo xterm* 'hs:ts=\E]0;:fs=\007:ds=\E]0;\007'
    defhstatus "screen ^E (^Et) | $USER@^EH"
    hardstatus off

    The escape string ^Et in the defhstatus is converted into the current window’s name/title.

  2. Create a function to update the name of each screen window.

    # Add to .bashrc
    # Set the title of a Terminal window
    function settitle() {
     if [ -n "$STY" ] ; then         # We are in a screen session
      echo "Setting screen titles to $@"
      printf "\033k%s\033\\" "$@"
      screen -X eval "at \\# title $@" "shelltitle $@"
     else
      printf "\033]0;%s\007" "$@"
     fi
    }

You can change the name of the current window by pressing Ctrl-a A, but we want to change the title for all the windows. screen -X eval will execute each of its arguments in the current screen. The command at \# title will execute the title command in all the windows (the \ before # is required otherwise # will be interpreted as the beginning of a comment). The shelltitle command will ensure that any newly created windows use this title.

Published

April 11, 2010 8:00PM

Tags

License

The contents of this blog are licensed under the Creative Commons “Attribution-Noncommercial-Share Alike 3.0″ license.