I'd like to run a function/set of functions when I open a specific file. Just like file type hooks, but for a specific file.

For instance, when I open foo.bar, I'd like some functions to run.

Maybe those functions are specified on my init.el or in the file itself; I don't mind either.

Is there some built in functionality?

1 Answer 1


Just do it in a find-file-hook function. Just have the function test for your file, and if the file to visit is different then do nothing.

C-h v find-file-hook tells us:

find-file-hook is a variable defined in files.el.

Its value is

(global-eldoc-mode-check-buffers global-font-lock-mode-check-buffers epa-file-find-file-hook vc-refresh-state)

Original value was nil

List of functions to be called after a buffer is loaded from a file.

The buffer’s local variables (if any) will have been processed before the functions are called. This includes directory-local variables, if any, for the file’s directory.

The hook is run with the buffer with the visited file current, so (buffer-file-name) should give you the name of the file.

  • Hello Drew, thanks for the answer. How should I obtain the file name on the function that I'm gonna use as a hook? buffer-file-name? Commented Mar 13 at 21:40
  • 1
    Yes. I updated the answer with that info.
    – Drew
    Commented Mar 14 at 1:51

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.