# Run hook on new buffer

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.

Thanks

• You provide no link for uniquely. Do you expect people who might help you to google for projectile and uniquely? – Drew Apr 1 '15 at 18:17
• @Drew I'm not expecting anything.. Projectile: github.com/bbatsov/projectile/blob/… Uniquely: github.com/emacs-mirror/emacs/blob/… – darksky Apr 1 '15 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 Apr 1 '15 at 18:35
• I would recommend you try smart-mode-line. By default, it displays file paths relative to the projectile project. – Malabarba Apr 1 '15 at 21:08

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.
• @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 Apr 1 '15 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 Apr 1 '15 at 18:31
• advice-add is the new way to advise. But you can still use defadvice (which is good). – Drew Apr 1 '15 at 18:37