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,914
12 votes
Accepted

How to apply arithmetic operators to query-replace-regex

\2 in your replacement is a string, and it needs to be a number in order to perform the division. You could convert it to a number using string-to-number, but there's in-built shorthand for treating ...
rpluim's user avatar
  • 5,245
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
  • 29.8k
8 votes
Accepted

Find and remove consecutive duplicated words while ignoring case

You can query-replace-regexp as follows: \(\b\w+\b\)\W+\1\b → \1 This means, match a whole word (\b\w+\b), followed by non word characters (\W+), followed by the first word (\1) and a word ending (\...
Juancho's user avatar
  • 5,455
8 votes
Accepted

Query replace with different replacements

Assuming Bob is the first replacement: M-% Adam RET Bob RET y for all the consecutive Bob replacements e to change the replacement string, to, e.g.: Chris RET y for any further Chris replacements etc....
phils's user avatar
  • 51.2k
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.2k
6 votes
Accepted

Dired search and replace is throwing no results

As @phils said in a comment, Emacs 25.1 no longer uses dired-do-query-replace-regexp for Q. It uses dired-do-find-regexp-and-replace for Q. (It shouldn't, IMHO, but it does.) Try M-x dired-do-query-...
Drew's user avatar
  • 77.5k
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,521
6 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.5k
5 votes
Accepted

Toggle string and regex replacement

Like Harald, I'm not sure what kind of answer you expect. But FWIW, I think the best way to perform such operations is with: C-s <string1> M-% <string2> RET in which case the answer is ...
Stefan's user avatar
  • 26.4k
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,221
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.5k
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.5k
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-...
wvxvw's user avatar
  • 11.4k
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.5k
4 votes
Accepted

replacing regular expressions with querying on and functions on match

You might write the perform-replace version like this: (perform-replace "\\b\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]\\{4\\}\\)\\b" (cons (lambda (&rest _) (let ((data (list (string-to-number ...
phils's user avatar
  • 51.2k
4 votes
Accepted

How to count number of replacements made by `query-replace`?

As mentioned by @db48x, indeed, reporting a bug would be a good thing to do, as I guess that docstring should say "DID cancel the call". Also, there might be a small chance that the function ...
dalanicolai's user avatar
  • 7,805
3 votes

Help with `replace-regexp-in-string`, to replace only first instance in every line

C-hf replace-regexp-in-string actually covers this exact requirement (or it does in Emacs 25.3, at any rate). To replace only the first match (if any), make REGEXP match up to \' and replace a sub-...
phils's user avatar
  • 51.2k
3 votes

Query-replace-regexp alternative capable of handling nested braces?

This is a situation where I'd prefer to use a macro. Start your macro by searching forward to the beginning of your match, then perform generalized actions using methods like forward|backward-sexp, ...
Jordon Biondo's user avatar
3 votes

query-replace-regexp on specific lines

If you want to add a prefix of hello to all lines which start with a letter, you can use C-M-% ^\([a-zA-Z]\) hello \1 The idea is to use \(...\) to capture stuff and \1 to represent the first ...
icarus's user avatar
  • 1,914
3 votes
Accepted

Passing a string with substitutions to the "perform-replace" command

The way backslashes work is logical but you have to keep careful track of how many levels of backslash expansion are going on. When you use "\\." as a regular expression, you're starting from the ...
Gilles 'SO- stop being evil''s user avatar
3 votes

Difficulty with query-replace-regexp

To replace space and/or tab sequence followed by an asterisk to just an asterisk: [ LiteralTabHere]+\* * That LiteralTabHere should be a literal tab char. Enter it by pressing C-q C-i. You need the ...
Xah Lee's user avatar
  • 1,766
3 votes

How do I get the old query-replace up-arrow functionality back?

If you upgrade to 26.1 you can set query-replace-from-to-separator to nil 25.1 NEWS: *** 'query-replace' history is enhanced. When 'query-replace' reads the FROM string from the minibuffer, ...
phils's user avatar
  • 51.2k
3 votes
Accepted

What mode does the command `query-replace' activate?

query-replace is just a command, it is not a major or minor mode. Before the phase in which it prompts for what to do with each match, it has no special bindings and uses minibuffer-local-map (this is ...
Omar's user avatar
  • 4,812
3 votes
Accepted

Text substitution only in specific column of org table

You can select the column as a rectangular region using C-x SPC (rectangle-mark-mode) and then M-% (query-replace) will be limited to that rectangle.
Omar's user avatar
  • 4,812
3 votes

Query replace a copied string by another copied one

Avoiding window switching: Copy string Copy newstring M-%C-yM-yRET C-yC-u-1M-yRET Or if you can get used to copy newstring before string: Copy newstring Copy string M-%C-yRET C-yM-yRET And another ...
muffinmad's user avatar
  • 2,300
3 votes
Accepted

Make region(s) invisible (not evaluated) to query-replacy and similar commands

Yes. Set variable isearch-filter-predicate to a function that returns nil for the text that you want to ignore for search and query-replace. (Set it back to its default value of isearch-filter-...
Drew's user avatar
  • 77.5k
3 votes
Accepted

How to perform query-replace inside mathematical expressions in latex

As suggested by @Drew, this can be achieved by setting isearch-filter-predicate appropriately. Indeed, the docstring for query-replace includes: C-h f query-replace ... Ignore read-only ...
JeanPierre's user avatar
  • 7,465
3 votes
Accepted

Can Dired query replace use regular expression to rename a group of files?

I do M-% to start the regex file rename but it does not work. M-% runs the command query-replace C-M-% runs the command query-replace-regexp I see that it does not like backslash as an escape ...
phils's user avatar
  • 51.2k
3 votes

Automatically save files after multi-file search/replace

You could trigger save-buffer after the replace. I believe the following should work: (add-hook 'replace-update-post-hook 'save-buffer)
aadcg's user avatar
  • 1,248

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