How to set a directory local variable for a directory itself non-recursively? I don't want the variable set in .dir-locals.el affect the sub-directories.


My use case is: I'd like to hide dot files under ~ by setting dired-omit-files since there are quite a lot of them in my home directory. But I do not want to hide dot files of any other subdirectory of ~. Though I think I can set it using some dired-mode-hook by comparing buffer-name or buffer-file-name, I guessed there may be simpler solution by using directory local variables.

  • 1
    Not quite a duplicate, but cross-referencing with emacs.stackexchange.com/q/54974/454
    – phils
    Dec 6, 2020 at 9:27
  • You could test (string= (expand-file-name dired-directory) (expand-file-name "~/")) to see whether the current dired buffer is visiting your home directory.
    – phils
    Dec 7, 2020 at 3:00

2 Answers 2


The Directory Variables section of the emacs manual says:

Here’s an example of a ‘.dir-locals.el’ file:

 ((nil . ((indent-tabs-mode . t)
          (fill-column . 80)
          (mode . auto-fill)))
  (c-mode . ((c-file-style . "BSD")
             (subdirs . nil)))
   . ((nil . ((change-log-default-name
               . "ChangeLog.local"))))))

[... snip ...] The special ‘subdirs’ element is not a variable, but a special keyword which indicates that the C mode settings are only to be applied in the current directory, not in any subdirectories.

so that should do the job, e.g.

((nil . ((dired-omit-files . t)
         (subdirs . nil))))

although for some reason when I test, it seems to be applying to all directories, and don't have time to figure out why. Perhaps you'll have better luck!

  • 1
    That's a bug. My initial take is that dir-locals-collect-variables is permanently deleting the subdirs element, and so it has no effect in any subsequent usage of that (cached) dir-locals spec. Could you M-x report-emacs-bug?
    – phils
    Dec 4, 2021 at 1:26
  • I have no time right now, sorry. Maybe later. Dec 4, 2021 at 16:43

No, there isn't. You can set the variable in the directory-local variables, and the unset it in the child directory's variables. Or you can use file-local variables.

  • There so many subdirectories though... Dec 6, 2020 at 9:17
  • 1
    @stackunderflow I suggest you edit the question to explain your use-case in detail. What you're asking for isn't supported, but depending on what you're actually trying to achieve, there might be other convenient options.
    – phils
    Dec 6, 2020 at 9:26
  • Then consider using file-local variables instead. Or you could certainly consider extending Emacs so that it could support non-recursive directory-local variables.
    – db48x
    Dec 6, 2020 at 9:26
  • @phils I've updated the question details. Dec 7, 2020 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.