Imagine if you wanted some process to run automatically, tailored to your project, after you saved your changes. For me it's exporting an org-mode file to a file 11ty can render. How is that done in emacs?
tl;dr
Get a .dir-locals.el
file that holds dotted pairs like this:
(org-mode . ((eval . (add-hook 'after-save-hook #'org-11ty-export-to-11tydata-and-html nil t))))
Explanation
Dir local variables are inherited by nested directories. So at the top level of a project, all files and files in directories under the .dir-locals.el
will have trigger my command on save.
This means that in all my org mode buffers, exporting the org file happens automatically after I save. In my case, I generate files that 11ty uses. (As for why I'm using 11ty AND org-mode to make the site and not just one or the other, it's technical debt).
These variables of course, don't have to introduce runnable code (via the eval
). They can just hold state:
;; This just sets a variable org-confirm-babel-evaluate to nil for all modes (nil . ((org-confirm-babel-evaluate . nil)))
Read more in the manual: emacs#Directory Variables.
Ergonomics
add-dir-local-variable
: You can interactively add dir-local variables instead of manually editing the.dir-locals.el
.copy-file-locals-to-dir-locals
: When you specify file specific variables, you can just have emacs put them into.dir-locals.el
for you.
Example:
I often have local file variables like below, under a commented header so it doesn't export. I can run the command to have this variable put into
.dir-locals.el
for me:* COMMENT Local Variables ;; Local Variables: ;; flycheck-disabled-checkers: (proselint vale) ;; End:
More
- Endless parenthesis covered this topic too.
- Use
.dir-locals-2.el
if.dir-locals.el
is committed to a repository and you don't like dangling changes. Found this tidbit in the manual.