Take the 2-minute tour ×

Every once in a while I want to replace all instances of values like:




i.e. do a regular expression replace of all things inside angle brackets with its lowercase equivalent.

Anyone got a nice snippet of Lisp that does this? It's safe to assume that we're dealing with just ASCII values. Bonus points for anything that is generic enough to take a full regular expression, and doesn't just handle the angle brackets example. Even more bonus points to an answer which just uses M-x query-replace-regexp.



share|improve this question

1 Answer 1

up vote 50 down vote accepted

Try M-x query-replace-regexp with "<\([^>]+\)>" as the search string and "<\,(downcase \1)>" as the replacement.

This should work for Emacs 22 and later, see this Steve Yegge blog post for more details on how Lisp expressions can be used in the replacement string.

For earlier versions of Emacs you could try something like this:

(defun tags-to-lower-case ()
    (goto-char (point-min))
    (while (re-search-forward "<[^>]+>" nil t)
      (replace-match (downcase (match-string 0)) t))))
share|improve this answer
That's cool! I wasn't aware of \,() in Emacs regular expressions. –  emk Mar 24 '09 at 11:40
This gets me the error "Invalid use of `\' in replacement text" –  Dominic Rodger Mar 24 '09 at 11:46
Regexp should be "<\([^>]+\)>" and the replacement doesn't work as expected if the search string matches tag in all caps. –  Eugene Morozov Mar 24 '09 at 12:02
I've edited the answer so that the backslashes in the search expression are now visible. –  Luke Girvin Mar 24 '09 at 12:21
Emacs can handle .*? as a non-greedy match. –  ashawley Mar 24 '09 at 18:52

Your Answer


