Sunday, June 05, 2011

Set the title of your Emacs window to the current file being edited

You can set the title of your Emacs window to the name of the file being edited. Add the following code to your .emacs file. Note, I run Emacs from within Cygwin where the HOME environment variable uses double backslashes as path separators, and the buffer-file-name uses slashes as path separators. The regular expression replacement below performs the conversion from backslashes to slashes.

(setq frame-title-format
        (if buffer-file-name
             (replace-regexp-in-string "\\\\" "/" (getenv "HOME")) "~"
             (concat (file-name-directory buffer-file-name) "%b"))


Antoine said...

Try expand-file-name.

tsengf said...

Actually, I want the opposite of expand-file-name. The code displays ~/file instead of /home/user/file.