I have a largish project with many hundreds of source files involved, and duplicate filenames. I would like to set the buffer names (automatically) to the (projectile) project relative path, instead of (currently) just the leaf name, which is driving me nuts in this case!

For example foo/bar.txt when I load ~/project/foo/bar.txt.

OR, a valid alternative would be to display this relative path some other way; if there was a second mode line then the full path could even be displayed, but as it is even some of the project relative paths are a bit long for the current status line.

How might I do this?


1 Answer 1


Actually, not quite as difficult as it seemed, added this to my init.el and it seems to work:

(defun my-proj-relative-buf-name ()
   (file-relative-name buffer-file-name (projectile-project-root))))

(add-hook 'find-file-hook #'my-proj-relative-buf-name)

Edit/update, it turns out this causes problems sometimes, first time I tried M-x package-install I couldn't carry through the operation because of some sort of 'not a project' error thrown by projectile-project-root, so here's a revised version:

(defun my-proj-relative-buf-name ()
     (file-relative-name buffer-file-name (projectile-project-root)))))

(add-hook 'find-file-hook #'my-proj-relative-buf-name)

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.