tags:

views:

143

answers:

2

One problem that I have with emacs is that it doesn't seem to handle like-named files in different directories very well. For example, if I'm not careful, I'll end up with 20 __init__.py buffers open. What I've been doing is using M-x rename-buffer and renaming it to indicate what package it's within. However, doing this manually is somewhat tedious.

Does anyone have any strategies for attacking this problem?

+17  A: 

I like uniquify, which comes with Emacs:

(require 'uniquify)

(setq uniquify-buffer-name-style 'reverse)
(setq uniquify-separator "/")
(setq uniquify-after-kill-buffer-p t) ; rename after killing uniquified

(setq uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers (or Gnus mail buffers)

With those settings, the directory gets added to the buffer name, giving you an indication of where the file is. For example, loading the files /some/path/to/Makefile and /some/path/to/different/Makefile would result in the following buffer names:

Makefile/to           (which is /some/path/to/Makefile)

and

Makefile/different    (which is /some/path/to/different/Makefile)

uniquify also handles updating the buffer names when buffers are deleted, so when one of the two Makefile buffers is deleted, the other gets renamed to simply Makefile.

Trey Jackson
Works like a charm. Thanks!
Jason Baker
I too was happy to discover uniquify.
seth
And it comes with Emacs Starter Kit by default.
Török Gábor
You mean Emacs Starter Kit enables it by default? Because it's shipped with Emacs itself.
Trey Jackson
A: 

If you want full control you can redefine create-file-buffer.

If you want the full filename it could be as simple as

(defun create-file-buffer (filename)
  "Create a suitably named buffer for visiting FILENAME, and return it."
  (generate-new-buffer filename))

See files.el for reference.

starblue
It's not a wise thing to redefine a standard library function. Better advise it how uniquify does.
Török Gábor