Downcase with replace-regexp

April 6, 2009

Recently I’ve been working with CSS files and wanted to replace color definitions with downcased equivalents, i.e. #FFFFFF; would become #ffffff;

To do that I used pretty well known feature of Emacs, namely use of Lisp expressions in the replacement string of replace-regexp function.

Commands entered in minibuffer:

M-x replace-regexp RET \(#.*\)  RET \,(downcase \1) RET

The only problem with that code is that if you have variable case-fold-search in Emacs set to true (which is the case in my Emacs by default) replace-regexp will report about successful replace but string won’t be downcased anyway. To quickly switch off case-fold-search I used M-: (setq case-fold-search nil) and after that downcasing worked as expected.