Change the Terminal title bar within emacs

Oct 12, '04 11:27:00AM

Contributed by: EatingPie

This is a hint for the more hardcore hackers (read "geeks") in the audience! Previous hints talk about setting the Terminal's title bar in Terminal to, for example, display your currrent directory path. Nice ... but for me this had one little problem: I run the emacs editor in the Terminal. This allows me to edit files in multiple directories, which basically means that the path showing in my title bar often displays incorrect information (the path where I started emacs vs. the path to the file I'm editing). Unfortunately, there was no good way to have emacs change the Terminal's title bar.

Well (you guessed it), now there is! I prefer to describe the process in a stepwise fashion; read the rest of the hint for the steps...

  1. Dowload Noah Friedman's xterm-title.el and xterm-frobs.el from his emacs-lisp software page. Don't let the "xterm" fool you, this works for Apple's Terminal as well (as explained in previous hints).

  2. Put them in a "permanent" location so you can load them in emacs.

  3. Edit your .emacs file to do the actual load. Your .emacs file resides in your home directory. If it does not exist, feel free to create it if you want to try this hint. Add the following lines (doing the obvious substitutions:
       (load "[PATH-TO-FILES]/xterm-frobs")
       (load "[PATH-TO-FILES]/xterm-title")
    
    You must load xterm-frobs first! But wait...

  4. As provided, these functions WILL NOT WORK! (Hey, wouldn't be much need for a hint if it worked outta the box!). They both require a function built into emacs after version 21.3. So unless you've downloaded that version yourself, you most likely don't have the necessary function (Panther uses 21.1). So we need to add the missing function.

    Here's the trick, and it's very simple. You need to add the missing function ... which just happens to generate the text for the Terminal window's title bar. It's called format-mode-line.

    Here's a simple version that you can copy into your .emacs file. It displays the word "Emacs" followed by the current file ("buffer" in emacs parlance) you're editing.
    (defun format-mode-line (dummy)
       (concat
         (if buffer-read-only " %% "
         t (if (buffer-modified-p) " ** "))
         "Emacs" " - " (buffer-name)) )
    
    Note that the dummy argument does nothing. It simply acts as a placeholder for the required argument being passed from the xterm-window functions. The if statement also prints a ** when your buffer is modified, and a %% when it's read-only (standard emacs practice).

  5. Turn it on! To enable this functionality, you also need to add the following snippet of code to your .emacs file...
      (when (and (not window-system)
             (string-match "^xterm" (getenv "TERM")))
        (require 'xterm-title)
        (xterm-title-mode 1))
    
That should do it. The next time you start up emacs, your Terminal title will instantly change to the name of your current buffer!

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20041008195252133