how to save .dir-local variables in a seperate directory

712 Views Asked by At

Now I'm trying to use .dir-locals.el for my own projects. However it is saved at the end of init.el whenever I choose to save it permanently. I'd like to change it to an another seperate file - eg ~/mydirlocals.el.

Please let me know what could be the solution for this.

PS : I've already tried to change custom file to a seperate one. But unfortunately it saved my dir-local variables with other custom variables. I want to avoid this and save my dir-locals variables in a completely seperate file.

2

There are 2 best solutions below

3
On

You can't use the customize interface to update .dir-locals.el files. Customize is for your own config. If you want to edit/update a directory-local variables file, you need to either edit that file directly, or use M-x add-dir-local-variable.

The latter command will prompt you for the details. Note that no default value is offered at the prompt for the value of the variable, but that you can type M-n or <down> to obtain the variable's value in the current buffer.

Note also that the command does not ask which directory the variables are local to -- it will create/update a .dir-locals.el file in the default directory for the current buffer. Issuing the command from a dired buffer for the intended directory is a safe approach, naturally, but you may wish to do so from a buffer in the mode for which you wish to add variables -- that way the default suggestion for the mode, and the current values of the variables in question, will be more useful to you.

(If there is no file of the appropriate type in the directory, you can always C-xC-f a new/unsaved buffer of an appropriate filename, use add-dir-local-variable as many times as necessary, and then when you're done just kill any new buffers you created without saving them.)

That all said, I'm still not 100% sure what your requirement is, as your question is a little confused; but you may also like to know that you can use directory-local variables without a .dir-locals.el file at all, as you can alternatively configure them entirely in your init file.

See https://www.emacswiki.org/emacs/DirectoryVariables for details and examples of that.

0
On

Disclaimer: I haven't used dir-local vars, but I have used file-local vars. I'm guessing dir-locals work the same.

The local variables aren't getting saved at the end of init.el or custom.el, the safe values are. As in, Emacs doesn't trust them by default, unless they match some sort of predicate indicating they're OK. This is a good policy, because file and dir locals can cause Emacs to run arbitrary code just by opening a file. When you apply permanently, you're telling Emacs that that value is safe; it basically just makes a predicate that matches the exact value and stores that with customize.

If you want to prevent the prompt (and thus the saving), you need mark the variables as safe with your own predicate.

For example, I set

(put 'adaptive-wrap-extra-indent 'safe-local-variable 'integerp)

which means that adaptive-wrap-extra-indent is OK as long as it's an integer. I know this is OK because I added that variable to the adaptive-wrap package (though I didn't know about safe locals at the time; I submitted a bug to fix it, which appears to be ignored). Clearly you can use any predicate, including (lambda (x) t), though I'd recommend against that.