GNU Emacs
Emacs
Dashboard

19.7.1 Making Buffer Names Unique

When several buffers visit identically-named files, Emacs must give the buffers distinct names. The default method adds a suffix based on the names of the directories that contain the files. For example, if you visit files /foo/bar/mumble/name and /baz/quux/mumble/name at the same time, their buffers will be named ‘ name<bar/mumble>’ and ‘ name<quux/mumble>’, respectively. Emacs adds as many directory parts as are needed to make a unique name.

You can choose from several different styles for constructing unique buffer names, by customizing the option uniquify-buffer-name-style.

The forward naming method includes part of the file’s directory name at the beginning of the buffer name; using this method, buffers visiting the files /u/rms/tmp/Makefile and /usr/projects/zaphod/Makefile would be named ‘ tmp/Makefile’ and ‘ zaphod/Makefile’.

In contrast, the post-forward naming method would call the buffers ‘ Makefile|tmp’ and ‘ Makefile|zaphod’. The default method post-forward-angle-brackets is like post-forward, except that it encloses the unique path in angle brackets. The reverse naming method would call them ‘ Makefile\tmp’ and ‘ Makefile\zaphod’. The nontrivial difference between post-forward and reverse occurs when just one directory name is not enough to distinguish two files; then reverse puts the directory names in reverse order, so that /top/middle/file becomes ‘ file\middle\top’, while post-forward puts them in forward order after the file name, as in ‘ file|top/middle’. If uniquify-buffer-name-style is set to nil, the buffer names simply get ‘ <2>’, ‘ <3>’, etc. appended.

The value of uniquify-buffer-name-style can be set to a customized function with two arguments base and extra-strings where base is a string and extra-strings is a list of strings. For example the current implementation for post-forward-angle-brackets could be:

(defun my-post-forward-angle-brackets (base extra-string)
  (concat base \"<\" (mapconcat #'identity extra-string \"/\") \">\"))

Which rule to follow for putting the directory names in the buffer name is not very important if you are going to look at the buffer names before you type one. But as an experienced user, if you know the rule, you won’t have to look. And then you may find that one rule or another is easier for you to remember and apply quickly.