Code and Hacks

Stuff I've stumbled on or figured out... mostly Perl, Linux, Mac and Cygwin.

My Photo
Name:
Location: CA, United States

Perl hacker, investor and entrepreneur.

Monday, April 12, 2010

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.

Labels:

7 Comments:

Blogger Andrew Grangaard said...

interesting, you set all the screen terminals to the same title? Do you find it distracting to find the appropriate shell within a given screen session? I suppose you only have a handful of terminals open for a given task.

I may borrow that termcap configuration to change my xterm title to match my screen title. But I rarely see my xterm title in my ion-wm setup.

I rename my individual screen terminals automatically via my shell. By default I have the hostname and path. When I run a command, a precmd appends the command to the title and resets after command completion. This gives me screen terminals with long descriptive names that are easy to navigate via screenkey-" I also wrap ssh, to set the title from the remote hostname, to handle logging into a remote host where I don't have renaming configured.

ex:
15 doka03 ~/src/local_foo/trunk : 'vim t/test.t lib/code.pm -O'

see the xterm_title, screen_title and preexec, precmd and ssh commands in my public zshrc. Similar steps can (likely) be taken in bash:
http://github.com/spazm/config/blob/master/zshrc

April 27, 2010 at 10:34 AM  
Blogger Andrew Grangaard said...

I found your blog this morning when I searched for Vroom::Vroom + markdown. I haven't seen you on perl iron man, but I've fallen behind on my reading. I'd like to expand Vroom to use markdown as an option, but I haven't looked into the Ingy's source yet.

I write my presentations for Los Angeles Perl Mongers in Vroom. It's a little minimal, but it forces me to think about what I'm trying to say. I blogged yesterday about powerpoint considered harmful.

April 27, 2010 at 10:38 AM  
Blogger Andrew Grangaard said...

edit: yes, I have seen you on iron man! I really enjoyed the "stealing from Padre for VIM" posts. Thanks for posting.

April 27, 2010 at 10:40 AM  
Blogger Lauren said...

Hi Mark, I came across your information and wanted to touch base with you regarding a Sr. Perl Developer position that I think you would be a great fit for. Please contact me if you (or anyone you know) is looking for new opportunities. I look forward to speaking with you soon!
-Lauren Quach
lquach@cypresshcm.com

September 23, 2010 at 2:53 PM  
Blogger Rajendran Thirupugalsamy said...

Nice post. Was very helpful to me.

October 28, 2010 at 6:40 PM  
Blogger Saint Andrew's Paradise said...

Does this work in all GNUs?

February 13, 2012 at 8:47 AM  
Blogger Unknown said...

Awesome, thank you.

November 21, 2012 at 1:51 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home