Configure Everything with Org Babel

Org Babel

Execution: C-c C-c (output, value results)

Org Babel Documentation Org Babel: Languages

42
42
(org-babel-do-load-languages
  'org-babel-load-languages
  '((emacs-lisp . t)
    (python . t)))

(setq org-confirm-babel-evaluate nil)

Structure Templates

;; This is needed as of Org 9.2
(require 'org-tempo)

(add-to-list 'org-structure-template-alist '("sh" . "src shell"))
(add-to-list 'org-structure-template-alist '("el" . "src emacs-lisp"))
(add-to-list 'org-structure-template-alist '("py" . "src python"))

Setting Up the Config

Apply the same configuration to every block:

Tangling Output

(org-babel-tangle)
(org-babel-tangle-file "~/Projects/Code/emacs-from-scratch/Emacs.org")

Configuring Other Apps

(push '("conf-unix" . conf-unix) org-src-lang-modes)
value=42

To create output directories automatically, add :mkdirp yes

Noweb Blocks

Enables you to pass variables into a source block!

55

Add :noweb yes!

value=<<the-value>>

Tips

  • Automatically tangle on save
  • Should I also commit tangle output?
;; Automatically tangle our Emacs.org config file when we save it
(defun efs/org-babel-tangle-config ()
  (when (string-equal (buffer-file-name)
                      (expand-file-name "~/Projects/Code/emacs-from-scratch/Emacs.org"))
    ;; Dynamic scoping to the rescue
    (let ((org-confirm-babel-evaluate nil))
      (org-babel-tangle)))

(add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook #'efs/org-babel-tangle-config)))
Subscribe to the System Crafters Newsletter!
Stay up to date with the latest System Crafters news and updates! Read the Newsletter page for more information.
Name (optional)
Email Address