28

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" (interactive) (let ((enable-local-variables :all)) (hack-dir-local-variables-non-file-buffer))) And if you want to reload dir-locals for every buffer in ...


22

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 modes are enabled via eval. I think you wanted this: ((nil . ((eval . (flycheck-mode 1)) (cmake-ide-dir . "~/workspace/Samira3D") (cmake-ide-...


20

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 if any value is valid. (defvar my-variable-with-any-safe-value …) (put 'my-variable-with-any-safe-value 'safe-local-variable (lambda (x) t)) or (...


12

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 is OK if you know the set of possible values, as your question implies you do. If you want to make a variable safe for all values that satisfy a predicate, you ...


9

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 variable applied to a subdirectory of your current directory. So I expect giving it the absolute path to a directory is causing your problem. In your case, I think ...


9

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 time, you can add the following to your ~/.emacs: (add-to-list 'safe-local-variable-values  '(LaTeX-command . "lualatex -shell-escape"))


9

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-local-variable 'my-project-path) (file-name-directory (let ((d (dir-locals-find-file "."))) ...


8

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 for any/all major modes: ((nil . ((eval . (flycheck-mode 0))))) Local variables are processed after mode hooks, so this should reliably counteract your ...


8

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 directory and using it to enable auto-revert-mode. Specifically, the file should look contain this: ((nil . ((eval . (auto-revert-mode 1))))) If you want only org ...


7

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 (nil . ((indent-tabs-mode . t) (fill-column . 80))) (c-mode . ((c-file-style . "BSD") (subdirs . nil))) ;; Subdir specification ("src/...


6

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 move the project or use it on another machine. ((nil . ((eval . (progn (require 'projectile) (setq company-clang-...


5

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), then type demo RET, then "this is a demo" RET. Your .dir-locals.el file will appear in a buffer: ;;; Directory Local Variables ;;; For more information see (...


5

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 (concat lds-dir "settings.el")) (recursive-load-dir-settings (file-truename(concat lds-dir ".."))))))) (defun load-dir-settings() (interactive) (when ...


5

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: ((nil (eval (lambda () (when (string= (file-name-nondirectory buffer-file-name) "file-name.extension") (my-mode)))))) A ...


5

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 appropriately based on the presence or not of a local variable (which could be directory-local). It can still be called from anywhere, but you would ensure that it never ...


5

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 prompted for a variable, and you could type/select lsp-purescript-use-npx. Finally, you should be prompted for the value. Depending on your setup, you may or may ...


4

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 specify the major mode for only a given subset of files in the directory. Addendum: Amusingly, it also has the side effect of setting the major mode of .dir-...


4

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 cdr of each such mode-vars pair, i.e. the mode-specific settings, should also form an alist: ((mode . ((var . val)))) instead of ((mode . (var . val))). Each ...


4

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 to be discussed. In the following I add the missing closing parenthesis ): ((nil (tags-table-list ("/R/Source/R-devel-SVN/" "/gitRepos/DADS/")))) Nevertheless,...


3

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 local variable foo to 42 in lisp-mode then do something like this: (add-hook 'lisp-mode (lambda () (setq-local fill-paragraph-function 'my-fill-para))) Or better ...


3

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 nil if no backup is to be done. For example, (defun my-backup-enable-predicate (name) (let (found) (dolist (specialdir '("/somedir/" "/some/other/") ...


3

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 with iffy options Emacs will save your choice into this custom.el file, leaving you free to share your init.el file publicly. If you like to use Emacs' ...


3

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: ((markdown-mode . ((eval . (wc-mode 1))))) I have assumed that markdown was an error, and have substituted markdown-mode.


3

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))) (nil . ((indent-tabs-mode . t)))) I feel this is a change of behaviour, and possibly a bug. It's not flagged in NEWS in any case. Edit: Ok, definitely a ...


3

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 . " */") (comment-padding . 0)))) Or you could even use nil, which applies to every mode (again, maybe that's ok in practice). ((nil . ((comment-start . "/*...


3

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 Emacs. Probably introduced at or before Emacs version 24.3. Documentation: Non-nil means dir-local variables will be applied to remote files. n.b. I believe ...


2

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) nil) Then it'll only re-confirm when you edit one.


2

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 for your scenario, you can remove your "current directory" file and specify all the variables in the parent .dir-locals.el file. Keys which are strings ...


2

The variable compile-command is not buffer local by default, thus you can not set it per buffer. To change this add the following to your init file: (make-variable-buffer-local 'compile-command)


2

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-fill to text-mode-hook. But the hook is not run again and your configuration is without effect. If you want to deactivate truncate-lines for all major modes and ...


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