Emacs does a wonderful job of disambiguating buffer names when I have several files with the same name open. That is, if I open a single main.cpp, the buffer is simply called main.cpp, but if I open two of them, then one will be main.cpp<foo> and the other main.cpp<bar> (where foo and bar are directories).

This is all fine and good. But there are some file names that are, frankly, worthless on their own. If I'm writing a Python project, __init__.py tells me nothing about what file I'm looking at, and I probably have a couple hundred files with that name in my repo, so I've often had the thought that it would be nice if __init__.py buffers always showed the directory they're in. Likewise, Rust has mod.rs which serves the same purpose and is, as a filename, equally useless.

Is there a whitelist or a regexp that I can use to tell Emacs to always include the directory in the buffer name for files with certain names, even if it's the only file with that name I happen to have open?

  • That would be ... difficult. The tag is made as short as possible and is recalculated every time a file buffer is created or destroyed, so it is not constant. In particular, there is no tag the first time a file buffer is created with the given name and there is also no tag on the last buffer after all the conflicting ones have been destroyed. In order to implement what you want, it seems to me that you would have to make the tags maximal (the whole path up to the last component), just in case there is a conflict at the top level of the hierarchy. Such tags would be very long and unwieldy.
    – NickD
    Commented Sep 15, 2021 at 0:06
  • 2
    I would suggest M-x ibuffer to get the association between a buffer name and the complete pathname, Or just C-x C-f on the buffer without a tag to get the path and then C-g to abort.
    – NickD
    Commented Sep 15, 2021 at 0:07
  • @NickD, I also used to C-x C-f C-g all the time :-) and that became tiresome with time.. I found uniquify-min-dir-content to save me from that tiredness. (It can be set it to a large number for complete pathname.) Hope it helps you too.
    – Y. E.
    Commented Sep 15, 2021 at 7:36

1 Answer 1


Since Emacs 28.1, customize uniquify-buffer-name-style user option:

** User option 'uniquify-buffer-name-style' can now be a function.
This user option can be one of the predefined styles or a function to
personalize the uniquified buffer name.

This change was added on Tue Sep 15 12:50:33 2020, so on Emacses built after that date, C-h v uniquify-buffer-name-style outputs (among the rest of things):

The value can be set to a customized function with two arguments
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 \"/\") \">\"))

Optionally, one may also take uniquify--create-file-buffer-advice function as an example for writing a custom advice function to be applied around create-file-buffer.

For those who don't need that much of the flexibility, the following configuration can be commonly recommended (and used on earlier version of Emacs):

(setq uniquify-buffer-name-style 'forward
      uniquify-min-dir-content 3)

[It may be also recommended to look at the documentation of these variables with C-h v and tweak the values per exact need.]


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.