Temporary Obsessions       Archives

Emacs: Default content for a blog post

I wanted to configure Emacs to automatically insert a template for particular kinds of files, but only in certain directories. All of the machinery for this exists, but it doesn't quite seem to be all connected together.

The starting point is auto-insert, which provides a simple template mechanism to insert text into a file when it is first created. The template language can also execute Lisp expressions, making it possible to insert dynamic data as well.

Here's the basic setup for an org-mode template for a blog post using Hugo (which is why we have to fix up the timezone in the date string):

(define-auto-insert 'org-mode
  '("Blog post header"
    "#+TITLE: " - "\n"
    "#+DATE: "
    (format-time-string "%FT%T%z")
    ;; Fix the timezone format for Hugo
    -2
    ":00"
    "\n\n"))

This assumes that auto-insert-mode is enabled, and that auto-insert-query is configured to ask for confirmation, since I don't want to do this in all Org files.

I'll add the per-directory part later.