Emacs: Switching Between Buffers with the Same Nam

2020-03-20 09:25发布

问题:

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.

回答1:

Use uniquify. I use it like that:

(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)


回答2:

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".



回答3:

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))