03 Oct 2021

# My Zettelkustom (with Emacs, of course)

## Zettelwhat?

A couple of years ago, I was trying to improve my note taking abilities and did some research. I discovered the Zettelkasten method and read the book How to take smart notes by Sönke Ahrens which describes this approach invented by Niklas Luhmann, a German sociologist.

For a quick introduction to the method, I find this web site very well done. If you get interested in Zettelkasten, before jumping to the last shiny app or Emacs package, I think it's better to read Ahrens' book.

Anyway, in a few words, in Zettelkasten, you create notes with small bits of knowledge that are meant to be self contained. These notes may contain links to other notes with related content. The idea is that the knowledge is not organized hierarchically, but in a graph where notes point to other notes.

The Zettelkasten is a living thing where notes are regularly added and most importantly, the notes are frequently read and improved, either by reformulating the content, adding links to other notes, etc.

The Zettelkasten is meant to be personal, edited by a single person. It's like a second brain.

Niklas Luhmann did everything by hand and his Zettelkasten was made of paper cards. He had to invent a clever indexing method and used special structure notes to create tables of contents for different subjects. It is a pleasure to browse the original Zettels.

In order to implement a digital Zettelkasten, we only need a note taking application with the ability to create links between notes. A nice bonus is adding tags to the notes to simplify search and generation of sets of related notes.

There are lots of applications on the proprietary software market that support the creation and management of a Zettelkasten. There are also free software counterparts.

Emacs offers several alternatives in terms of packages:

There are probably others that I am not aware of. Org-roam seems to be the most popular one.

After reading Ahrens' book, I decided that I wanted to try the approach. I did not think want to choose one of the available Emacs packages for several reasons. The first one is that I did not understand why I needed anything else than plain org-mode. The second one was that I did not want to commit to any particular implementation before understanding how and if the approach would be useful for me.

## How I do Zettelkasten

I have a big org-mode file called zettels.org with to top-level headings, one for structure notes and another one for standard zettels. Each note is a second level heading with a title, possibly some org-mode tags, a property drawer and the note content.

The property drawer contains at least the DATE_CREATED property with an inactive org-mode timestamp for the day and time when the note was created.

The structure notes are created by hand. That means that I create a heading, write the note and add the DATE_CREATED property. I do not create many structure notes, so a manual workflow is OK.

For the standard zettels, I use org-capture. The capture template automatically inserts the DATE_CREATED property, but also a FROM property with an org-mode link to the place I was in Emacs when I run org-capture. This link can therefore point to another Zettel (for which an org-id will be created), any org-mode heading if I am in an org-mode file, but this can also be an e-mail in Gnus, a pdf document, an EPUB file, a web page, etc. This is possible because I do everything in Emacs. Storing where I was when I created the note gives interesting context.

I have 2 org-capture templates for Zettelkasten, one which does what I described above, and another one which is used for quotes. The latter will copy the marked region in the current buffer into a quote org-mode block.

So a typical captured zettel may look like this:

** The title of the note :tag1:tag2:
:PROPERTIES:
:DATE_CREATED: [2021-08-06 Fri 22:43]
:FROM: [[nov:/home/garjola/Calibre Library/abook.epub::26:7069][EPUB file at /home/garjola/Calibre Library/abook.epub]]
:END:

My ideas on the subject. Etc.

#+begin_quote
Some text that was marked in the EPUB I was reading.
#+end_quote



This way, while I am reading interesting things, I can capture an idea with all its context. I will of course come later to this note to improve it.

This is done in my regular gardening sessions. During these sessions, I browse the Zettelkasten, read notes, add tags and links to other notes, rewrite things, etc.

For this tasks, I use a couple of functions. The first one jumps to a random Zettel, so I am sure that I regularly explore forgotten parts of the Zettelkasten. The second one finds back-links, that is notes having links that point to the current note. This is useful for a bi-directional browsing of the content.

## My custom Zettelkasten setup

### Capture templates for Zettelkasten

The first component of the setup is the capture templates. They look like this:

(setq org-capture-templates
(append org-capture-templates
(quote (("z" "Zettelkasten")
("zz" "Zettel" entry
(function my/zettel-template)
:empty-lines 1)
("zq" "Quote" entry
(function my/zettel-quote-template)
:empty-lines 1)))))


There are 2 templates, one for notes without quotes (called with zz) and another for notes where I want to insert the marked region in the current buffer as a quote (called with zq). Both templates insert the note in the zettels.org file under the Zettels heading. Instead of writing the template in this code, I prefer using a function to generate it. I find this more readable.

The 2 functions are here:

(defun my/zettel-template ()
"* %?\n:PROPERTIES:\n:DATE_CREATED: %U\n:FROM: %a\n:END:\n%i\n")
(defun my/zettel-quote-template ()
"* %?\n:PROPERTIES:\n:DATE_CREATED: %U\n:FROM: %a\n:END:\n#+begin_quote\n%i\n#+end_quote")


They are straightforward. The cursor is placed in the heading (with the %? org-expansion) so I can write the title. The property drawer will contain the time stamp and a link to the place Emacs was when org-capture was called. In the case of the quote, the marked region is copied inside the org-mode quote block.

And that's it!

Back-links can be tricky. The package org-sidebar provides a function for that. But for some reason that I don't remember, I didn't like the way it worked (or more probably, my incompetence did not allow me to make it work). So I searched a bit and found a bit of elisp that does what I need.

(require 'org-ql)
(require 'org-ql-view)

(interactive)
(let* ((id (org-entry-get (point) "ID"))
(custom-id (org-entry-get (point) "CUSTOM_ID"))
(query (cond ((and id custom-id)
;; This will be slow because it isn't optimized to a single regexp.  :(
(warn "Entry has both ID and CUSTOM_ID set; query will be slow")
(or (link :target ,(concat "id:" id))
((or id custom-id)
(link :target ,(concat "id:" (or id custom-id))))
(t (error "Entry has no ID nor CUSTOM_ID property"))))
(org-agenda-tag-filter nil))
(org-ql-search (quote ("~/org/zettels.org")) query :title title)))


It uses the wonderful org-ql package to search all org headings with a link matching the org-id of the current note. The search is limited to my zettels.org file.

The last bit I needed for serendipity is jumping to a random note. For this, I use the org-randomnote package. This package uses the org-randomnote-candidates variable to store the list of files that will be searched for random headings. It is initialized to the value of org-agenda-files. I just wrote a couple of functions to temporary change the list of candidate files so that I can limit the random jump to my zettels file:

(defun my/random-note (candidates)
"Jump to a random org heading in one of the CANDIDATES files"
(let ((old-randomnote-candidates org-randomnote-candidates))
(setq org-randomnote-candidates candidates)
(org-randomnote "zettels")
(setq org-randomnote-candidates old-randomnote-candidates)))

(defun my/random-zettel ()
(interactive)
(my/random-note '("~/org/zettels.org")))


## Moving to org-roam (or maybe not)

My first Zettel dates back to December 30 2019. I've been using this system since then and I am very happy with it. A year ago, I started seeing a big buzz about org-roam and I looked into it. It seemed very nice, with functionalities that I don't have in my system and I started wondering whether I should use it. Since at that time I understood that there were some breaking changes planned for version 2, I decided to wait for org-roam v2 to be released and re-evaluate the situation.

In the meantime, my Zettelkasten has continued growing. Today, it contains 1345 zettels. After org-roam v2 was released, David Wilson at System Crafters did a series of videos showing the power of org-roam. As always, David's videos are of great help to get started and he has the rare ability of guiding the interested crafter so that he or she is able to customize thing as wanted.

I therefore installed org-roam and even wrote the code to migrate my Zettelkustom to org-roam. I was an interesting exercise that allowed me increase my knowledge of Emacs Lisp and get familiar with the org-element API.

However, I am not sure that I want to do the migration. Although org-roam is very rich and has a large community of users, I have the feeling that it does to many things that I don't need. I guess that, if I was starting with org-mode, org-roam would be the good choice, but I have been using org-mode for personal organization (with GTD), note taking, writing scientific papers and reports, doing literate programming, and may more things.

And I have my personal preferences and habits that I will likely not change. For instance, for me, mixing project management and Zettelkasten does not make sense. I also don't need a special integration between Zettelkasten and my bibliographic database (org-ref and ivy-bibtex are all I need).

Therefore, by now, I'll stick to my 2 capture templates and 2 simple functions that I understand well.

I any case, all this shows the power of org-mode and its ecosystem.