(or emacs irrelevant)

Blogging about blogging

In Emacs, of course. As I mentioned before, this blog is run using Jekyll and a fork of the lanyon theme.

It started out pretty convenient: thanks to jekyll serve I would see the live updates at http://localhost:4000/ as I was editing the markdown. But after a few posts, I've experienced a drastic decrease in refresh time: it went from under a second to 15 seconds. Below, I'll show how I've managed to speed it back up.

No posts - no problem

Thanks to (require 'dired-x) I can jump from the current post to the posts directory with C-x C-j. It even puts the point on the current file. Now:

  • m to mark to current file
  • t to invert the mark, making the current file unmarked and all others marked
  • D to delete all marked files. No need to worry, since they are all version-controlled (except the original one, which isn't being deleted), dired even asks you for confirmation, so I hit y.

Now, that there is only one post in the whole blog, jekyll serve is much faster at refreshing.

Even faster jekyll serve

Which brings me to the next point. Since I'm calling it so much, might as well wrap it in some Elisp. Elisp is like frosting - it makes everything better:

(defun jekyll-serve ()
  (interactive)
  (let* ((default-directory
          (if (string-match "_posts/$" default-directory)
              (directory-parent default-directory)
            default-directory))
         (buffer (if (get-buffer "*jekyll*")
                     (switch-to-buffer "*jekyll*")
                   (ansi-term "/bin/bash" "jekyll")))
         (proc (get-buffer-process buffer)))
    (term-send-string proc "jekyll serve\n")
    (sit-for 3)
    (browse-url "localhost:4000")))

This function, when called from the current post or the current blog, will:

  • open a new ansi-term called *jekyll*
  • issue a jekyll serve command to it
  • wait for 3 seconds for Jekyll to start
  • open localhost:4000 in Firefox

One post isn't a blog

Here's how to quickly bring back the deleted posts.

  • Fire up magit; I have magit-status bound to μm.
  • Stage the current post with s (magit-stage-item).
  • Start the commit with C (magit-commit-add-log). I'm using my own modification of magit-commit-add-log that arranges the whitespace in a nice way, when aimed at a file instead of a hunk.
  • I get _posts/2015-02-01-blogging-about-blogging.md: string auto-generated by C.
  • I just amend it to look like _posts/2015-02-01-blogging-about-blogging.md: add and finalize the commit with C-c C-c (git-commit-commit).
  • Now I have around 40 files marked as deleted, but not staged. I create a stash with zz and name it foo or something. Then delete the stash with magit-discard-item, which I like to bind to d to be similar to dired. Now it's as if these files were never deleted.

Whoa, now that I look at it, it's a lot of steps. But somehow dired and magit are very similar to Super Mario Bros: it takes long to explain what you're doing, but as you play it, it's very simple and natural.