Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [query-replace]

The tag has no usage guidance.

1
vote
1answer
32 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 ...
4
votes
1answer
31 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 ...
2
votes
1answer
54 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 .....
7
votes
3answers
602 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 ...
1
vote
1answer
39 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
34 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
25 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
29 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
176 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
57 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 ...
1
vote
1answer
42 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 ...
2
votes
1answer
45 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
103 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
105 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
200 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
62 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
56 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
53 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
53 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 ...
2
votes
2answers
120 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
61 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
45 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
38 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
64 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
24 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
56 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
353 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 ...
3
votes
2answers
180 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
50 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 ...
7
votes
2answers
281 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
285 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
203 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
124 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
230 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)...
1
vote
1answer
50 views

Intelligently edit draft with emacs

I have a co-worker to edit LaTeX document. The co-worker uses LaTeX \color and \sout command to make the changs visible in pdf file. For example, Hello world becomes Hell{\color{red}\sout{o}a} ...
4
votes
1answer
87 views

Regexp replace without interaction

Is it possible to replace regex like query-replace-regex without asking for each matching ?
2
votes
1answer
701 views

How can I recreate this keybinding in Spacemacs?

My .vimrc contains this snippet: nnoremap S :%s//g<LEFT><LEFT> It creates a key binding which means that when I'm in normal mode, I can: type the character S type the string find/...
4
votes
3answers
502 views

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 | | ...
3
votes
1answer
61 views

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

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 ...
0
votes
1answer
37 views

query replace ingonring new lines

My text is written in paragraphs that are split into lines, which makes it difficult to search for strings that might be spanned across several lines. For instance, given the following text This is ...
2
votes
2answers
86 views

substitute in the filename

In Dired Query and Replace, can we substitute in the filename being visited? For example, I want to substitute "solve" in all *.mod files with the filename *.mod.
1
vote
1answer
63 views

Regexp doesn't work

I want to convert all the deques in my python code to lists. I.e. deque([1]) should become [1]. I tried M-% deque(\(.+\)) RET \1 RET, but it said Replaced 0 occurences. What's wrong with my regexp?
1
vote
2answers
912 views

Named groups for query-replace-regexp

I'd like to define a function to do a query-replace-regexp with a pattern I often use. When not using capture groups, it works: (defun my-replace () (interactive) (query-replace-regexp "[[:...
14
votes
1answer
4k views

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 ?
3
votes
1answer
269 views

How to combine the history of isearch and query-replace?

I often use isearch (C-s normally or / in evil) and then want to use that query (or part of it) for replacing. This answer explains how to cycle history, but M-p cycles the history for each mode ...
3
votes
2answers
110 views

How to query-replace across all hits in a compilation-mode buffer

How can I run a query-replace on the all the files pointed to by an arbitrary compilation-mode buffer? I think to be usable this would need to be resumable from point in the compilation-mode buffer. ...
5
votes
1answer
280 views

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