2022 Developer Survey is open! Take survey.

Hot answers tagged

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 '...
user avatar
  • 24.2k
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 ...
user avatar
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 ...
user avatar
  • 22.3k
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 ...
user avatar
  • 2,055
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 ...
user avatar
  • 8,536
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 ...
user avatar
  • 24.2k
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" "...
user avatar
  • 1,829
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 ...
user avatar
  • 4,093
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) (...
user avatar
  • 69.2k
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 ...
user avatar
  • 20.1k
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 ...
user avatar
  • 13.6k
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 ...
user avatar
  • 14.2k
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 ...
user avatar
  • 506
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)
user avatar
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-...
user avatar
  • 1,316
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, ...
user avatar
  • 69.2k
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 ...
user avatar
  • 19.6k
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>.
user avatar
  • 4,950
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 ...
user avatar
  • 1,472
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 ...
user avatar
  • 5,786
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 (...
user avatar
  • 1,006
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,...
user avatar
  • 1,259
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 ...
user avatar
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 ...
user avatar
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 ...
user avatar
  • 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_"]) ("&&"...
user avatar
  • 3,276
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 ...
user avatar
  • 19.1k
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 ...
user avatar
  • 9,993
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 ...
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 ...
user avatar
  • 25.2k

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