Questions tagged [query-replace]

query-replace is a built-in Emacs command for interactively replacing one string with another in a buffer. query-replace-regexp does the same thing, but with support for matching regular expressions. Use this tag for both commands.

By default, query-replace is bound to M-%. From the manual:

This command finds occurrences of ‘foo’ one by one, displays each occurrence and asks you whether to replace it.

query-replace-regexp is bound to C-M-% by default, and provides a similar interface, with the addition of support for regular expression matching.

Both commands are explained in the manual node (emacs) Query Replace.

80 questions
24
votes
1 answer

How to perform case-sensitive query-replace?

When I do M-x query-replace to replace all occurrences of v by w, then it also matches V (uppercase V). How to force emacs to only find lowercase v ?
Name
  • 7,689
  • 4
  • 38
  • 84
9
votes
2 answers

Replace two strings with each other

I would like to change all occurrences of a string (say string1) with another string (say string2) and vice-versa. To be precise I have a long document and these strings occur many times. What I currently do, is to replace string1-->string3, then…
Name
  • 7,689
  • 4
  • 38
  • 84
8
votes
1 answer

How to apply arithmetic operators to query-replace-regex

I am attempting to replace all "begin" and "dur" attribute values in my SVG code with one-tenth of (or ten times) their current values, in order to speed up/slow down animation for testing/debugging purposes. e.g. replace dur="900s" with…
7
votes
1 answer

Sane settings to show context when doing search and replace in org-mode

If I run certain functions like isearch-forward (with C-s) in an org-mode buffer, all content become temporarily visible. When I end the search, headings automagically collapse the way they were before I searched. Nice. But if I run something like…
Brian Z
  • 2,863
  • 16
  • 22
6
votes
1 answer

Reusing previous inputs (old or new) for query-replace

Most functions in emacs keep a default option, with the last arguments used. Is it possible to reuse some of the default arguments while changing others? For example, when using search and replace, a string such as Query replace (default thing1 ->…
user2699
  • 2,181
  • 16
  • 32
6
votes
3 answers

Regexp replace to match a string, but not match a superstring

Let's say I want to replace the string "Vector" by "VectorBase", but there are existing instances of "VectorBase". So I would like to omit "VectorBase". What is an elegant way to achieve this? An easy way is to do ignore the condition and do the…
hrkrshnn
  • 439
  • 6
  • 14
6
votes
1 answer

Query replace with different replacements

Suppose we start with the following Adam ate an apple. Adam ate an apple. Adam ate an apple. Adam ate an apple. Adam ate an apple. I want to replace Adam with Bob, Chris, and David. So I want to do it such that I call a query-replace-like…
Quarky Quanta
  • 415
  • 4
  • 12
6
votes
2 answers

how to query-delete-duplicate-lines?

For me, one of Emacs' most useful functions is delete-duplicate-lines. I call it this way, which very helpfully leaves blank lines intact: (defun delete-duplicate-lines-keep-blanks () (interactive) (delete-duplicate-lines (region-beginning)…
incandescentman
  • 4,111
  • 16
  • 53
6
votes
1 answer

Regexp replace without interaction

Is it possible to replace regex like query-replace-regex without asking for each matching ?
user3593232
  • 383
  • 1
  • 8
5
votes
5 answers

More simple approach to replace some text in many files?

Linux Mint 20.2 Emacs 27.1 Task: I has folder with MyProject with 5 subfolders. Total count of files in all this folders are about 500 files. I need all files that content import org.apache.log4j.Logger; replace by line: import…
a_subscriber
  • 3,854
  • 1
  • 17
  • 47
5
votes
1 answer

replacing regular expressions with querying on and functions on match

EDIT: gave more context for my first approach. My goal is to search my notes for strings of the form "4/1/2014" to org-mode timestamps "<2014-04-01 Wed>" (that particular day-of-week may be incorrect). I also want querying to be on so I do not…
Yan X Zhang
  • 195
  • 6
5
votes
1 answer

(RegEx-)Replace: Sporadic Match data clobbered by buffer modification hooks

I know this was asked in other contexts already, but I couldn't find anything related to C++ development with my hooks that could help me. I am working a lot with replace and query-replace and query-replace-regexp and I love it. Now I have the very…
5
votes
1 answer

How to remove empty lines of a selected region of code in Emacs?

Is there a way to remove blank lines from a region of code I select? I am asking because I copy/pasted code and it seems to have several blank lines I want to get rid off.
Billal Begueradj
  • 345
  • 1
  • 5
  • 18
5
votes
3 answers

How to find and replace regex within a rectangle selection block

I have the following text: | Row # | Facility | Recycled Off-site | |-------+--------------------------+-------------------+ | 1 | 1234, 12th st, jamestown | 216,574 | | 2 | 1234, 12th st, jamestown | …
modulitos
  • 2,432
  • 1
  • 18
  • 36
5
votes
2 answers

Query-replace with some context checking?

(query-replace "foo" "bar") shows you all occurrences of foo and offers to replace them by bar. I would love to have a version of query-replace that only picks the occurrences of foo that are "in some context" (to be defined). At the moment, I'm…
phs
  • 1,095
  • 6
  • 13
1
2 3 4 5 6