35 votes
Accepted

Reloading directory-local variables

This function will re-read the dir-locals file and set the new values for the current buffer: (defun my-reload-dir-locals-for-current-buffer () "reload dir locals for the current buffer" ...
user avatar
24 votes
Accepted

Calling functions in .dir-locals in Emacs

Your list nesting is wrong. You need to use the eval pseudo-variable to evaluate expressions / call functions. Using mode to set a minor mode is deprecated. That's now for major modes only, and minor ...
user avatar
  • 43.8k
22 votes
Accepted

Mark a local variable safe for any value

Set the safe-local-variable property to a function that validates the value. For example, fill-column has a safe-local-variable property set to integerp. Use (lambda (x) t) as the validation function ...
user avatar
13 votes

Trust .dir-locals.el

You can customize safe-local-variable-values in your init file. This will require you to also specify which values you consider safe. (add-to-list 'safe-local-variable-values '(var . value)) This ...
user avatar
  • 1,279
9 votes
Accepted

.dir-local.el misunderstanding

When you call add-dir-local-variable, it assumes you are already in the directory you want to apply the directory-local variable to. When it prompts for a directory, it's asking if you want the ...
user avatar
  • 20.4k
9 votes

Remember permission to execute "risky" local variables

Emacs should offer (and normally does offer) the option to accept and remember the choice for later sessions. I suggest you M-x report-emacs-bug about this missing choice in your case. In the mean ...
user avatar
  • 25.4k
9 votes
Accepted

How can I turn off flycheck minor mode for given directory

Well you wouldn't want to remove the hook, as that's a global effect. You can disable the mode on a per-buffer basis, though. Use the eval pseudo-variable. e.g. a .dir-locals.el like this would act ...
user avatar
  • 43.8k
9 votes

How can I set directory local variable in relative to .dir-locals.el file location?

My solution so far (based on this Stackoverflow answer): .dir-locals.el: ;;; Directory Local Variables ;;; For more information see (info "(emacs) Directory Variables") ((nil . ((eval . (set (make-...
user avatar
  • 552
8 votes
Accepted

How to enable auto-revert-mode for files under a certain directory

Funny: it looks like you choose a tag that directly answers your question ("directory-local-variables") The best way to accomplish this is by placing a .dir-locals.el file in your org ...
user avatar
  • 5,474
7 votes
Accepted

How are file paths mapped to subdirectories in .dir-locals.el?

Based on the documentation in the manual, you are missing a portion of the subdir specification. Sample .dir-locals.el From the manual with comments for emphasis/clarification (;; Dir settings (...
user avatar
6 votes
Accepted

variable project root folder in .dir-locals.el

I found the solution with projectile. It has a function projectile-project-root which can be used to get the project path. I can noe use the following in .dir-locals.el and it will still work when I ...
user avatar
  • 785
6 votes
Accepted

does tramp mode look at .dir-locals

enable-remote-dir-locals is a variable defined in ‘files.el’. Its value is nil You can customize this variable. This variable was introduced, or its default value was changed, in version 24.3 of ...
user avatar
  • 43.8k
5 votes

How to set major mode for a file in .dir-locals.el?

The following .dir-locals.el works just fine for me to set the major mode of all files in a directory to shell-script-mode: ((nil . ((mode . shell-script)))) The obvious downside is that you can't ...
user avatar
5 votes
Accepted

How to set major mode for a file in .dir-locals.el?

It's possible to specify a string instead of a mode, but this only works for subdirectories. So I'll use nil instead to match all modes, then the eval key to change the major mode conditionally: ((...
user avatar
  • 21.1k
5 votes

How can I make an interactive function be local to directories?

So as per my earlier comment, Emacs doesn't really support what you're asking for. I would strongly recommend that you simply define a global command in the normal way, and make it behave ...
user avatar
  • 43.8k
5 votes

Reloading directory-local variables

You can also use: M-: (hack-dir-local-variables-non-file-buffer) To check it works: from a buffer (an org mode file for instance), type M-x add-dir-local-variable, then validate (default org-mode), ...
user avatar
5 votes
Accepted

White list of .dir-locals.el

I added this to my init.el: (defun recursive-load-dir-settings (currentfile) (let ((lds-dir (locate-dominating-file currentfile "settings.el"))) (when lds-dir (progn (load-file (...
user avatar
  • 692
5 votes

How to set minor mode variables in dir-locals?

As @phils suggested you should be able to run M-x add-dir-local-variable. You will be prompted for a mode, and you could type/select lsp-mode (I am guessing that is the mode name). Then, you will be ...
user avatar
4 votes
Accepted

flycheck-gcc-include-path in .dir-locals.el

I see a few potential issues: I am not familiar with a cpp-mode; I assume you mean c++-mode. The contents of a dir-locals-file should form an alist, i.e. ((mode . vars)) instead of (mode . vars). The ...
user avatar
  • 11.4k
4 votes

Customized c-offsets-alist override c-file-style in dir-locals

Instead of modifying c-offsets-alist, you can modify c-file-offsets whose purpose is to set offsets via file local variables. A small example for a .dir-locals.el file: ((nil . ((fill-column . 80))) ...
user avatar
4 votes
Accepted

How to set tags-table-list as a per-directory local variable?

All your attempts to set tags-table-list are errorneous. All cases except the second one do not define a mapping of modes to alists of settings and are therefore wrong. Only the second case remains ...
user avatar
  • 30.4k
3 votes

How to customize major mode initialization with local variables?

If you want to set a buffer-local value for a given mode, do so after the mode has been established. You do that by putting the value-assignment on the mode hook. For example, if you want to set ...
user avatar
  • 70.2k
3 votes

how to apply parent directory and current directory variable together

No, there is no provision (at least as at Emacs 24.5) for additionally continuing to look for and process ancestor .dir-locals.el files after a nearer one has been located. However, if it makes sense ...
user avatar
  • 43.8k
3 votes

White list of .dir-locals.el

If you write configuration to your init.el file manually, then you could add the following: (setq custom-file "~/.emacs.d/custom.el") (load custom-file t) Now when emacs encounters a .dir-locals.el ...
user avatar
3 votes
Accepted

How to stop auto backup in some directory in Emacs

Backup decisions seem to pass through the function normal-backup-enable-predicate, via the variable backup-enable-predicate which is set to this by default. You can provide your own function to return ...
user avatar
  • 160
3 votes
Accepted

dir-locals not working

To elaborate on theldoria's comments: If wc-mode is a major mode then either: ((markdown-mode . ((mode . wc)))) or: ((markdown-mode . ((eval . (wc-mode))))) If wc-mode is a minor mode, then: ((...
user avatar
  • 43.8k
3 votes

Enable .dir-locals.el variables inside just opened buffer

As the new buffer is not saved yet it doesn't know where to look for dir-locals. Have a look at function hack-dir-local-variables-non-file-buffer which uses default-directory to apply dir-locals to ...
user avatar
  • 366
3 votes
Accepted

Can I override the “nil” variables in .dir-locals.el with a more specific mode?

Experimentally, I think Emacs is using the first match it finds. So if you re-arrange spec to put the nil case last, you'll end up with the desired outcome. ((python-mode . ((indent-tabs-mode . nil))...
user avatar
  • 43.8k
3 votes
Accepted

Apply settings in .dir-locals.el to both c and c++ major modes

Not as such (AFAIK). However, both those modes derive from prog-mode which might be fine to target for this directory. ((prog-mode . ((comment-start . "/* ") (comment-end . " */") ...
user avatar
  • 43.8k
3 votes

Set multiple dir-local variables for the same mode

The dir-locals file contains an alist of nested alists. This would be the correct form for your example of setting multiple variables for python-mode: ((python-mode . ((python-shell-buffer-name . &...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible