2009-11-29

making buffer names unique

When you open a file in emacs, the buffer gets the name of that file. That's all fine, but what if you open multiple files with the same name? At least for me, it's a fairly common to have a number of different Makefile.am buffers Makefile.am<3> etc., but that does really help to find the right one at the same time. Emacs does make those names unique – Makefile.am<2>, quickly.

To do that, emacs provides uniquify – it makes buffer names unique. In your .emacs:

(require 'uniquify) 
(setq 
  uniquify-buffer-name-style 'post-forward
  uniquify-separator ":")

This is emacs, so you can influence the way in which the names are made unique. I prefer post-forward, and as separator I use a : rather than the default |. Note, instead of post-forward there are other bizarre styles, please see the documentation.

Anyway, now, when opening ('visiting') files test/a/foo and test/b/foo, their buffers get the names foo:a and foo:b. In other words, the name followed by a colon and part of the path. I think it's much clearer than the default names foo and foo<2>. One could ask why emacs should not use uniquify as its default behavior; it seems a clear improvement.

Uniquify is a small convenience that's been a documented part of emacs for 20 years. Still, somehow I missed it until this year. I suspect I am not the only one - which is why I write this.

10 comments:

lifealgorithms said...

Great!
You said it: especially when working in multiple projects that all use Makefiles, this snippet really helps. If it really has been part of standard emacs for 10 years now, I massively wonder why this option is not activated by default. I do not think, anybody likes the current numbering system...

Marko Budisic said...

Hey, thanks a lot, this is useful. Agreed, it should be the default behavior for Emacs.

draxil said...

Excellent, REALLY useful. Thanks!

abrown969 said...

I found some days ago too. VERY VERY useful. And it even comes with sane default behaviour.

Very useful for django's project/app structure too where there are multiple files with the same name.

Trevor Oke said...

very nice. This has been bugging me for a while now.

Anonymous said...

Also try toggle-uniquify-buffer-names which is much better, IMHO.

almost said...

Thanks very much! This has been something that's been annoying me for ages.

Anonymous said...

Great, thanks!

hussain(hus787) said...

Thanks. The snippet was helpful.

Cay said...

Thank you!!! I am editing ten different files called index.html at a time, and it was driving me insane! Works great with speedbar too.