7

I have two files with the same name but in different directories:

apples/main.cpp
oranges/main.cpp

I open them in one emacs window via emacs apples/main.cpp oranges/main.cpp

When I use C-x b to switch between these two buffers, the buffer names are "main.cpp" and "main.cpp<2>". I would love to be able to see the full path of these two files when switching buffers, so that I may disambiguate between the apples and the oranges version. Is there a way to do this?

One way could be to modify whatever code generates the <2> after the second main.cpp when Emacs detects that a buffer with that name is already open. However, I couldn't find how to do this.

Alan Turing
  • 12,223
  • 16
  • 74
  • 116
  • arghghghgh! I wrote an elisp function to do this, but it's at work and I don't remember how I did it. If you don't have an answer by monday, email me. – drysdam Apr 09 '11 at 20:54
  • Looks like [uniquify](http://www.emacswiki.org/emacs/uniquify) provided in the answer below gets the job done perfectly. – Alan Turing Apr 09 '11 at 22:45
  • Yep. My elisp function actually uniquifies the buffer by the `bzr` branch I'm in, which is nice. – drysdam Apr 09 '11 at 22:56

3 Answers3

13

Use uniquify. I use it like that:

(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)
Tomasz Elendt
  • 1,475
  • 13
  • 14
  • Here's are some other [uniquify settings, which you might also like to try](https://github.com/purcell/emacs.d/blob/master/init-uniquify.el). – sanityinc Apr 11 '11 at 09:41
5

Expanding on Tomasz's answer.

There are other options for uniquify-buffer-name-style, besides forward, that you may consider:

Files /foo/bar/mumble/name and /baz/quux/mumble/name would have the following buffer names in the various styles:

forward        bar/mumble/name  quux/mumble/name
reverse        name\mumble\bar  name\mumble\quux
post-forward   name|bar/mumble  name|quux/mumble
post-forward-angle-brackets   name<bar/mumble>  name<quux/mumble>

If you want to strip common directory suffixes of conflicting files, add the line below to your emacs init file.

(setq uniquify-strip-common-suffix t)

Now, if you open /a1/b/c/d and /a2/b/c/d, the buffer names will say "d|a1" and "d|a2" instead of "d|a1/b/c" and "d|a2/b/c".

Babu Srinivasan
  • 2,339
  • 23
  • 24
1

Use lusty explorer. Configure it with:

(require 'lusty-explorer)

;; Override the normal file-opening and buffer switching.
(global-set-key (kbd "C-x C-f") 'lusty-file-explorer)
(global-set-key (kbd "C-x b")   'lusty-buffer-explorer))
Ben
  • 1,339
  • 1
  • 11
  • 15