Stack Exchange Network

Stack Exchange network consists of 174 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 [replace]

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

6
votes
3answers
546 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 ...
0
votes
1answer
32 views

Replacing selected text in Emacs with one character

In Vim, I can do this trick on a line with the words "People": yy p Shift-v r + It will produce a set of lines that like this: People ++++++ I would like to be able to reproduce this in Emacs if ...
0
votes
1answer
29 views

How remove text before “,” (include) in every lines?

suppose I has the next text: -1,0x0 Fireplace HD 1160,0x0 Music HD 719,1 HD -1,1 MUSIC CHANNEL 154,1+1 -1,100% News -1,100AutoMoto TV I need to remove text before "," (include ,) So the result must ...
0
votes
0answers
10 views

Apply evil-mode Macro to helm-ag Results

I love using helm-ag-edit within projectile to make bulk edits to many files in my project in one fell swoop. However, sometimes I want to apply keyboard macros (using evil-mode) to these files. The ...
1
vote
3answers
51 views

Function that does `M-x replace-regexp` or `M-x occur` with a fixed pattern and replacement strings

I often use M-x replace-regexp and M-x occur with some fixed regexp pattern and replacement string on many files. Doing it manually by opening each file in an Emacs buffer and hitting the keys is ...
0
votes
1answer
31 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) ...
0
votes
1answer
77 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.
0
votes
2answers
30 views

Create strings of a wordlist using replace-regexp

I have a word list with a lot of words that I want to convert into a json file. To do this I need to make them strings and add a comma in the end of all words. I figured it would be nice to use ...
0
votes
1answer
34 views

Using a predefined Regex on a yank

can you please help me to write a small emacs function? This is the first lisp function I am writing and I think I am getting confused with point and mark placement. I want to write a function that ...
1
vote
0answers
81 views

Match data clobbered by buffer modification hooks in tex files

I run into the error Match data clobbered by buffer modification hooks when editing tex files. The way to reproduce is as follows. In terminal, cd mkdir tèst/ cd tèst/ touch test.tex echo "\input ./...
0
votes
3answers
38 views

Change Case of last searched pattern

Is there a fast way of changing the case of the last searched pattern? Inside Evil mode, I press # key to quickly search for keywords at point. And I want to change the case of all occurences of ...
0
votes
1answer
71 views

In elisp, search and replace only on lines containing a specific string

Running spacemacs 0.200.13 on emacs 25.2.2 on Kubuntu 18.04. I need to search and replace only on lines containing a specific string. Interactively, I use occur, move to the occur buffer, and do all ...
0
votes
1answer
20 views

Replace a match only till some other match is found

Aiming to convert first row of a html table from to tags. So, I wrote the following code. (defun tdtoth () (if (search-forward "</tr>" nil t) (while (search-forward "td" nil t) ...
1
vote
1answer
72 views

Case-sensitive search and replace in elisp in spacemacs

Running Spacemacs v.0.200.13 on emacs 25.2.2 on Kubuntu 18.04. Consider the following code: (defun xx-ll () (interactive) (save-excursion (goto-char (point-min)) (xx-replace-...
1
vote
1answer
77 views

Produce \' in a string replacement

As you know, functions such as replace-regexp-in-string and replace-match have an argument LITERAL, meaning ... If optional ... arg LITERAL is non-nil, insert NEWTEXT literally. Otherwise treat ‘\’ ...
1
vote
1answer
79 views

Replacing autoref command format specification in auctex's font-latex-match-reference-keywords-local

With some help I have redefined the autoref command to take an optional argument, see https://tex.stackexchange.com/questions/428229/autoref-with-optional-argument-text-in-generated-link Now I am ...
-1
votes
1answer
96 views

How to find all characters, newlines included?

I have this LaTeX code: %%% 10 \bibitem{bib2} W.~Kockelmann, G.~Burca, J.F.~Kelleher, S.~Kabra, S.-Y.~Zhang, N.J.~Rhodes et~al., \emph{Status of the neutron imaging and diffraction instrument IMAT},...
2
votes
2answers
54 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 ...
0
votes
3answers
49 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 = ...
1
vote
2answers
157 views

How to search for any unicode octal character in a buffer?

I'm looking for a way to locate all unicode octal characters in a buffer such as \344 and replace them with ä or \353 with ë, etc. At the least if I could just locate octal characters using ...
8
votes
2answers
283 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)...
2
votes
2answers
171 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 ...
7
votes
4answers
316 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 ...
0
votes
1answer
27 views

How can I constrain a predefined replacement-function to selected text?

When using replace-string from the M-x while having some text selected, it only operates on that selection (specifically, on an Evil visual selection, in my case). However, if I put the same ...
1
vote
1answer
59 views

Replace certain multibyte characters

In my markdown file (coming from a Windows computer on which I have no control), I have several multi-byte characters. Some text xx Some text yy Some text In emacs, they are displayed as \221 and \...
0
votes
2answers
86 views

Find and replace a text among the files of a project

How can I do a "find and replace a string" in the files of a project? Is there a standard, built-in way?
2
votes
1answer
203 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\...
1
vote
1answer
35 views

Executing multiple replacement regexps against a string

I wanted to write a function that "auto-corrects" a string, based on a list of regular expressions and their respective replacements. I've come up with this, and it works: (setq auto-corrections-...
0
votes
1answer
57 views

Using result of an Elisp function as input to replace-regexp in interactive mode

I am using replace-regexp interactively. Can I programmatically refer to a match or otherwise interpolate values in the replacement string? In Ruby, for example, it is easily done, by using the #{ <...
1
vote
1answer
102 views

elisp replace float number with half value

I need to replace below string: (24.0000,28.0000) .. controls (27.3100,28.0000) and (29.9800,25.3100) .. (29.9800,22.0000) to: (12,14) .. controls (13.655,14) and (14.99,12.655) .. (14.99,11) The ...
0
votes
0answers
81 views

<C-x>r-t without overwriting

how do i use rectangle select-replace C-x r t without overwriting the current column? For example, when I would like to add something before the first or last column, i must manually go and insert ...
2
votes
2answers
111 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, ...
1
vote
2answers
25 views

M-x replace-regexp uppercases words in replacement text based on the first letter present in the line

So, I have the following buffer: 1234 Red blue green yellow # colors orange Now, if I run M-x replace-regexp I get odd results. If I have the search string: ^\(.+\)$ and the replacement string: ...
1
vote
2answers
125 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 ...
2
votes
1answer
83 views

How can I replace outside of org-babel blocks?

I am using a command which affects the entire buffer, replacing all the ocurrences of a given expression (goto-char (point-min)) (replace-string "foo" "bar") Is it possible to apply a command like ...
2
votes
4answers
159 views

Search/replace inside a rectangular region, without CUA mode

I want to use search/replace (string or regexp) inside a rectangular region (aka column selection?). Consider this question: Search and replace inside a rectangle in emacs That's what I want, except ...
3
votes
1answer
127 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 ...
2
votes
1answer
80 views

Perform-replace only on text (no comments, no math) in latex-mode?

I need to write some Emacs-Lisp code that uses the perform-replace function, limiting its range of action only on text (ignoring comments, in-line math and math ambients) on LaTeX documents (in latex-...
1
vote
1answer
31 views

Incorrect value of region-end in function that calls replace-string

I am writing a function to do replacements on a highlighted region. It appears that while region-end changes after each replace, the value is not correct. That is, it does not point to the last ...
1
vote
1answer
282 views

“Invalid use of '\' in replacement text” with non-interactive calls

It seems that replace-regexp has a different behavior if called interactively and not, but I do not understand why, and how to get the wanted behavior. What I want is the non-interactive equivalent ...
4
votes
1answer
835 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 ...
3
votes
1answer
30 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 ...
0
votes
1answer
65 views

How do I debug when match-data is not getting set?

Lately, I have seen some cases where I use re-search-forward to find a pattern, and then try to do something with the match, only to find that the match-data is apparently not correct. For example, ...
0
votes
3answers
153 views

Search and replace on lines range, numerically

I was lately being teased by a vi user. He said that within few commands, one can do search-replace (both regexp and simple string ), on lines range. And one does not need to do highlight. One inputs ...
1
vote
1answer
404 views

Find and replace matching html tags

I want to transform some of the opening and closing tags in an html document to other tags. That is I would like to transform <div class='foo'> This is some text. </div> to something ...
3
votes
2answers
179 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 ...
6
votes
2answers
261 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 ...
11
votes
2answers
228 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 ...
1
vote
2answers
257 views

How do I replace a block of text with another block of text in elisp?

I'm working with an org-mode file that might contain headlines and text, like * Headline 1 :PROPERTIES: :ID: h1 :END: This is some text. ** Subheading A :PROPERTIES: :ID: s1 :END: ...
3
votes
1answer
838 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 lowercased, like this: "cfname" "clname" "cdob"...