Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Change the Terminal title bar within emacs Apps
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!
    •    
  • Currently 2.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[14,955 views]  

Change the Terminal title bar within emacs | 3 comments | Create New Account
Click here to return to the 'Change the Terminal title bar within emacs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Change the Terminal title bar within emacs
Authored by: yoyoregime on Oct 12, '04 10:38:46PM
Wow this worked! I couldn't find the xterm-title.el file on Noah Friedman's software page. You can get it here: http://www.splode.com/~friedman/software/emacs-lisp/src/.

---
T Koulis

[ Reply to This | # ]

Change the Terminal title bar within emacs
Authored by: kwyjibo on Oct 13, '04 11:19:05AM
I would prefer to use
buffer-file-name
instead of
buffer-name
That way, you can see the entire path of file you are editing. This is helpful when you are editing two or more files with the same name. Also, the buffer name is listed in the mode line, so it seems redundant to put it in the title bar.

[ Reply to This | # ]
Add line and column
Authored by: EatingPie on Oct 13, '04 12:10:26PM
You can display current line and current column in the title bar as well (just like your mode line). Simply add a
(what-line)
to the concat. I'm not sure if there's a "current-colum" in emacs, so I wrote my own.

 (save-excursion
    (let ((here (point)))
      (beginning-of-line)
      (- here (point))))
You'll need to run that through
(int-to-string)
before inserting in the concat. -Pie

---
-Pie


[ Reply to This | # ]