We’re rewarding the question askers & reputations are being recalculated! Read more.

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

Replace full-width numerals with half-width numerals

I'd like to convert numerals from half-width to full-width characters with a simple regex search and replace as follows: replace-regexp-in-string regexp rep string &optional fixedcase literal ...
0
votes
2answers
43 views

Replace a block of code with another

How can I replace a block of code with another, akin to query-replace/query-replace-regexp? For example: Replacing the following block: for(A) { if(B) { s1; } else { C; } } with ...
0
votes
3answers
83 views

Complex searching and replacing [duplicate]

Suppose I have following pattern: throw "some message"; which I'd like to replace with: throw new Error("some message"); So, "some message" part should not be changed (or copied as is), and ...
1
vote
2answers
67 views

Selecting next occurrence of word incrementally

I want to make changes inside a paragraph of a large text file. If I have the cursor at the word "foo", what key combination would get the cursor at the next occurrences of this word? The cursor ...
0
votes
1answer
36 views

How to call `replace-string` with specific strings from code?

Sometimes I want to replace [$] with \( and [/$] with \) either in the region or in the whole buffer. replace-string works well for that, except for requiring me to type the strings I want to replace. ...
1
vote
1answer
44 views

Regex: Don't Match Text Between Curly Braces (LaTeX Commands)

I've been using pandoc and emacs to convert beamer presentations to markdown format. In the beamer presentations we highlighted certain code in example code blocks red. Pandoc has left me to get rid ...
1
vote
1answer
41 views

How to paste a multiline regex from re-builder into `replace-regexp`

I built with M-x re-builder a regex to match empty quoted lines in emails. In read syntax it is: "^>\\s-*\n>" How can I copy this regex in re-builder, so that I can paste it in the first ...
1
vote
1answer
60 views

csv-mode: do not split line on newline inside quoted field

I'm using the latest csv-mode in Emacs 25.3.1 in centos 6.10. The problem is that I have many fields that are long documentation strings and many of them have embedded newlines. This messed up csv-...
2
votes
1answer
62 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 ...
0
votes
0answers
36 views

replace char at position using Emacs script

Below script will insert a char at position: #!/usr/bin/env emacs --script (defun modify(filename) (find-file-literally filename) (goto-char 3) (insert "\x39") (save-buffer 0) (kill-buffer)...
0
votes
2answers
77 views

Using Occur to to recursively search in a directory [duplicate]

I use 'occur' to search for regexp in current buffer and edit the results in occur buffer . Now my requirement is to search all the files in a directory(including sub-directories ) and edit the ...
1
vote
2answers
95 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 ...
0
votes
1answer
51 views

Search and replace isn't working in my function

I created the following function to remove macrons from vowels (this is a reduced version that only searches for the vowel "ā"): (defun nv/eliminatemacron () "" (interactive) (save-excursion ...
1
vote
1answer
60 views

Elisp expressions in projectile-replace-regexp

I need to execute a regular expression with case conversion in a entire project. If I run query-replace-regexp in a single file informing '\([A-Z]+\)' and '\,(downcase \1)', it works as expected. But ...
1
vote
1answer
181 views

search and replace double space

I've only recently started using emacs, for professional reasons. I'm trying to replace all double spaces in a text file by single ones using regex. In C-M-s the expression \s-\{2\} matches two spaces ...
4
votes
3answers
85 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 ...
2
votes
1answer
51 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
55 views

How safe is “with-silent-modifications” to mask (hide) strings before and after buffer modification?

In my LaTeX code I want to replace one-line eqnarray environments with the equation environment. To do that I need to "count" the new-line (\\) strings in a region and I also need to ignore some new-...
6
votes
3answers
722 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
93 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
37 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
21 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
76 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
61 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) ...
1
vote
1answer
411 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
53 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
63 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
143 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
53 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
132 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
24 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
168 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
120 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
125 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
142 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
66 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
85 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
457 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 ...
12
votes
2answers
835 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
359 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 ...
9
votes
4answers
541 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
36 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
117 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 \...
1
vote
2answers
235 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?
3
votes
1answer
344 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
63 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
91 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
124 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 ...
2
votes
2answers
157 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
29 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: ...