I am trying to create something like uniquely, which renames the buffer to the full path within the projectile project. I have the code working, however, I can't seem to figure out how to get it to run when a new buffer is open. It seems like Emacs does not provide any hooks for when a new buffer is opened.

What is a good way to run the code when a new buffer is opened? How does uniquely do it? I tried reading its source, but I can't seem to find where the code gets triggered to run.


  • You provide no link for uniquely. Do you expect people who might help you to google for projectile and uniquely?
    – Drew
    Commented Apr 1, 2015 at 18:17
  • @Drew I'm not expecting anything.. Projectile: github.com/bbatsov/projectile/blob/… Uniquely: github.com/emacs-mirror/emacs/blob/…
    – darksky
    Commented Apr 1, 2015 at 18:30
  • Thanks. I think that might help get you more & better answers. It was just a suggestion. Sorry if it sounded like a rebuke.
    – Drew
    Commented Apr 1, 2015 at 18:35
  • I would recommend you try smart-mode-line. By default, it displays file paths relative to the projectile project.
    – Malabarba
    Commented Apr 1, 2015 at 21:08

1 Answer 1


You can advise create-file-buffer, if you care only about file buffers. Likewise, you can advise rename-buffer.

That's what uniquify does, for instance. Are you familiar with uniquify.el? Maybe it offers all that you need. See the Emacs manual, node Uniquify.

  • Any idea how one would do it for non-file buffers? I seem to recall seeing a S.O. thread a long while back that said it couldn't be done, but I could easily be misremembering.
    – Dan
    Commented Apr 1, 2015 at 18:16
  • 2
    @Dan: AFAIK, there is no single function that all buffer-creation goes through. But you can try advising get-buffer-create. That's written in C, however. You can certainly advise generate-new-buffer, which calls get-buffer-create. (But you might also ask yourself whether you really need this for non-file buffers.)
    – Drew
    Commented Apr 1, 2015 at 18:25
  • Yes, I am familiar I read through the source and couldn't figure it out. I never heard of advise before so I will take a look at what that means. I think this solves it. Thanks!
    – darksky
    Commented Apr 1, 2015 at 18:31
  • advice-add is the new way to advise. But you can still use defadvice (which is good).
    – Drew
    Commented Apr 1, 2015 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.