9

I'm converting my dotfiles to org-babel files because I really enjoyed rewriting my Emacs configuration in it, and I have a function for the init.org that automatically tangles it whenever I save. However, I haven't been successful in making the .org files in say ~/.dotfiles to do the same automatically. I have this function currently:

(defun my/tangle-dotfiles ()
  "If the current file is in '~/.dotfiles', the code blocks are tangled"
  (when (equal (buffer-file-name)
               (expand-file-name "~/.dotfiles/tmux.org"))
    (org-babel-tangle)
    (message "Dotfile tangled")))

(add-hook 'after-save-hook #'my/tangle-dotfiles)

However, this only works for the specific file I name, not for any org file. Using *.org doesn't work either, nor have I figured out how to extract the directory name from both the buffer and directory and do it that way.

I also asked on the #emacs on Freenode but nobody answered me there. Any help or pointer on where to begin would be highly appreciated.

1 Answer 1

9

I figured out how to do it:

(defun my/tangle-dotfiles ()
  "If the current file is in '~/.dotfiles', the code blocks are tangled"
  (when (equal (file-name-directory (directory-file-name buffer-file-name))
               (concat (getenv "HOME") "/.dotfiles/"))
    (org-babel-tangle)
    (message "%s tangled" buffer-file-name)))

(add-hook 'after-save-hook #'my/tangle-dotfiles)

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.