The dir-config
Emacs package automatically loads and evaluates Elisp code from a .dir-config.el
file found in the buffer's current directory or its closest parent directory. This facilitates adjusting settings or executing functions specific to the directory structure of each buffer.
For instance, you can use the dir-config
package to:
Features:
.dir-config.el
file from the directory of the current buffer or its parent directories.dir-config-allowed-directories
, ensuring control over where configuration files are sourced from.dir-config-mode
mode: Automatically loads the .dir-config.el
file whenever a file or directory is opened, leveraging the find-file-hook
to ensure that the dir configurations are applied..dir-config.el
file name can be changed by modifying the dir-config-file-names
defcustom.To install dir-config
from MELPA:
If you haven't already done so, add MELPA repository to your Emacs configuration.
Add the following code to your Emacs init file to install dir-config
from MELPA:
(use-package dir-config :ensure t :custom (dir-config-file-names '(".dir-config.el")) (dir-config-allowed-directories '("~/src" "~/projects")) :config (dir-config-mode))
Note:
(setq dir-config-file-names '(".project-config.el" ".dir-config.el"))
will make dir-config
search for the .project-config.el
file first, and if it is not found, it will then search for the .dir-config.el
file'.(setq dir-config-verbose t)
and (setq dir-config-debug t)
to increase the verbosity of messages each time a file is loaded while dir-config-mode
is active.Assuming that the dir-config
package has been configured to allow loading configuration files from specific directories, such as ~/src
, by setting the dir-config-allowed-directories
variable:
(setq dir-config-allowed-directories '("~/src" "~/projects"))
Adding the following code to the ~/src/my_python_project/.dir-config.el
file can modify the PYTHONPATH
environment variable for Python buffers within its directory or one of its subdirectories (e.g., ~/src/my_python_project/my_python_project/file.py
). Modifying PYTHONPATH
ensures that processes executed by tools like Flycheck or Flymake have access to the Python project's modules:
;;; .dir-config.el --- Directory config -*- no-byte-compile: t; lexical-binding: t; -*- (when (or (derived-mode-p 'python-ts-mode) (derived-mode-p 'python-mode)) (let ((python-path (getenv "PYTHONPATH")) (dir (dir-config-get-dir))) ;; Update the PYTHONPATH environment variable to ensure that Flycheck, ;; Flymake, and other subprocesses can locate the Python modules. (when (and dir (or (file-exists-p (expand-file-name "setup.py" dir)) (file-exists-p (expand-file-name "pyproject.toml" dir)))) (setq-local process-environment (copy-sequence process-environment)) (setenv "PYTHONPATH" (concat dir (when python-path (concat ":" python-path)))))))
It is recommended to always begin your .dir-config.el
files with the following header:
;;; .dir-config.el --- Directory config -*- no-byte-compile: t; lexical-binding: t; -*-
The dir-config
package allows for automatic application of specific configurations based on the directory of the files being accessed, enhancing the flexibility and customization of the Emacs environment.
.dir-locals.el
By default, Emacs loads .dir-locals.el
from the current directory or its parent directories, applying project-specific settings such as indentation, compilation commands, or custom minor modes. While useful in many cases, this behavior can lead to unintended overrides.
If you prefer to disable .dir-locals.el
, you can do so by setting enable-dir-local-variables
to nil
:
(setq enable-dir-local-variables nil)
Disabling .dir-locals.el
ensures that only the .dir-config.el
files managed by this package are loaded, creating a more controlled and consistent configuration environment.
Here is the difference between with .dir-locals.el
and the .dir-config.el
files:
.dir-locals.el
(built-in):
.dir-locals.el
relies heavily on nested lists and alist structures which can quickly become difficult to read and maintain.dir-locals.el
is inherently static unless dynamic behavior is explicitly added using eval
. Here is an example of .dir-locals.el
:
((nil . ((eval . (progn
(setq-local my-variable t)
(message "Hello world"))))))
.dir-locals.el
file can be specified by modifying the dir-locals-file
variable..dir-config.el
(this package):
.dir-config.el
files are easier to maintain, as they use standard Elisp code instead of nested alists..dir-config.el
file names by adding them to the dir-config-file-names
list.Dir config files (.dir-config.el
) offer advantages for managing complex directory-specific configurations that require dynamic logic. They allow projects or directories to define their needs, providing flexibility to customize configurations to specific requirements.
For example, one of the author's use cases is to use dir-config.el
to enable Emacs features only in trusted directories while keeping these features disabled by default (for security reasons, some code linters need to be disabled by default because they evaluate code).
While this code could be included in the author's init file, he prefers to keep the init file as a general editor configuration without project-specific details. Since the author uses multiple machines for various purposes, he finds it more straightforward to let the filesystem hierarchy (e.g., a Git repository or a project directory) determine the specific settings for each directory or project using .dir-config.el
.
The dir-config
Emacs package has been written by James Cherti and is distributed under terms of the GNU General Public License version 3, or, at your choice, any later version.
Copyright (C) 2023-2025 James Cherti
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program.
Other Emacs packages by the same author:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4