Uniquify your buffer names

If you open more than one file that has the same name (say test.txt), then by default Emacs will add a number to the end of the buffer name to distinguish them, so you would see test.txt <1> and test.txt <2> and so on. This is not very useful as it is easy to lose track of which file is which.

Luckily it is easy to fix with some simple tweaks (I’ve taken these from the configuration files for prelude). Add these to your emacs config file and your buffer names will be made unique by adding just enough of the path to the file. So you might see docs/test.txt and scratch/test.txt. Much nicer!

;; meaningful names for buffers with the same name
;; from prelude
;; https://github.com/bbatsov/prelude
(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)
(setq uniquify-separator "/")
(setq uniquify-after-kill-buffer-p t)    ; rename after killing uniquified
(setq uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers
  • Michael Cox

    It confuses me more.

    When I enable the setup, I got the result as below:

    file location -> buffer name
    ——————————————————-
    /home/my/tmp/t.txt -> my/t.txt
    /tmp/t.txt -> /t.txt

    • You could try (setq uniquify-strip-common-suffix nil)
      and then you would see
      tmp/t.txt
      and
      /tmp/t.txt
      But I’m not sure if that is much better. You can look at the customisation options for uniquify with
      M-x customize-group uniquify
      and see if there is anything there that will help

  • Excellent tipT thanks for sharing

  • Clément Pit-Claudel

    I think this has been the default since at least Emacs 24.5, right?