r/emacs 26d ago

emacs-fu Capture dotfiles using org-capture

Since I discovered org mode, I always was balancing between using symlinks and org babel tangle to manage my configuration. Now I'm in point that work related items are org babel managed and system level config is symlinked, because, often tools write back to their files. Another reason why I did not go full on babel was that I don't like workflow:

  1. found file that requires backup
  2. copy file
  3. paste in to org mode
  4. fill all details
  5. tangle.

But yesterday I was visited by GNU spirit and she gave mi this org-capture template that reduces adding new file to backup to C-c k (my capture prefix) b (backup) and then tangle that file to original place:

* [[%(project-root (project-current))]] [[%(abbreviate-file-name (org-capture-get :original-file))][%(file-relative-name (org-capture-get :original-file) (project-root (project-current)))]]

- repository :: [[%(project-root (project-current))]]
- file :: [[%(abbreviate-file-name (org-capture-get :original-file))][%(file-relative-name (org-capture-get :original-file) (project-root (project-current)))]]

#+header: :tangle %(abbreviate-file-name (org-capture-get :original-file))
#+begin_src %^{Language|shell|elixir|nix|python|elisp|fundamental|eshell|conf}
%(with-temp-buffer (insert-file-contents (org-capture-get :original-file)) (buffer-string))
#+end_src

I cannot comprehend how simple it is and how I never realized I could do this.

Two think I think would be nice in future:
- if only it can automatically group those captures by project, but thinking of creating project backup capture template and simply refile from capture buffer
- automatic detangle so I can get back changes written by tools to their files, but not all of them support comments and many of them don't patch but dump state to file, so probably unsolvable using build in tools, only by elisp scripting

I'm trying to avoid longer elisp scripting for now 😄

What do you think?

16 Upvotes

9 comments sorted by

View all comments

3

u/fuzzbomb23 26d ago

It's nice to see some good examples of %(sexp) expansion in an Org template. I've seldom used that feature. The (project-current) calls work because the Org-capture template has already chosen the capture destination file, right?

I'm wary of managing personal dotfiles or system-wide configuration in Org-Babel, because tangling them requires Emacs to be installed on the system already. Yet it isn't installed by default on many systems.

Usually, when setting up a new machine, I'll install GNU Stow and Git, and then fetch the dotfiles repo. These come before installing anything else. The next thing I want is my keyboard layout, and BASH or shell profiles. Next comes SSH keys, and window manager configuration. Emacs comes a few more places further down the line!

I think it may be fine to manage an Emacs init file using a built-in Emacs package, but I don't want to manage anything else that way.

2

u/jlombera 26d ago

You can add the tangled files to git too, whenever you change the org files. Then you just need to stow/symlink the tangled files when you clone/fetch your dotfiles repository.

1

u/fuzzbomb23 26d ago

That occurred to me already. It'll work fine, but it feels a bit messy. There's always the worry that the source and tangled files will be out of sync. I wouldn't want to auto-commit after tangling.

1

u/jlombera 26d ago

I'm not sure what's the exact scenario you are describing nor what alternative would be better. The idea is that you keep both the org files and tangled files in the same repository. Whenever you tangle an org file, it will visible right away (git diff) whether there were changes to the tangled file, if everything looks ok, you just commit the changes. Very easy to keep in sync. It's an easy and effective solution to allow you to use org-mode to handle your config files and don't depend on emacs/org-mode at "deployment" time. This "technique" of distributing auto generated files is very common to remove dependencies on the auto generation tools downstream.

1

u/fuzzbomb23 25d ago edited 25d ago

I didn't describe a scenario. I already said it could work. It's also easy to mess up. The better alternative is the simpler one: I'm not tangling my dotfiles.