27 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 ...
phils's user avatar
  • 50.5k
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 ...
Gilles 'SO- stop being evil''s user avatar
10 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 ...
Tyler's user avatar
  • 22.2k
10 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 ...
phils's user avatar
  • 50.5k
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-...
Flow's user avatar
  • 574
7 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 ...
phils's user avatar
  • 50.5k
6 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))) ...
Mohammed Sadiq's user avatar
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 ...
Resigned June 2023's user avatar
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), ...
Picaud Vincent's user avatar
5 votes

Remember permission to execute "risky" local variables

You can disable this "don't remember risky variables" feature by putting the following in your ~/.emacs: ;; allow remembering risky variables (defun risky-local-variable-p (sym &optional _ignored)...
Tommy Jollyboat's user avatar
5 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 ...
Basil's user avatar
  • 12.3k
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 ...
John Kitchin's user avatar
  • 11.8k
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 ...
Tobias's user avatar
  • 33.1k
4 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 ...
dakra's user avatar
  • 406
4 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 . &...
Anders Johansson's user avatar
4 votes

Set multiple dir-local variables for the same mode

This is already answered, but just for another perspective: If you just want to avoid syntax problems or view correct syntax, you can call add-dir-local-variable. It prompts for a mode, then a ...
martian's user avatar
  • 548
4 votes
Accepted

Marking as safe all possible lists of strings

C-hig (elisp)File Local Variables says: You can specify safe values for a variable with a ‘safe-local-variable’ property. The property has to be a function of one argument; any value is safe if the ...
phils's user avatar
  • 50.5k
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 ...
Drew's user avatar
  • 77.1k
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: ((...
phils's user avatar
  • 50.5k
3 votes

Load package in dir-locals

I would like to create a .dir-locals.el file in my work project root This is not the tool you are looking for, or at the very least, not in the way you are trying to use it. The problem is that... ...
Daniel Pittman's user avatar
3 votes

evaluating fn in dir-locals.el

Bearing in mind that .dir-locals.el contains data, and forms are not evaluated unless you explicitly say to do so... ((nil . ((company-clang-arguments . (eval (company-clang-header-paths "/path/to/...
phils's user avatar
  • 50.5k
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))...
phils's user avatar
  • 50.5k
3 votes
Accepted

Problems trying to turn on auto-fill in a directory local way

The directory local variables are set after the major mode command run. Otherwise the major-mode dependent file local variables would not work. Therefore, you just buffer-locally add turn-on-auto-...
Tobias's user avatar
  • 33.1k
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 ...
phils's user avatar
  • 50.5k
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 ...
meuh's user avatar
  • 160
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 . " */") ...
phils's user avatar
  • 50.5k
3 votes
Accepted

Disable blacken-mode on certain projects

Setting a variable is not enough to disable the mode: you have to call the mode function with a negative argument. You can use the eval pseudo-variable for that: ((python-mode . ((eval . (blacken-...
NickD's user avatar
  • 29.3k
3 votes
Accepted

dir local “auto-mode-alist” sets wrong mode

I've reported this as bug#64415; seems like it will be rejected... To sum up This bug is about different behavior between the variable auto-mode-alist and the directory variable auto-mode-alist. The ...
shynur's user avatar
  • 5,174
3 votes

duplicate key in dir-locals file

Convenient We can add new per-directory variables anywhere; we don't need to locate where the same key is to set a new variable. Efficient For example, ;;; /tmp/.dir-locals.el ((nil . ((my/name . ...
shynur's user avatar
  • 5,174
3 votes

Access dir-locals variables with elisp

By default, directory-local variables are processed for file-visiting buffers and not too many other kinds of buffer -- anything else needs to be handled explicitly (using hack-dir-local-variables-non-...
phils's user avatar
  • 50.5k

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