Emacs: Find and Replace Commands

By Xah Lee. Date: . Last updated: .

Find Replace Commands

Here are the most useful find replace commands for current file.

Alt+x query-replaceAlt+%
Interactive find replace on text selection, or cursor position to end of buffer.
Alt+x query-replace-regexpCtrl+Alt+%
Interactive find replace with regex, on text selection, or cusor point to end of buffer. [see Emacs: Regex Tutorial]
Alt+x dired-do-query-replace-regexp (In dired, Q)
Interactive find replace on marked files in dired. See: Emacs: Find Replace Text in Directory.

Press Ctrl+g to cancel. (call undo to undo existing replacement.)

How to Insert Literal Tab, Newline

Default Case Sensitivity: Smart

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 Alt+x set-variable.

Turn Off Smart Case Sensitivity

Alt+x toggle-case-fold-search or menu 〖Options ▸ Case-Insensitive Search〗.

You can also give it a key such as F8. [see Emacs: How to Define Keys] Or you can give it a alias such as “tc” [see Emacs: Use Alias for Fast M-x]

Force Case Change on Matched Text in Regex Match

If you are doing a regex search, and you want to force the replacement to upper case or lower case, in the replace prompt, give \,(upcase \1) or \,(downcase \1).

For example, suppose you have this text:

<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]\).

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.

(info "(emacs) Search")

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


Emacs Tutorial

Quick Start

Manage Windows

File

Buffer

Find Replace

Copy/Paste

Unicode

Whitespace

Rectangle Edit

Line Wrap

Shell

View Doc

Working with Brackets

Power Editing

Org Mode

HTML

Emacs Efficiency

Misc