Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to handle multiple like-named files in emacs?

Tags:

emacs

elisp

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?

like image 839
Jason Baker Avatar asked Nov 30 '22 11:11

Jason Baker


2 Answers

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.

like image 98
Trey Jackson Avatar answered Dec 06 '22 23:12

Trey Jackson


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.

like image 27
starblue Avatar answered Dec 06 '22 22:12

starblue