It's a very simple task, really. But all my google results come up with complex solutions. I must be blind or going about this wrong. Anyway...

Here's what I want:

I want to split up my emacs configuration in the following structure:


I want init.el to be as minimal as possible, just something that says "load all .el files in custom1/ and custom2/, recursively". Whenever I put a new file in any of those two directories I want it to automatically be loaded when emacs is started.

It seems simple and easy to me. But I fail at finding a simple solution.


Here's an answer based on the comment from @lawlist:

The file ~/.emacs.d/load-directory.el:

(defun load-directory (directory)
  "Load recursively all `.el' files in DIRECTORY."
  (dolist (element (directory-files-and-attributes directory nil nil nil))
    (let* ((path (car element))
           (fullpath (concat directory "/" path))
           (isdir (car (cdr element)))
           (ignore-dir (or (string= path ".") (string= path ".."))))
       ((and (eq isdir t) (not ignore-dir))
        (load-directory fullpath))
       ((and (eq isdir nil) (string= (substring path -3) ".el"))
        (load (file-name-sans-extension fullpath)))))))

Then in ~/.emacs.d/init.el:

(load "~/.emacs.d/load-directory")
(load-directory "~/.emacs.d/custom1")
(load-directory "~/.emacs.d/custom2")
  • I think load-directory could be simplified to (require 'find-lisp) (mapcar (lambda (fn) (load (file-name-sans-extension fn))) (find-lisp-find-files directory "\\.el\\'")) – YoungFrog Nov 27 '15 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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