Questions tagged [replace]

Replacing is the action of searching a string for a sub-string and replacing it with a different string.

Filter by
Sorted by
Tagged with
33
votes
5answers
4k views

Is there a way to use query-replace from grep/ack/ag output modes?

I'm aware of find-grep-dired, marking the corresponding files and then pressing Q to run dired-do-query-replace-regexp on the marked files. Unfortunately that requires restarting the grep and no ...
19
votes
6answers
7k views

How to search and replace in the entire buffer?

Search and replace, by M-% and !, is done from current position to the end of the buffer. How can I do it for the entire buffer? Thanks.
13
votes
3answers
3k views

Is there an equivalent to sed-style substitution commands from vim?

One of the things I miss from vim is being able to type in a substitution command that will work over multiple lines, for example: :/begin/,/end/s/foo/bar/g The above command substitutes "foo" for "...
13
votes
2answers
1k views

Incrementally replace a given string

Let us say I have a text like so below: AC(nn) AC(nn) AC(nn) AC(nn) AC(nn) AC(nn) AC(nn) AC(nn) AC(nn) AC(nn) AC(nn) Now I want to replace the nn with numbers like so AC(0) AC(1) AC(2) AC(3) AC(4)...
11
votes
4answers
116 views

How to convert a series of lines into a working HTML list?

Right now, this is a task I find a lot easier in something like gedit, because I can just replace "\n" (the line break) with "</li>\n<li>" and then I have a list. One of the few little ...
11
votes
2answers
382 views

Search/replace-like feature for swapping text

I frequently find myself trying to replace e.g. foo with bar and bar with foo in a buffer. The way I usually do it is either: 3 query-replaces: aaa -> @@@, bbb -> aaa, @@@ -> bbb give up on ...
10
votes
5answers
1k views

How to replace matching parentheses?

I write (and rewrite) a lot of mathematical formulas in LaTeX with Emacs. I frequently run into situations where I want to change a pair of matching parentheses, to improve readability. My Emacs is ...
8
votes
2answers
525 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 ...
7
votes
2answers
411 views

GROUP BY + COUNT over lines in a region

What is the simplest way of getting the number of distinct repeated in a region? For example, from THIS IS LINE A THIS IS LINE A THIS IS LINE A THIS IS LINE B THIS IS LINE B THIS IS LINE C I would ...
7
votes
2answers
586 views

query-replace in keyboard macro

Let me define a keyboard-macro: I enter C-x (, then go to beginning with M-<, then type M-% to query-replace some occurrences of foo by bar. Out of 3 occurrences found, I replace only the last 2, ...
7
votes
2answers
275 views

How to refactor elisp programs using pattern matching on sexps with replacement?

I'm looking for functionality that can do source-to-source transformation of elisp programs given a set of rewriting rules. For example, given a rule like: (set (make-local-variable (quote ?var) ?...
7
votes
1answer
102 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 ...
6
votes
3answers
6k views

How to replace words in a region using iedit?

I want to replace some words inside a region. I would like to use iedit more than query-replace, but found the following problems (probably because I don't understand its documentation): If I select ...
6
votes
3answers
836 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 ...
6
votes
1answer
811 views

Regexp replacement: difference between \([[:digit:]]+\) and ([[:digit:]]+)

With M-x replace-regexp, I found that both \([[:digit:]]+\) and ([[:digit:]]+) will match (123), and not one without parentheses, But the first will replace 123 and the second (123). I wonder why the ...
6
votes
3answers
135 views

`query-replace` leaves potential match to be replaced at bottom of window

If I have a long document that I run query-replace (or any other number of commands) in, as I'm saying y or n to replace that match or not, the current potential item to be replaced is always at the ...
6
votes
1answer
2k views

how do I do case sensitive searches using evil/spacemacs?

I'm trying to perform a search and replace in spacemacs but I need it to be case sensitive when by default it appears to be case insensitive. So I try to do :set noignorecase but then I have this ...
6
votes
2answers
336 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)...
5
votes
3answers
348 views

Repeat replacement until not possible?

I would like to repeat replacing a string with another string in a text file, until it is not possible. Is it possible to do that in Emacs? I only know to replace just once with M-% and !. For ...
5
votes
1answer
3k views

Replace string in buffer programatically

I'm writing some elisp code and would like to replace some string with some another string in a buffer. Which elisp function does that ? I did a search on documentation for the related function but ...
5
votes
1answer
1k views

How to save part of a regular expression during search and replace?

I think the best way to explain what I'm trying to do is using an example. Suppose one would want to replace all underlined words with italic ones in org-mode using a regular expression: _example_ --&...
5
votes
1answer
397 views

replace-regexp: pattern is matching too much

I'm having problems to replace a URL inside a HTML tag with replace-regexp. This is the original string: <img src="http://localhost/~user/foo.bar/place/foo_bar.jpg" ... attr="..." /> This is ...
5
votes
2answers
210 views

How to cancel the default in replace-string?

When I use replace-string for the first time, it asks me for the string to replace and string to replace with. When running it again, it uses the previous values as default: Replace string (default a ...
5
votes
1answer
1k views

Why are double quotes not escaped in the replace-regexp command?

I have text like this: myvariable = ABCD, EFGH, IJKL, ~subsection1 MNOP, QRST, UVWX, YZ Which I want to transform to: myvariable = "ABCD," "EFGH,\n" \ "IJKL,\n" \ "~subsection1\n" \ "MNOP,\n" \ "...
4
votes
3answers
1k views

Search for and correct capitalized words that are not at the beginning of a sentence

I have a latex document in which there are many incorrectly Capitalized words. Could anybody suggest a clever regex to track them? Even better if there is a function which cycles through such words ...
4
votes
2answers
334 views

Automatically replace C++ operators

In some projects I'm working with, there are coding rules that don't allow C++ operators such as == (for fear of being confused with = I suppose, but oh well...). Instead, special macros such as _EQ_ ...
4
votes
1answer
286 views

Search and replace with case preservation and plurals

Is there a way (or package) to search and replace code like: class Account def list_accounts end end to class Company def list_companies end end Basically I want to replace account(s) with ...
4
votes
3answers
124 views

Search and replace TeX \over with LaTeX \frac

I have a source file that was originally coded in TeX (well, this is actually a conTeXt file). A fraction like 1/(2/x^2) is coded as {1 \over {2 \over x^2}}. I'd like to convert to LaTeX syntax, that ...
4
votes
1answer
3k views

How can I edit the Helm results buffer and have the changes reflected in the original buffers?

I've seen this demonstrated before in various screencasts and can't find it searching through the docs. I do use helm-swoop-edit quite often in a single buffer but when I need to change all ...
3
votes
1answer
75 views

Processing of text

It is pleasure to ask you about something I cannot solve myself. I am working with big text files with many similar lines. The format of the files is defined and I want to replace repetitively some ...
3
votes
4answers
191 views

Replace all literal substrings from a list in a string

My goal is to replace all literal sub-strings in a given string. (defun translate (str) (concat (mapcar (lambda(cons) (replace-in-string (car cons) (cdr cons) str)) '(...
3
votes
2answers
11k views

How to escape underscore in string

I want to escape a underscore in a filename with a backslash (\). So file_01.jpg would become file\_01.jpg. I tried: (replace-regexp-in-string "_" "\_" "file_01.jpg" t t) but that produces ...
3
votes
1answer
167 views

Arithmetics in a regex in a lisp function

I want to replace all hard column widths in several LaTeX-documents, e.g. replace {90mm} by {0.75\textwidth}. I already found out that you can use \, and #1 to so arithmetics on captured groups, like ...
3
votes
1answer
920 views

Evil-emacs: how to make search/replace (:s/) global by default?

Vim has the following setting: :help gdefault When on, the ":substitute" flag 'g' is default on. This means that all matches in a line are substituted instead of one. When a 'g' flag is given to a ...
3
votes
1answer
424 views

Quoting a replacement string for a regular expression

Emacs Lisp has regexp-quote to quote any string to match it literally. Apparently, there is no equivalent function to quote an arbitrary replacement string to use it literally. I mean, what could I ...
3
votes
2answers
205 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, ...
3
votes
2answers
682 views

Emacs replace-regexp reference the match in the replace argument

Is there an easy way to use replace-regexp to do something like: XYZ001 XYZ002 XYZ003 to 'XYZ001' 'XYZ002' 'XYZ003' i.e. replace-regexp XYZ* 'XYZ*' Is replace-regexp the the wrong tool for the ...
3
votes
1answer
384 views

Find/replace in differrent file extensions

Windows 10, Emacs 25.1, Dired+, xah-find I use package xah-find for find/replace text in many files. OK. It's work. If I want to find in all java files I use the next regexp: File path regex: \.java\...
3
votes
1answer
854 views

Regular expression matching group replacement not working

I want to convert .waitForVisible('.someselector') .click(); Into: .waitAndClick('.someselector'); Automatically in a lot of files. So I wrote: (defun hhaamm-replace-regexp-in-buffer (from to) (...
3
votes
1answer
57 views

How to add a suffix to the end of a string in a line?

Here's an example of content in a file This_is_line_1 : 1 This_is_another_line :2 This_is_3rd_line :3 Here_is_line_4 :4 So, how can using emacs to add char @ immediately to the end 1 string ...
3
votes
1answer
133 views

Search and replace two consecutive newlines

Running spacemacs 0.200. I am sorry if it is too basic. But I am just not able to get this. How do I convert two consecutive newlines with some text, say, 'there were two newlines here'? I could ...
3
votes
1answer
33 views

how can i do for replace a part of a regexp

For example i have this text: select * from T1 ; select * from T2 ; select * from T3 ; My objetive is replace the text by this other: select * from T1_H ; select * from T2_H ; select * from ...
3
votes
1answer
2k views

How do I lowercase (downcase) a group with replace-regexp?

I have some text that looks like this: "CFName" "CLName" "CDOB" "CGender" I would like to get output where are of the text inside the double quotes is ...
3
votes
1answer
596 views

Multiline regex replace within elisp function

I would like to write a elisp function that corrects a few formatting issues in my LaTeX code. Precisely, I would like to transform the following text Some useless text \begin{equation} some math ...
2
votes
2answers
848 views

Replacing string in a region and conditional replace

My goal is to write a simple function that can do the following: Staring with an expression that looks like $$ x = y $$ $$ y = z $$ $$ z = x $$ (There can more lines of such equations) I want to ...
2
votes
2answers
111 views

Replacing every empty line with a string, i.e. inserting strings to empty lines

Given that I have a file such as Lorem Ipsum Dolor I want to insert strings (here, the % character) to those empty lines to obtain Lorem % % Ipsum % Dolor How can I do this? I tried to follow a ...
2
votes
1answer
660 views

Capitalizing letters that follow colons using 'replace-regexp-in-string'

I want to capitalize letters that follow colons. Here is the code: (replace-regexp-in-string ": \w" 'upcase ": really") The result I expected is : Really. Instead I get : really. Similarly, this ...
2
votes
2answers
91 views

How can I use a char as a counter?

I have this LaTeX code: \begin{enumerate}[a] \affiliation{...} \affiliation{...} \affiliation{...} ... \affiliation{...} \end{enumerate} and, using a script written in Emacs Lisp, I want to ...
2
votes
2answers
261 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 ...
2
votes
2answers
197 views

Replace whole words in string

replace-string has the DELIMITED parameter, which can be used to replace only whole words. replace-in-string does not have such a parameter. Is there an easy work-around besides creating a temporary ...