Commands to Rename Frames

Library frame-cmds.el contains two commands for renaming frames:

Prefix arg ALL-NAMED non-‘nil’ means rename all frames named FRAME to NEWNAME.
OLD-NAME can be a frame, its name, or ‘nil’. Default is ‘selected-frame’.
NEW-NAME is a string or ‘nil’. Default NEW-NAME is current ‘buffer-name’.
Prefix arg ALL-NAMED non-‘nil’ means rename all frames named FRAME to NEWNAME.
OLD-NAME can be a frame, its name, or ‘nil’. Default is ‘selected-frame’.
NEW-NAME is a string or ‘nil’. Default NEW-NAME is current ‘buffer-name’.

Setting the Frame Title and Icon Title

The frame title is changed by changing the value of the variable ‘frame-title-format’. Likewise, the icon title can be changed with ‘icon-title-format’. They recognize most of the same special characters as variable ‘mode-line-format’; see that variable for further details:

  A string is printed verbatim in the mode line except for %-constructs:
    (%-constructs are allowed when the string is the entire mode-line-format
     or when it is found in a cons-cell or a list)
    %b -- print buffer name.      %f -- print visited file name.
    %F -- print frame name.
    %* -- print %, * or hyphen.   %+ -- print *, % or hyphen.
          %& is like %*, but ignore read-only-ness.
          % means buffer is read-only and * means it is modified.
          For a modified read-only buffer, %* gives % and %+ gives *.
    %s -- print process status.
    %p -- print percent of buffer above top of window, or Top, Bot or All.
    %P -- print percent of buffer above bottom of window, perhaps plus Top,
          or print Bottom or All.
    %m -- print the mode name.
    %n -- print Narrow if appropriate.
    %z -- print mnemonics of buffer, terminal, and keyboard coding systems.
    %Z -- like %z, but including the end-of-line format.
    %[ -- print one [ for each recursive editing level.  %] similar.
    %% -- print %.   %- -- print infinitely many dashes.
  Decimal digits after the % specify field width to which to pad.

Examples of Setting Frame and Icon Titles

Note that most of the following examples use lists but string values are also valid.

    (setq frame-title-format '("" "%b @ Emacs " emacs-version))
    (setq frame-title-format '("" "[%b] - Emacs " emacs-version))
    (setq frame-title-format (list "%b - " (getenv "USERNAME") "@" (getenv "USERDOMAIN"))) ;;list is needed here, could someone explain ?
    ;; list is needed in the last example to flatten the sub-lists, otherwise a quote is sufficient
Also see BackquoteSyntaxLawrenceMitchell)

Another example to use in an init file, used on e.g. Ubuntu where USERDOMAIN doesn’t exist, and the environment variable HOSTNAME isn’t exported by default (as can be seen by running “export -p” at a login shell), and we want to get the host name by running the POSIX ‘hostname(1)’ command only once, at startup.

    (setq my-hostname 
      (replace-regexp-in-string "\\(^[[:space:]\n]*\\|[[:space:]\n]*$\\)" "" ;; like perl chomp()
           (call-process "/bin/hostname" nil standard-output nil))))
    (setq my-username (getenv "USERNAME"))
    (setq frame-title-format '("%b - " my-username "@" my-hostname))
the one before didn’t work for me (suse linux), here’s the one that works:
    (setq frame-title-format (list "%f - " (getenv "USER") "@" (getenv "HOSTNAME")))
here’s another way to see “USER@HOST”:
    (setq frame-title-format `(,(user-login-name) "@" ,(system-name) "     " global-mode-string "     %f" ))

And if you’re an ErcChannelTracking fan, take a look at

    (setq frame-title-format '("" erc-modified-channels-object "%b @ Emacs " emacs-version))

To set both frame and icon title to the same thing, which also tells if you are using GnuEmacs or XEmacs:

    (setq frame-title-format
          (setq icon-title-format
                (format "%%b - %semacs" (if (featurep 'xemacs) "x" ""))))

Display date/time if ‘display-time-mode’ is set:

    (setq frame-title-format '("" invocation-name "@" system-name "     "
      global-mode-string "     %f" ))
    ;; See also:  `display-time-format' and `mode-line-format'

Feel free to add your titles here!


By Kelvin Hu (edited by Reuben Thomas):

Hi guys, I find a almost perfect format, as below:

    (setq frame-title-format
          '(buffer-file-name "%f"
            (dired-directory dired-directory "%b")))

Setting Xterm Window Titles from within Emacs

When running Emacs inside of xterm (e.g. with emacs -nw), it is possible to mostly mimic the behavior of frame/icon titles. To do so, you will need a copy of xterm-title.el, xterm-frobs.el, and the following snippet for your .emacs:

    (when (and (not window-system)
               (string-match "^xterm" (getenv "TERM")))
      (require 'xterm-title)
      (xterm-title-mode 1))

Then modify the frame/icon frame title variables as you would in a window system.


By avar:

My Emacs insists that I have a “dumb” terminal. Even under xterm. This works for me:

  (when (and
         (not window-system)
          (string= (getenv "TERM") "dumb")
          (string-match "^xterm" (getenv "TERM"))))
    (require 'xterm-title)
    (xterm-title-mode 1))

By PaulGoins:

My Emacs (emacs-snapshot on Ubuntu, version 1:20090730-1~intrepid1) says TERM is “dumb” after fully loading, but while loading .emacs it does show the right thing (rxvt-unicode, in my case). “Your mileage may vary”, I suppose.

(Note: xterm-title-mode does not seem to work with urxvt. It does work fine with other xterm-compatibles like gnome-terminal.)

By wisnij:

I wasn’t able to get xterm-title working with my Cygwin xterm for some reason. As a stopgap, using xterm-frobs directly seems to work well enough:

  (if (and (not window-system)
           (string-match "^xterm" (getenv "TERM")))
        (require 'xterm-frobs)
        (defun my-xterm-title-hook ()
            (cond (buffer-read-only "%  ")
                  ((buffer-modified-p) "*  "))
            "  (" invocation-name "@" system-name ")")))
        (add-hook 'post-command-hook  'my-xterm-title-hook))
      (setq frame-title-format
              (:eval (cond (buffer-read-only "%%  ")
                           ((buffer-modified-p) "*  ")))
              "%b  (" invocation-name "@" system-name ")")))

By CraigMcDaniel:

As an alternative to using xterm-title.el/xterm-frobs.el, send your own escape codes to the terminal. I’m running iTerm2 so this example sets the window title to the full file name and the tab title to the shorter buffer name:

  (defun xterm-title-update ()
    (send-string-to-terminal (concat "\033]1; " (buffer-name) "\007"))
    (if buffer-file-name
        (send-string-to-terminal (concat "\033]2; " (buffer-file-name) "\007"))
        (send-string-to-terminal (concat "\033]2; " (buffer-name) "\007"))))
  (add-hook 'post-command-hook 'xterm-title-update)

By mcandre:

If you already show directories in the buffer name, you may want to use a simpler format:

  (setq uniquify-buffer-name-style 'post-forward
             uniquify-min-dir-content 7))
  (setq frame-title-format '("%b"))

For ncurses / CLI title setting, xterm-title doesn’t seem to work for me in (Mac OS X). So I just use xterm-frobs directly:

  (require 'xterm-frobs)
  (defun my-xterm-title-hook ()
        (xterm-set-window-title (buffer-name)))
  (add-hook 'post-command-hook  'my-xterm-title-hook)

By Jens Bo

In order to mark that there are unsaved changes in the current buffer one could use:

  (setq frame-title-format
    '((:eval (if (buffer-file-name)
                  (abbreviate-file-name (buffer-file-name))
      (:eval (if (buffer-modified-p) 
                 " •"))
      " - Emacs")

CategoryFrames CategoryDisplay FrameModes