As of May 31, 2023, we have updated our Code of Conduct.

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.

Filter by
Sorted by
Tagged with
2 votes
2 answers
98 views

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 ...
Ruy's user avatar
  • 767
0 votes
1 answer
85 views

How can I recursively find and replace text in case sensetive?

I am using combination of following solutions in order to find-and-replace matched text in all Python files: Using Emacs to recursively find and replace in text files not already open How can I ...
alper's user avatar
  • 1,164
0 votes
1 answer
42 views

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-...
Gabriele Nicolardi's user avatar
1 vote
1 answer
162 views

How can I replace all in symbol manner?

I am using following solution (How do I add a keybinding to use during an interactive isearch, `C-s ...`) to move over symbols in the buffer, which iterates over the matching symbols: (define-key ...
alper's user avatar
  • 1,164
1 vote
2 answers
68 views

Read-only mode gets in the way when query-replacing text in multiple files

I am searching and replacing text within all marked files in a Dired buffer, as per this post: https://emacs.stackexchange.com/a/9497/37866 I am successful until the point where it finds the first ...
marsh's user avatar
  • 11
0 votes
1 answer
38 views

Find and recursively delete (with query) consecutive duplicate words not necessarily in the same line and/or same case

I would like to scan through a (latex) document and recursively delete -- WITH QUERY -- all consecutive, repeated words that need not be on the same line and/or the same case. Here is a simple ...
underflow's user avatar
  • 111
4 votes
4 answers
429 views

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 I all files that content import org.apache.log4j....
a_subscriber's user avatar
  • 3,830
0 votes
0 answers
52 views

Why doesn't project-query-replace-regexp work well

It seems promising to have a built-in function that performs this action: find and replace all matching text recursively, within your project. The problem is that it doesn't seem consistent. I am ...
young_souvlaki's user avatar
1 vote
1 answer
62 views

(Spacemacs) pressing SPC to replace a match during query-replace does not work

(Spacemacs) M-% starts query-replace, and when a match is found, ? shows help, which indicates: Type Space or ‘y’ to replace one match, Delete or ‘n’ to skip to next, However, pressing Space simply ...
taranaki's user avatar
  • 113
1 vote
0 answers
44 views

How to perform query-replace excluding LaTeX math?

I have this test code: foo \begin{equation} foo \end{equation} foo $foo$ foo I want to replace "foo" with "bar" everywhere but LaTeX math, so I have written these functions: ...
Onner Irotsab's user avatar
0 votes
1 answer
36 views

Automatically save files after multi-file search/replace

How can I get GNU Emacs to automatically save all the files I've changed after doing a multi-file search/replace with query-replace-regexp? Right now I have to switch back to the buffer for every ...
cjg's user avatar
  • 1
1 vote
0 answers
46 views

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,...
Yuxiang Wei's user avatar
1 vote
2 answers
256 views

How to make query-replace automatically fill what I want to replace with marked text?

I want to select some text somewhere in a buffer, call query-replace and then type (or paste from kill-ring) only the second argument, TO-STRING, (and make the selected text count as FROM-STRING). I ...
achempion's user avatar
  • 115
0 votes
0 answers
48 views

query-replace within LaTeX/Math

The answer below was taken from https://stackoverflow.com/a/44913296. This is the most upvoted answer there. Unfortunately, after running the code (for example by replacing q with \theta) one ...
Name's user avatar
  • 7,644
1 vote
1 answer
193 views

remove "-" (soft hyphen) between words

When I try to paste a text from a web page inside Emacs I get this "-" between words. Here is one example: Is there a way to get rid of this (don't know the term..) ? Aus Sicht vie­ler ...
Enrico Pirani's user avatar
1 vote
2 answers
559 views

How do I search/replace with case sensitive search?

In emacs I'm using M-% to replace occurrences of b_abcd (of which there are only a few) with B_abcd (of which there are very many). Currently Emacs asks if it's OK to replace at each B_abcd, which ...
HarryH's user avatar
  • 121
0 votes
2 answers
108 views

How can I paste a defvar into the minibuffer?

I've written a regular expression to transform Markdown links into Org Mode links and I've stored it into a variable. (defvar markdown-link-to-org-regexp "s/\[\(.+\)\](\(.+\))/[[\2][\1]]") ...
tonicebrian's user avatar
0 votes
1 answer
80 views

How to query-replace with exact match with the exact given word in case-sensetive manner?

I have HELLOWORLD string inside my file. My setup: (defun my-find-orgfiles () (interactive) (setq toggle-case-fold-search 0) ; Turn Off Smart Case Sensitivity (find-name-dired "~/folder&...
alper's user avatar
  • 1,164
3 votes
1 answer
54 views

How to access Query-replace history in replace format (for eg: "ABC → XYZ")

I have enabled query replace save history as below. When I am in query-replace mode, I see previous search-replace patterns of the current session in the form "ABC -> XYZ". But the search-replace ...
emacsbeginner's user avatar
1 vote
3 answers
2k views

Replace string in rectangular region?

When you select a region normally, you can do a replace-string on only that region. It would be very useful to select a rectangular region (via C-x SPC), and be able to replace-string on only that ...
gilbereth's user avatar
2 votes
1 answer
69 views

Query replace regexp with a distant match?

I have a single transcript file for a set of course lectures I am watching. After doing some formatting on the file. I have it broken up into sets of lessons where the main heading is the lesson title ...
dylanjm's user avatar
  • 303
0 votes
2 answers
120 views

query-replace{-regexp} fails in linux in a non-X terminal when I am in anything other than text mode

I am running emacs 26.1 under linux. It has been working fine for over a year. Recently, I upgraded a lot of packages on my machine, but I did not upgrade nor alter emacs in any way. Now, query-...
HippoMan's user avatar
  • 582
3 votes
1 answer
213 views

Can Dired query replace use regular expression to rename a group of files?

So I have long list of files that I'm trying to use regex to rename but, it looks like it does not take standard regular expressions. I’m sure I’m just doing this incorrectly. After entering dired ...
Troy Wilson's user avatar
3 votes
2 answers
2k views

Search/replace and replace within a block of selected text of buffer in spacemacs

As stated in the title, I want to search and edit (including replace) certain strings in a selected part of a buffer, not the entire buffer!!!, how can I do it? Are there any built-in keystrokes?
Jason Goal's user avatar
3 votes
2 answers
314 views

How to count regexp in a string and use that number in replacement?

In my LaTeX code I want to replace these strings: \left.\left.\left. \left.\left. \left. with: \left.\left.\left. \mycommand{3} \left.\left. \mycommand{2} \left. \mycommand{1} where the argument ...
Gabriele Nicolardi's user avatar
1 vote
1 answer
140 views

How to perform query-replace inside mathematical expressions in latex

Inspired by this answer, I have written the following function to perform a query-replace inside a math environment, but it does not work. Edit. If you execute the function, the query-search tries to ...
Name's user avatar
  • 7,644
8 votes
1 answer
830 views

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 ...
Peter Chaplin's user avatar
3 votes
1 answer
129 views

How can I identify all changes to match-data?

I have a sporadic error when running query-replace: Args out of range: #<buffer lib.rs>, 0, 1 There are similar questions: Match data clobbered by buffer modification hooks (RegEx-)Replace: ...
Shepmaster's user avatar
1 vote
1 answer
103 views

Make query-replace skip certain matches

Lets say I have these strings: "foo", "fooo" and "fo". And this line of code: (query-replace-regexp "\\<fo+\\>" "foo" nil (point-min) (point-max)) I'd like to skip the query-replacement for ...
Gabriele Nicolardi's user avatar
2 votes
1 answer
96 views

How to replace a digit by a letter?

I want to replace some digits by letters such that 1 ---> a 2 ---> b ... In emacs I have tried the following replacement: M-x query-replace-regexp RET \([0-9]+\) RET \,(string (+ ?a \#1)) RET ...
Onner Irotsab's user avatar
2 votes
1 answer
138 views

Add multiple predicates to isearch-filter-predicate

I wrote a set of predicates to filter matches in my ìsearch-* and query-replace* routines. I also wrote a macro to bind the isearch-filter-predicate variable to multiple predicates: (defmacro with-...
Gabriele Nicolardi's user avatar
0 votes
1 answer
64 views

texmathp in isearch-filter-predicate breaks query-replace's match highlighting

I tried to use texmathp with iserch-filter-predicate to let query-replace function skipping maths in my LaTeX code. I wrote this code: (defun skip-math (beg end) "Return nil if some text BEG to ...
Gabriele Nicolardi's user avatar
1 vote
1 answer
49 views

How query-replace-regexp for all text exact before specific text?

emacs 26.1, windows 10 I have some text. I need to replace all text before WARN I use: query-replace-regexp: .*WARN query replace regexp: RET But I need to delete all text BEFORE WARN. How I can ...
a_subscriber's user avatar
  • 3,830
5 votes
1 answer
161 views

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 ...
Yan X Zhang's user avatar
3 votes
2 answers
131 views

Make region(s) invisible (not evaluated) to query-replacy and similar commands

Is there a way to make some regions of the buffer invisible (I mean not evaluated by) to query-replace and similar commands? Let me better explain with an example. Let's take a piece of LaTeX code .....
Gabriele Nicolardi's user avatar
6 votes
3 answers
976 views

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 ...
hrkrshnn's user avatar
  • 429
5 votes
1 answer
226 views

(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-...
AltruisticDelay's user avatar
0 votes
1 answer
189 views

Advanced search and replace

I have a file, with a lot of instances of ... fclose(f1); fclose(f2); fclose(f3); ... I'd like to replace these with ... if(fclose(f1)!=0) return 1; if(fclose(f2)!=0) return 1; if(fclose(f3)!=0) ...
Mikkel bruun's user avatar
2 votes
0 answers
331 views

fix buffer invalid UTF-8 encoding characters by query/replace

When I copy text from web pages, there are some special characters like ', - etc (not UTF-8 encoding). As you can see in the following screenshot: I checked out some similar questions in this site: [...
stardiviner's user avatar
  • 1,848
1 vote
1 answer
81 views

Query replace a copied string by another copied one

Use case: I want to: copy a (long, not handy to manually type) newstring, replace all occurrences of another (long, not handy to manually type) string by newstring. Current process (not handy): ...
Denis Bitouzé's user avatar
6 votes
1 answer
197 views

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 ...
Quarky Quanta's user avatar
4 votes
2 answers
265 views

Text substitution only in specific column of org table

I came across this problem today: I had a table with many entries and wanted to perform a text substitution (normal text substitution was sufficient as I hardly ever need regex text substitutions) in ...
UTF-8's user avatar
  • 875
2 votes
1 answer
88 views

What mode does the command `query-replace' activate?

I'm editing my keybinds mode-by-mode, and I've now come to the query-replace-map. I want to define my keybinds in a mode-hook to keep my files logically consistent, but I cannot determine which mode ...
John DeBord's user avatar
3 votes
1 answer
96 views

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 ...
Peter B's user avatar
  • 181
4 votes
1 answer
1k views

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's user avatar
1 vote
1 answer
145 views

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 ...
undostres's user avatar
  • 1,773
4 votes
1 answer
688 views

Find and remove consecutive duplicated words while ignoring case

I am trying to find and remove duplicated words in a LaTeX file. Right now I am using query-replace-regexp with the regular expression \b\(\w+\)[ #]+\1 where # stands for line-break (C-q C-j). This ...
Tássio's user avatar
  • 209
1 vote
1 answer
175 views

Toggle string and regex replacement

How can I quickly toggle between string and regexp versions of functions such as replace-string/replace-regexp, query-replace/query-replace-regexp etc? Pretty sure I saw a shortcut once but now I cant ...
ext's user avatar
  • 113
1 vote
1 answer
61 views

Add breakpoints to a source code file en masse

I'm editing a .rb file with about 3000 lines of code. This file has about 30+ statements that does a .create( create() appears in many ways: as part of another expression: puts Foo.create(...) or ...
american-ninja-warrior's user avatar
1 vote
3 answers
1k views

Help with `replace-regexp-in-string`, to replace only first instance in every line

I have the following string: // const asdf = 'asdf' // // comment // const asdf2 = asdf And I want to run a regexp replace to make it look as follows: const asdf = 'asdf' // comment const asdf2 = ...
modulitos's user avatar
  • 2,402