I have some customizations for emacs that I'd like to use for a single project, but not affect emacs globally. Is there any way I can have emacs load a specific init file when launched from a certain directory or automatically load all init files in the directory its launched from?
2 Answers
You might want to take a look at Directory variables. Basically you will need to create a .dir-locals.el
file in your project root with all the project specific setting in it. Now whenever you open a file in the directory or any of its subdirectories emacs will apply the settings in the .dir-locals.el
file.
So for example suppose you want to use the make package install
as compile command only in project A, you will need to create a file named .dir-locals.el in root directory of the project and add something like the following
((nil . ((compile-command . "make package install"))))
Henceforth whenever you run compile
command from any file under the directory A, the default compile-command
will be the one specified above, however the compile command
for files outside the directory A will stay untouched.
A less-featured alternative to .dir-locals.el
is the EditorConfig standard, which has an emacs-package. It doesn't support nearly as many options, however, it allows a team of people to standardize on some settings and use them across different editors.
It behaves much the same way, with a project-specific .editorconfig
file at the top-level.