Emacs carnival: rec mode, an all-text database system
This month's Emacs carnival is on underappreciated built-in features in Emacs.
rec mode
One of the key features of Emacs is that everything is just text.1 An example of this is rec mode.2 Rec mode implements a simple yet flexible database system using just text files. Accompanying rec mode, the Emacs code, is the package that includes the tools for manipulating files containing rec databases. On Debian, the package is recutils.
Entries in a rec file are defined by fields and their content. A single file will contain all the records in your database. Each record is separated from other records by a blank line. Beyond this, there is no imposed structure. For instance, each record may have different fields defined.
A small example
I use rec mode to keep track of my books. Although I do use calibre to manage my ebooks, in particular, I do not like interacting with calibre as it is mouse based.3 I have a separate rec database which tracks which books I have acquired, am reading, have finished, or wish to buy. A couple of typical entries look like this:
title: Safe Enough
author: Lee Child
status: finished
Date: 2024-08-29
# collection of short stories, none about Reacher
title: All systems red
author: Martha Wells
series: murderbot
number: 1
status: finished
Date: 2017-05-02
notes: library
Note that these two entries, although both referring to books, have different fields used.
As I'm writing about Emacs, the obvious question is: does org mode support rec files? The answer, as with all things Emacs, is "of course it does". With a src block:
#+begin_src rec :data my_books.rec author = 'Martha Wells' #+end_src
the outcome on evaluating is the following table:
| title | author | series | number | status | Date | notes |
|---|---|---|---|---|---|---|
| All systems red | Martha Wells | murderbot | 1 | finished | 2017-05-02 | library |
| Artificial condition | Martha Wells | murderbot | 2 | finished | 2018-05-08 | library |
| Rogue protocol | Martha Wells | murderbot | 3 | finished | 2018-08-07 | library |
| Exit strategy | Martha Wells | murderbot | 4 | finished | 2018-10-02 | library |
| Home | Martha Wells | murderbot | 4.x | finished | 2021-10-01 | library, short story |
| Fugitive telemetry | Martha Wells | murderbot | 4.5 | finished | 2018-10-02 | library |
| Network effect | Martha Wells | murderbot | 5 | finished | 2020-05-02 | library |
| System collapse | Martha Wells | murderbot | 6 | finished | 2026-05-06 | library |
The best thing about rec mode is, to repeat myself, that it's all text. There is no danger of corrupting a database or, at least, if somehow the database becomes corrupt, it should be easy to fix (using Emacs, of course). Being all text means that version control is trivially supported, which brings me to the final comments below.
Honorable mention
When writing this blog post, I spent some time thinking about which one feature was most underappreciated and I decided on rec mode, as you have read above. However, closely behind was vc mode. Many people talk about some of the killer features of Emacs, mentioning org mode and magit. These are both excellent packages and I use them both myself. However, I only use magit every now and again, relying on vc mode instead for day to day version control. Almost everything I need to do in terms of version control is supported by vc mode and, more importantly for me, vc mode is agnostic with respect to the actual version control system used. I do use git but I also use mercurial and src. The latter is particularly nice for single file projects.
Blog navigation
| Previous post | Blog | Next post |
|---|---|---|
| Emacs carnival: Mistakes and misconceptions | Index | None (yet!) |
You can find me on Mastodon or you can email me should you wish to comment on this entry or the whole blog.
References
Footnotes:
Well, actually, it's more than that: everything is a buffer that contains text.
There are Emacs packages for working with calibre, such as calibre.el