I have some text that looks like this:


I would like to get output where are of the text inside the double quotes is lowercased, like this:


So I've tried to use M-x replace-regexp with the regexp being \(".*"\) and the replace as \,(downcase \1). Which is how it is explained in this question. The selection does what I expect, but the replacement doesn't.

What I get is:


The groups that are in ALL CAPS aren't downcased. I've tried using downcase-region and downcase-word but those both fail with errors such as Wrong type argument: integerp, #("\"CG_DOB\"" 0 8 (fontified t)). I don't understand why downcase doesn't work with a group that is all caps.

How do I get this replace-regexp to downcase all groups?

I'm using GNU Emacs 24.3.1. The problem persists even if I start emacs with the -Q (don't use .emacs) flag. I've tried it in both sql-mode and text-mode with the same result.

I've tried this on the following two versions of emacs with the same results:

GNU Emacs 24.5.1 (x86_64-apple-darwin14.4.0) of 2015-07-20

GNU Emacs 24.3.1 (x86_64-pc-linux-gnu) of 2014-03-07 on lamiak, modified by Debian

  • It works for me. Does it work for you if you start Emacs using emacs -Q (no init file)? If it does, recursively bisect your init file to find the problem. If it does not, try to give more details (baby steps, what mode, what Emacs version, etc.). – Drew Aug 23 '16 at 18:36
  • @Drew I've tried it with -Q and that had no effect. I've also tried it in text-mode instead of sql-mode and that also made no difference. – Gregory Arenius Aug 23 '16 at 19:37
  • What can I say? Are there more details to describe? I followed your recipe exactly, with emacs -Q (on MS Windows), with different Emacs versions, and it works fine for me. (BTW, you don't even say what the replacement does; you just say that it does not do what you expect. What is the result of replacing? No change?) – Drew Aug 23 '16 at 19:42
  • @Drew The replacement downcases all strings that are of mixed case, such as "CFName" but results in no change for strings that are entirely in caps such as "CDOB". – Gregory Arenius Aug 23 '16 at 19:47
  • Interesting. And what happens when you use M-x downcase-word or M-x downcase-region or M-: (downcase "CDOB")? IOW, is the problem with the use of replace-regexp of with downcasing (on your platform etc.)? – Drew Aug 23 '16 at 19:52

From the docstring of query-replace-regexp:

Matching is independent of case if ‘case-fold-search’ is non-nil and
REGEXP has no uppercase letters.  Replacement transfers the case
pattern of the old text to the new text, if ‘case-replace’ and
‘case-fold-search’ are non-nil and REGEXP has no uppercase letters.
(Transferring the case pattern means that if the old text matched is
all caps, or capitalized, then its replacement is upcased or

So if you M-x set-variable RET case-replace RET nil RET first, then you will get the results you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.