Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you tell Emacs to leave the case alone in a search and replace string?

I'm trying to perform a regex search and replace in Emacs (using M-x query-replace-regexp), but the usually helpful smart case is getting in the way. My source is:

One
Two
Three

And I want to replace each line with something like <item name="One"/> instead. Unfortunately the capital at the start of each line is being misinterpreted, and I'm getting <Item> with an uppercase that I don't want.

I can find examples about how to make the search case sensitive, and how to keep the \1 lowercase in the replacement string, but nothing about how to keep the entire replacement string case-unmodified.

like image 809
Malvineous Avatar asked Aug 24 '11 04:08

Malvineous


1 Answers

Try adding this to your .emacs:

(setq case-replace nil)

C-h v case-replace RET:

Documentation: Non-nil means `query-replace' should preserve case in replacements.

And a link to the manual for Replace Commands and Case details all the interactions with case and the appropriate variables.

Or you could define a new command like:

(defun query-replace-no-case ()
   (interactive)
   (let ((case-replace nil))
       (call-interactively 'query-replace))))

And, if you were coding this up in a function, and only wanted to set the variable temporarily, you'd do something like:

(let ((case-replace nil))
   (while (search-forward ...)
       (replace-match ...)))
like image 139
Trey Jackson Avatar answered Sep 20 '22 14:09

Trey Jackson