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.

87 questions
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
  • 435
  • 4
  • 12
3
votes
1 answer

How do I get the old query-replace up-arrow functionality back?

the case: Let's say I did a query-replace is foo1 with foo2, and now I want to query-replace foo2 with foo1. In earlier versions of emacs (emacs 24, and every other version I remember), each previous entry into query-replace could be accessed via…
Peter B
  • 181
  • 4
3
votes
2 answers

How to return to the original place where query-replace was started?

Assume that I perform a query-replace in a document. When the process is finished, is there a simple way (1) to return to the original place where query-replace was started? (2) to return to the first place where a replacement was done?
Name
  • 7,849
  • 4
  • 41
  • 87
2
votes
2 answers

How to count number of replacements made by `query-replace`?

I am writing a piece of lisp code employing the function query-replace. After this function runs, I'd like to know how many replacements were effectively done. At first this seems easy because the return value of query-replace is a list whose…
Ruy
  • 839
  • 5
  • 11
1
vote
0 answers

Multiple replacements when query-replacing

I have a piece of code like this: def convert(tir_expr: tir.expr.PrimExpr) -> expr.Expr: if isinstance(tir_expr, tir.expr.Add): return convert_Add(tir_expr) elif isinstance(tir_expr, tir.expr.Sub): return…
1
vote
1 answer

Make query-replace skip certain matches

Lets say I have these strings: "foo", "fooo" and "fo". And this line of code: (query-replace-regexp "\\" "foo" nil (point-min) (point-max)) I'd like to skip the query-replacement for the matched string "foo" because it would be replaced by…
Gabriele
  • 1,554
  • 9
  • 21
1
vote
1 answer

How to use query-replace(-regexp) non-(semi-)interactively in a command?

The title isn't entirely accurate. By "non-interactively" I mean, for lack of a better term, "semi-interactively". In a regular call through M-% or M-x query-replace, the command could be called interactive in two ways: 1) by giving the string to…
undostres
  • 1,813
  • 14
  • 15
0
votes
2 answers

Build a "with-query-replace-description" macro

In the company where I work we use Emacs as the default editor for the LaTeX copy-editing tasks and we share some Emacs scripts to automate/guide some operations. Actually I need to perform some query-replacements into the LaTeX code and to make my…
Gabriele
  • 1,554
  • 9
  • 21