Emacs: Switching Between Buffers with the Same Name but in Different Directories
Asked Answered
P

3

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.

Polybius answered 9/4, 2011 at 20:47 Comment(3)
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.Lockyer
Looks like uniquify provided in the answer below gets the job done perfectly.Polybius
Yep. My elisp function actually uniquifies the buffer by the bzr branch I'm in, which is nice.Lockyer
J
13

Use uniquify. I use it like that:

(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)
Jeopardize answered 9/4, 2011 at 20:56 Comment(1)
Here's are some other uniquify settings, which you might also like to try.Basenji
P
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".

Penang answered 11/4, 2011 at 13:47 Comment(0)
E
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))
Excurvate answered 9/4, 2011 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.