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

夙愿已清 提交于 2019-12-19 06:57:53

问题


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


来源:https://stackoverflow.com/questions/5607794/emacs-switching-between-buffers-with-the-same-name-but-in-different-directories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!