Use Org for Your Dot Emacs + Tangling Strategies

Create an init.el and an init.org

Add this to init.el

;; -*- lexical-binding: t; -*-
(load-file "~/.emacs.d/name-of-your-file.el")

Or this:

;; -*- lexical-binding: t; -*-
(require 'org)
(let ((org-file "~/.emacs.d/init.org")
      (el-file "~/.emacs.d/Raoul.el"))
  (when (or (not (file-exists-p el-file))
            (file-newer-than-file-p org-file el-file))
    (org-babel-tangle-file org-file)))

(load-file "~/.emacs.d/Raoul.el")
  • With the above code tangling is automatic.
  • This obviates the need to do a manually invoke org-babel-tangle

Add this information to init.org

#+TITLE: My Name Emacs Configuration
#+AUTHOR: My Name
#+EMAIL: myname@internet.com
#+OPTIONS: toc:2 num:nil
#+PROPERTY: header-args:emacs-lisp :tangle Raoul.el
#+STARTUP: overview

Use C-c C-v t to manually tangle current file

Use :tangle no

  • Note: This option goes in the code block header
  • It can be used with experimental code, examples, temporary configs that you want to test but not deploy
  • Can still be executed with C-c C-c in Org mode

Use descriptive filenames (e.g., :tangle ~/.emacs.d/hydras.el)

This can be used in conjunction with a file level property like

#+PROPERTY: header-args:emacs-lisp :tangle Raoul.el

Use :mkdirp yes to auto-create directories when tangling

  • Useful for organizing tangled files
#+begin_src emacs-lisp :tangle ~/.emacs.d/modules/ui.el :mkdirp yes
  (setq inhibit-startup-screen t)

Use :noweb yes to reference other code blocks with

* Helper Functions
#+name: my-helpers
#+begin_src emacs-lisp :tangle no
(defun my-custom-function ()
  "Does something useful"
  (message "Hello world"))
#+end_src

* Main Configuration
#+begin_src emacs-lisp :tangle ~/.emacs.d/init.el :noweb yes
;; Load helpers first
<<my-helpers>>
#+end_src

Note: :noweb yes references the #+name: property, not the Org heading.

Add :comments org to preserve Org headings as comments in tangled files

Or add it globally:

#+PROPERTY: header-args:emacs-lisp :tangle Raoul.el :comments org

Return to Home