Idiomdrottning’s homepage

Unix filters on region

I write and use a lot of Emacs Lisp but what I do even more, maybe two or three times more often, is to just make Unix filters to run on the region (with a prefix if I wanna replace the text).

Vi users can get in on the fun, too, with their ! command.

poetry-md

Here’s an example I wrote just this morning. This one is for for zshbrev but you can write filters in any language that can read and write stdin.

The example is for when you’re writing markdown text and you want to add two spaces to make newlines.

(make-sloppy-tree-accessor scadr)

(define (poetry-md)
  (pair-fold
   (fn
    (if (any empty? (list (car x) (scadr x)))
	(print (car x))
	(print (car x) "  ")))
   (void)
   (read-lines)))

So once I’ve compiled it with zshbrev, I can then from Emacs just C-u M-| poetry-md RET (in other words, that’s meta pipe with a prefix, or ug|poetry-md RET when using god-mode) when I have a poem or song-lyric in the region.

Vi users can :address!poetry-md, for example :1,5! poetry-md if the poem is on the first five lines.