Easy Regexp Generation With Emacs

Building regular expressions is quite tedious, and while Emacs has a number of tools that help build them they are generally geared towards building regexps which are Emacs Lisp specific.

Emacs Lisp regular expressions use a different syntax than you’re probably used to (those are usually PCRE used by many current/popular languages.) But it’s quite easy to convert them with a nice package called pcre2el.

Creating an optimized regexp to match a list of words

When you want to match a list of words, using a regexp, it’s fairly standard to use a pattern similar to this:


As that list of words becomes more complex, doing something like the following, to optimize the regexp becomes trickier:


Emacs can help us out here with a neat extension called regexp-opt (short for: regular expression optimiser.) It accepts a list of words, and builds an optimised regexp for them. regexp-opt is a builtin package, so you don’t need to install it.

For example, the list of words in this sentence.

  '("For" "example" "the" "list" "of" "words" "in" "this" "sentence"))

Generates the following Emacs Lisp style regular expression:


I think this is rather cool, although as it is, a bit awkward for use in most other languages. This is where the package pcre2el comes in handy, while pcre2el sounds like it converts PCRE to Emace Lisp style, which indeed it does, it also automatically works the other way and converts Emacs Lisp style regexps to pcre. pcre2el is available on MELPA, so providing you have MELPA packages available you should be able to just do:

M-x package-install

Once it’s installed, here’s what it gives us when we send our previous regexp-opt output though it:


This is the emacs lisp code:

    '("For" "example" "the" "list" "of" "words" "in" "this" "sentence")))

Turning this into an interactive function is pretty easy, let’s say you want Emacs to prompt you for the list of words…

(defun pcre-regexp-from-list-of-words (words)
  "insert a pcre regexp to match a list of words"
  (interactive "sList of words for regexp: ")
    (regexp-opt (split-string words)))))

You can just paste this into *scratch* and evaluate it (do C-x C-e) now you can run it with M-x pcre-regexp-from-list-of-words and enter a string of words, for example “this is a test” and we’d get the following regexp inserted into the buffer:


You can add the function to your .emacs.d/init.el (after you’ve done package-initialize) and bind it to a suitable key, using global-set-key, ie:

(global-set-key (kbd "C-c R") 'pcre-regexp-from-list-of-words)

I hope this helps you make regexps with Emacs, and also gives you some inspiration for Emacs hacks of your own.

Update: For more Emacs regexp goodness, have a look at Visual Regexp and Visual Regexp Steroids (which does PCRE-like regexps with lookbehinds etc.) Both of these are available via MELPA.


Remember Theme

Not a theme, but a simple mechanism to reload the last theme you were using when you quit Emacs.

I keep the same .emacs.d on all my machines, but I like to use different themes from time to time without impacting my config.

remember-theme provides a very simple method of doing this. Once installed, it will autoload at startup, check ~/.emacs-theme file it uses to remember your last theme, and then load it (after disabling any preloaded themes). When Emacs is quit, the last theme loaded will be saved, ready to be loaded up next time.

To install it, make sure you have the marmalade-repo available to the emacs package manager and then do M-x package-list-packages and find remember-theme in the list. (press i and x to begin installing.)

You can find the code on github :


Soothing Purple Haze on Marmalade

I’ve just posted a couple of my themes to Marmalade’s Elpa repository, soothe-theme and purple-haze-theme you can install them with package.el (part of Emacs24)

Find them with from M-x package-list-packages or just install them with M-x package-install


Purple Haze



Purple Haze Theme

This is yet another Emacs24 theme, it has core support for font-lock and some basic editor elements.

At the moment, it lacks specific support for Org-mode, Helm, Gnus, Ido etc. etc. and it’s not terminal friendly. (that stuff will get added, but this is quick and dirty.)

If that’s good enough for you, grab it from github at

No prizes for guessing where the name came from.

Clone from github

…and of course the obligatory screenshot


Soothe Theme Updated

Just a quick post about Soothe, I’ve updated it to run on the terminal, and adjust it’s colors accordingly.

Here’s a screengrab of it running in the terminal…

Soothe is also hosted on github as a standalone repo here, so it’s easier for me to add it to elpa/milkbox etc.

Also I’ve made a iTerm2 colortheme using the Soothe palette, which you can get over here


Soothe Theme

This is a theme I use almost exclusively now, it’s not Terminal friendly, and I should fix that, but if you’re using on OS X, or in another full color window manager it’ll serve you well.

The block-highlights may put you off, I find them very helpful for some reason I’m unable to clarify for you in any technical / psychological / color-theory sense, but try it out anyway.

Feel free to fork modify, whatever, it’s MIT Licensed.

Download: soothe-theme.el


Copyright © 2014 Jason Milkins - Design based on Greyshade by : Shashank Mehta