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
0
votes
1answer
21 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]]") ...
0
votes
1answer
39 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&...
3
votes
1answer
44 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 ...
0
votes
2answers
69 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 ...
2
votes
1answer
53 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 ...
0
votes
2answers
58 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-...
2
votes
1answer
58 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 ...
1
vote
1answer
285 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?
1
vote
1answer
62 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 ...
1
vote
1answer
83 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 ...
4
votes
1answer
235 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 ...
3
votes
1answer
65 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: ...
1
vote
1answer
58 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 ...
2
votes
1answer
53 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 ...
2
votes
1answer
72 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-...
0
votes
1answer
50 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 ...
1
vote
1answer
47 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 ...
5
votes
1answer
66 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 ...
3
votes
2answers
105 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 .....
6
votes
3answers
810 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 ...
4
votes
1answer
122 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-...
0
votes
1answer
89 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) ...
2
votes
0answers
147 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: [...
1
vote
1answer
50 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): ...
6
votes
1answer
186 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 ...
4
votes
2answers
121 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 ...
2
votes
1answer
68 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 ...
3
votes
1answer
61 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 ...
1
vote
1answer
876 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.
1
vote
1answer
124 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 ...
3
votes
1answer
454 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 ...
1
vote
1answer
104 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 ...
1
vote
1answer
59 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 ...
0
votes
3answers
270 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 = ...
0
votes
0answers
91 views

C-M-% not works in MacOS terminal

I'm fresh to emacs and following manual to practice Query-Replace operation. But I find it not work at all. Type C-h l where it shows ESC 5 seems my OS swallow some key input or emacs parse input ...
3
votes
2answers
202 views

Query-replace-regexp alternative capable of handling nested braces?

TL;DR Is there some method in Emacs to replace text with nested braces/parentheses? Example I had a situation where I had to replace \dchi{ARG1}{ARG2} by \chi^{ARG2}(ARG1) For simple cases, ...
0
votes
1answer
137 views

function to add/split a new line each time a dot (.) is present?

Im looking for a way (function) to add/split a new line each time a dot (.) is present? for example turn this When a user wants to communicate with the computer, the user interacts with an ...
2
votes
1answer
46 views

Query-replace in multiple LaTeX environments, narrowing each in turn

I am trying to come up with a Elisp function that narrows a buffer to a certain LaTeX enviroment (table, figures, ...), does some query-replace-regexp inside the narrowing, then searches for the next ...
0
votes
1answer
48 views

Need help to debug an issue with query-replace command

The query-replace command used to work perfectly for me. Recently, it fails with a symptom like this: [updated] Assuming the code looks like: (for example) set_flaag(var1); set_flaag(var2); Now I ...
1
vote
1answer
108 views

Passing a string with substitutions to the “perform-replace” command

I want to generalize this query-replace regexp: (perform-replace "Geophysical[\s\t\n]*Research[\s\t\n]*Letters" "Geophys.\\\\ Res.\\\\ Lett." t t nil 1 nil (point-...
1
vote
1answer
25 views

Face-based replacements

I need to perform this kind of (query-)substitutions in LaTeX code: "fig" --> "figure" But I don't want to match text inside commands e.g. \label{fig:1} or \includegraphics[width=2cm]{fig/D.pdf}. ...
3
votes
2answers
58 views

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 ...
2
votes
1answer
766 views

Dired search and replace is throwing no results

I am trying to quickly search and replace in multiple files using dired. The steps I follow are from this question (Chris Conway's and Frank Henard's answers), I am pretty sure the instructions are ...
2
votes
2answers
246 views

query-replace-regexp on specific lines

how does one do a query-replace-regexp to add a prefix string but not on EVERY line but rather just lines with text (or start with specific text?) i know how to add a prefix with query-replace-regexp ...
0
votes
2answers
69 views

Keep selection on/reselect for multiple query-replace sessions

Following this thread: https://stackoverflow.com/questions/39808820/replacing-on-mouse-selected-area-in-emacsI can highlight an area, and do replace only on this area. Great! Now, I want to do several ...
8
votes
2answers
508 views

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 ...
2
votes
1answer
424 views

Difficulty with query-replace-regexp

I'm having trouble getting query-regex-replace to do what I want. I've read the docs several times and still can't figure it out. Here are a few examples of things I can't figure out how to do with ...
3
votes
4answers
486 views

query-replace on directory

How do I search&replace selected instances (in particular those occurring in strings, not in variable names – but using an equivalent of 'query-replace is safer and still fine, there should not be ...
1
vote
1answer
179 views

query-replace-read-to: Command attempted to use minibuffer while in minibuffer

I want to rename file in dired by replacing underscores with spaces. I hit R Down (dired-do-rename) and after typing M-% (query-replace) I got: query-replace-read-to: Command attempted to use ...
6
votes
2answers
323 views

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