This page shows you how to use emacs to do find/replace, and tells you how to do case-sensitive or case-insensitive match or replacement, and how to force captured regex text pattern into upper or lower case.
Here are the emacs find/replace commands. These are also under the menu 〖Edit ▸ Replace〗.
| Command Name | Key | Target | Purpose |
|---|---|---|---|
query-replace | 【Alt+%】 | active region, or cursor point to end | interactive find/replace |
query-replace-regexp | 【Ctrl+Alt+%】 | active region, or cusor point to end | interactive find/replace with regex pattern |
dired-do-query-replace-regexp | In dired, 【Q】. | marked files in dired | interactive find/replace with regex pattern on multiple files |
For example, call query-replace, then type your search string, then type your replacement string.
When a query command asks you for confirmation, press y to do the replacement, press n to skip, press ! to do all remaining replacements without asking, press q to exit.
For detail on using dired-do-query-replace-regexp, see:
Interactively Find/Replace String Patterns on Multiple Files.
Emacs also have commands replace-string and replace-regexp. They are the non-interactive versions of query-replace and query-replace-regexp. They do all replacements in one-shot without asking confirmation for each replacement.
In practice, these are not much useful, because you can just call the interactive version and press ! to complete in one shot.
By default: If your search string contains a capital letter, search is automatically case-sensitive, otherwise it's not case-sensitive.
By default, the case of the replaced text is smartly dependent on the matched text. For example, suppose your search string is here, and your replacement string is dragon. Emacs will find any of {here, Here, HERE}. Now, when emacs found here, the replacement will be dragon, when emacs found Here, the replacement will be Dragon, when emacs found HERE, the replacement will be DRAGON.
If you want the letter case of your replacement string be exactly as you have it, you need to set the variable case-replace to nil. You can do so by calling set-variable.
If you want the letter-case in both find & replacement string be exactly you typed, then call toggle-case-fold-search or menu 〖Options ▸ Case-Insensitive Search〗.
I use this command often. So i give it a alias. This way, i just type “tc”. 〔☛ Emacs: Defining Alias to Increase Productivity〕
(defalias 'tc 'toggle-case-fold-search)
If you are doing a regex search, and you want to force the replacement to upper case or lower case, you can use \,(upcase \1) or \,(downcase \1).
For example, suppose in your HTML code you have:
<p>once upon a time …</p> <P>There is a dragon who lived in …</P> <p>princess Tana is still waiting …</p>
Suppose you want all paragraphs to start with a capital letter. So, you use a pattern that catches the first letter after <p>, like this <p>\([a-z]\). By default, emacs will match both <P>… and <p>….
To make your captured pattern upper case, give your replacement string this
expression: <p>\,(upcase \1). The \, tells emacs that what follows should be a lisp expression. The (upcase \1) is a lisp expression. The upcase is a lisp function and the \1 means the 1st captured string in your regex pattern.
For a more complex example in using the \, in replacement, see: Regex Replace with a Function in Emacs Lisp.