22
votes
Accepted
Incrementally replace a given string
General technique
Your replacement string can contain arbitrary lisp code. From the documentation for replace-regexp:
In interactive calls, the replacement text may contain ‘\,’ followed by a Lisp ...
12
votes
Accepted
Replace two strings with each other
For the interactive case query-replace-regexp (C-M-%) can do this, using the relatively unknown \, for the replacement.
C-M-% \(string1\)\|\(string2\)
\,(if (equal \& "string1") "string2" "...
11
votes
How to replace matching parentheses?
For those using evil you can use evil-surround which gives you the c s motion (change, surround).
For your example then just do c s ( [ (motion, from type of paren, to type of paren)
9
votes
Accepted
Regexp replace to match a string, but not match a superstring
Try \_<Vector\_>. The \_< construct matches the empty string, but only at the beginning of a symbol. \_> is the same, but at the end of a symbol. What is a "symbol" depends on the buffer's ...
8
votes
Accepted
Evil-emacs: how to make search/replace (:s/) global by default?
There is a variable named: evil-ex-substitute-global
If you check it with C-h v, it will give you the explanation:
If non-nil substitute patterns a global by default. Hide Usually
(if this ...
8
votes
Accepted
Search/replace-like feature for swapping text
Here is a small command that will do this:
(defun query-swap-strings (from-string to-string &optional delimited start end)
"Swap occurrences of FROM-STRING and TO-STRING."
(interactive
(...
8
votes
Search/replace-like feature for swapping text
Install plur
and run the command plur-query-replace
and input {foo,bar} and its replacement {bar,foo}
Hit y to replace the occurrences as desired.
There are also non-interactive, and isearch-like,...
8
votes
Accepted
how do I do case sensitive searches using evil/spacemacs?
Short and narrow answer
You need to set the variable case-fold-search to nil in order
to have case sensitive search. So: put this somewhere in your init file:
(setq case-fold-search nil)
See the ...

Dan♦
- 32.7k
7
votes
Accepted
How to delete all instances of a character in a bunch of files?
Unless you want an interactive confirmation, which you presumably don't since you're automatic the replacement of all occurrences, call replace-string or replace-regexp instead of query-replace-string ...
7
votes
Accepted
How to refactor elisp programs using pattern matching on sexps with replacement?
I believe you're looking for the el-search package, available in the GNU ELPA repository.
It lets you match using pcase patterns and does implement the search&replace functionality that you're ...
7
votes
Regexp replace to match a string, but not match a superstring
Another simple trick you can use is to match both Vector and VectorBase, and replace them both with VectorBase.
Vector\(Base\)? → VectorBase
More complicated cases can be handled by using elisp in ...
7
votes
Accepted
Emacs replace-regexp reference the match in the replace argument
replace-regexp is perfect for the job. You could also use query-replace-regexp, which is more visual.
You want to replace XYZ\(...\) with 'XYZ\1'.
Explanation:
. stands for a single character, you ...
6
votes
Accepted
Replace word at point preserving the case pattern
The replace-match function is your best bet. It's a C primitive and that's where the case preservation logic is implemented.
replace-match replaces the text indicated by the match data. You can call ...
6
votes
Accepted
Arithmetics in a regex in a lisp function
Here's an example. See if I understood the problem correctly.
(defun colum-width-relative ()
""
(interactive)
(beginning-of-buffer)
(save-excursion
(while (re-search-forward "{\\([0-9]+\\)...
6
votes
How to replace matching parentheses?
I use the code below and bind yf/replace-or-delete-pair to M-D.
Example usage : with point on (, I hit M-D [ and the () pair becomes a [] pair. If you hit M-D RET instead, the pair will be removed.
...
6
votes
Accepted
How to remove empty lines of a selected region of code in Emacs?
Select what you want to change, or C-x h to select the whole buffer.
Then: M-x flush-lines RET followed by ^$ RET or ^[[:space:]]*$ RET
^[[:space:]]*$ contain the meta-characters:
^ for beginning of ...
5
votes
Search and replace on lines range, numerically
I'm not sure I understand what's so great about not highlighting the
lines first.
Anyway, selecting them is easy. The vi command :20,2003s/foo/bar/g<CR> would be something like: M-g M-g 20 RET ...
5
votes
Incrementally replace a given string
You can also use cua-mode.
Select the rectangle région (all the nn) and then M-x cua-rectangle-mark-mode.
Next, M-n and accept the default values.
5
votes
Accepted
Using a predefined Regex on a yank
Some of your parens were in the wrong place. I think you were expecting endp to be updated when you do the replace-match, but it has a fixed value, so I changed it to use copy-marker instead, which ...
5
votes
Accepted
Advanced search and replace
You are correct in that query-replace-regex (Ctrl+Meta+%) can do this. For the regular expression, you want \(fclose(f.)\); and your "to-string" you want if(\1!=0) return 1;
In the regular expression ...
5
votes
Accepted
Replacing selected text in Emacs with one character
Use C-M-% (query-replace-regexp) to replace the pattern . with + in the region (In Emacs regexp, . matches any characters except newline).
5
votes
Accepted
Search and replace TeX \over with LaTeX \frac
The command my-replace-over-with-frac defined in the following Elisp code assumes that \over arguments are always delimited by braces.
There are more constructs that limit the arguments of \over like ...
5
votes
Accepted
Search/replace and replace within a block of selected text of buffer in spacemacs
Vanilla Emacs has query-replace and related commands, that stop successively at each match for your search pattern and ask whether you want to replace it, then move on to the next match. When the ...
5
votes
Accepted
How can I search and replace in one go and for the full or selected text without visiting each search hit?
The command M-x replace-regexp does this. It's not bound to a key by default, so if you use it regularly you may want to do that.
You can also use M-x query-replace-regexp, which is bound to C-M-% by ...
4
votes
Accepted
Regular expression matching group replacement not working
The backslash character is special both in string literals (strings typed in Emacs Lisp, surrounded by double quotes) and in regular expressions as well as replacement specifications. Backslash ...
4
votes
how to query-delete-duplicate-lines?
It seems to be too difficult to make the original delete-duplicate-lines behave in the way you want. Here's something that might do the job though:
(defun my/update-lines (bunches pos keep)
(cl-...
4
votes
How to cancel the default in replace-string?
Search regexp \(\) is the exact same as hitting RET when trying to specify an empty string for searching.
As a quick test, using both \(\) and RET resulted in 833 search hits for this random text ...
4
votes
Accepted
Find/replace in differrent file extensions
xah-find uses emacs regular expressions (regexps).
Regexps support the or operator with \|. The elisp regexp \.java\|\.xml matches .java OR .xml. Furthermore there are \( and \) for grouping. ...
4
votes
Replace certain multibyte characters
Most likely your file is actually not using latin-1, yet your Emacs tries to decode it as if it were using latin-1. Try C-x RET r windows-1252 RET to see if it fixes your problem.
If it does, you'll ...
4
votes
Replacing string in a region and conditional replace
This might suffice:
(defun my-command (beginning end)
"Convert \"$$ ... $$\" lines in the region to an {align} block."
(interactive "*r")
(setq end (copy-marker end))
(save-match-data
(...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
replace × 194regular-expressions × 62
search × 43
query-replace × 21
string × 12
latex × 11
text-editing × 11
org-mode × 8
query-replace-regexp × 7
evil × 6
region × 6
auctex × 5
isearch × 5
newlines × 5
case-folding × 5
unicode × 4
characters × 4
tex × 4
numbers × 4
dired × 3
hooks × 3
elisp-macros × 3
keyboard-macros × 3
occur × 3
spacemacs × 2