Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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 ...
zck's user avatar
  • 9,102
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" "...
icarus's user avatar
  • 1,924
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)
Anntoin Wilkinson's user avatar
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 ...
NickD's user avatar
  • 30.5k
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,...
InHarmsWay's user avatar
  • 1,329
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 (...
link0ff's user avatar
  • 1,186
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's user avatar
  • 33.1k
8 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 ...
jue's user avatar
  • 4,576
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 ...
Gilles 'SO- stop being evil''s user avatar
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 ...
Stefan's user avatar
  • 26.5k
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 ...
phils's user avatar
  • 51.5k
7 votes

How do I search/replace with case sensitive search?

Use isearch (C-s), turn on case sensitivity (M-c) then launch replace (M-%) from there. M-c runs the command isearch-toggle-case-fold M-% runs the command isearch-query-replace
xuchunyang's user avatar
  • 14.6k
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 ...
Gilles 'SO- stop being evil''s user avatar
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]+\\)...
wvxvw's user avatar
  • 11.4k
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. ...
YoungFrog's user avatar
  • 3,526
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 ...
Nsukami _'s user avatar
  • 6,531
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 ...
Omar's user avatar
  • 4,832
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.
djangoliv's user avatar
  • 3,199
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 ...
rpluim's user avatar
  • 5,315
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 ...
nega's user avatar
  • 3,276
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).
xuchunyang's user avatar
  • 14.6k
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 ...
Tobias's user avatar
  • 33.3k
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 ...
Drew's user avatar
  • 77.9k
5 votes

Replace string in rectangular region?

C-x SPC turns on rectangle-mark-mode, activating the region as rectangular (noncontiguous). Then you can use M-% (query-replace) to replace any string with another, within only the rectangle. You are ...
Drew's user avatar
  • 77.9k
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 ...
Tyler's user avatar
  • 22.3k
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 ...
Gilles 'SO- stop being evil''s user avatar
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. ...
dgtized's user avatar
  • 4,209
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 ...
Stefan's user avatar
  • 26.5k
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 (...
phils's user avatar
  • 51.5k
4 votes

Regexp replace to match a string, but not match a superstring

One simple, very old-school way is to do multiple replacement passes: Replace VectorBase by, say AAAA (some string with chars you're sure don't already occur somewhere). Replace Vector by VectorBase. ...
Drew's user avatar
  • 77.9k

Only top scored, non community-wiki answers of a minimum length are eligible