30
votes
Accepted
Is there a way to use query-replace from grep/ack/ag output modes?
@Malabarba mentioned the use of wgrep package for editing grep/ack/ag results.
I would like to write a detailed walk-through of how I use the ag package and wgrep-ag packages to achieve editing of '...
17
votes
How to search and replace in the entire buffer?
I don't see that supported while still retaining your starting position. (I don't see a way to wrap to the beginning of the buffer when search reaches the end.)
Your best bet is using M-< to go to ...
15
votes
Is there a way to use query-replace from grep/ack/ag output modes?
What I would like to do is to either quickly build a dired buffer from existing grep output [...], or transform grep output into a list of buffers for multi-occur [...].
You don't need to convert the ...
15
votes
How to replace words in a region using iedit?
In Spacemacs once you are in iedit state you can:
press F to limit the scope to the current function
press L to limit the scope to the current line
press J to increase the scope (starting from the ...
15
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
How to search and replace in the entire buffer?
You can follow the following steps:
C-x h — Select the whole buffer or M-< - Go to the top of the buffer
M-% — Initiate query-replace
! — Force replace all
C-u C-SPC C-u C-SPC — Move back to your ...
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
Accepted
Regexp replacement: difference between \([[:digit:]]+\) and ([[:digit:]]+)
In emacs regular expressions (unlike most regexp engines), \( and \) are group delimiters, while ( and ) match litteral brackets.
So: \([[:digit:]]+\) matches one digit or more, that is here 123, and ...
11
votes
Accepted
Replace string in buffer programatically
Use functions re-search-forward and replace-match in a loop, or function perform-replace. See the Elisp manual, node Search and Replace.
Sample code:
(while (re-search-forward "hello" nil t)
(...
10
votes
Accepted
GROUP BY + COUNT over lines in a region
On Linux, and I assume Mac, you can pipe the region through the uniq shell command to get almost exactly what you want.
Mark the region
Sort the lines with M-x sort-lines
Call shell-command-on-region ...
10
votes
Accepted
How to save part of a regular expression during search and replace?
Use \(...\)for grouping and \1 to reference the first saved group (\2 for the second, all the way up to \9). E.g.:
query-replace-regexp: _\([^_]+\)_ into /\1/.
See Regexp Backslash in the Emacs Lisp ...
10
votes
Is there an equivalent to sed-style substitution commands from vim?
Here is one way of doing it that uses built-in functionality only:
With point in the line that contains first occurrence of begin, press C-SPC.
Move to next occurrence of end:
C-s end RET
Replace ...
10
votes
How to convert a series of lines into a working HTML list?
Alternatively to query replace you can go with multiple-cursors:
Also consider using something like this:
(defun wrap-html-tag (tagName)
"Add a tag to beginning and ending of current word or text ...
10
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
How to search and replace in the entire buffer?
You could add the following command to your emacs initialization file, and bind it to
the keystroke of your choice.
(defun replace-regexp-entire-buffer (pattern replacement)
"Perform regular-...
9
votes
Accepted
how to search and replace an entire word?
You don't need to define a separate function (command) for this. And even if you did define one, it need not use query-replace-regexp.
The standard command query-replace does just what you request, ...
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
How to convert a series of lines into a working HTML list?
You can use query-replace-regexp (C-M-%). Replace ^\(.*\)$ with <li>\1</li>.
8
votes
Repeat replacement until not possible?
It's worth learning how to use regular expressions to do things like this. Use M-x replace-regexp and replace " +" with " " (without the quotes). " +" means "one or more space". You are telling emacs ...
8
votes
Accepted
How to escape underscore in string
Your second example is actually correct. The resulting string contains a single backslash; it looks like a double backslash because it's printed as an Emacs Lisp string.
You can see this by ...
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,...
7
votes
Accepted
How to convert a series of lines into a working HTML list?
The most direct way of doing this would be to
Mark the region of text lines to become a list
Press M-% (query-replace)
Type C-q C-j RET </li> C-q C-j <li> RET (C-q C-j inserts a quoted ...
7
votes
Accepted
Search and replace with case preservation and plurals
Retaining the cases on replacing
By default, emacs retains the cases when replacing.
From the emacs manual,
When the newstring argument is all or partly lower case, replacement commands try to ...
7
votes
Accepted
Is there an equivalent to sed-style substitution commands from vim?
evil provides a stripped-down version of ex, so it's probably best to presume that it's not an exact drop-in. However, the example you provided works out of the box, provided that point is prior to ...

Dan♦
- 31k
7
votes
Accepted
Automatically replace C++ operators
You could define a dedicated input-method, unless you're already using one.
(quail-define-package "C++-operator" "C" "Cop")
(quail-define-rules
("==" ["_EQ_"])
("&" ["_BITAND_"])
("&&"...
7
votes
How to replace words in a region using iedit?
There are a couple ways to do this. (I'm using standard GNU Emacs, not familiar with Spacemacs.)
You can narrow the buffer to the region you are interested in and then use iedit to highlight / change ...
7
votes
Accepted
Processing of text
C-x h C-M-% \(.\{5\}\).\{3\}\(.*3.0e-4\) RET \1aaa\2 RET
Dissection:
C-x h select the whole file
C-M-% run query-replace-regexp (you may want M-x replace-regexp instead)
\(.\{5\}\) capture 5 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
replace × 165regular-expressions × 54
search × 39
query-replace × 21
text-editing × 10
latex × 9
string × 7
evil × 6
region × 5
isearch × 5
case-folding × 5
org-mode × 4
unicode × 4
characters × 4
newlines × 4
numbers × 4
query-replace-regexp × 4
auctex × 3
dired × 3
hooks × 3
elisp-macros × 3
occur × 3
spacemacs × 2
elisp × 2
window × 2