Hot answers tagged

28

@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 'ag'ged results using multiple-cursors package. These packages are available through Melpa. You also need to have ag aka the_silver_searcher installed on your ...


15

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 current line) one line below press K to increase the scope one line above navigate between the occurrences with n and N and press TAB to remove an occurrence.


14

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 the start of the buffer, then do the query-replace, when you're done press C-uC-spaceC-uC-space to jump back to your starting point.


13

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 grep results buffer into anything, there's already a mode specifically for editing grep output (and reflecting the changes on the files, of course). Install ...


12

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 expression used as part of the replacement text. Inside of that expression, ‘\&’ is a string denoting the whole match, ‘\N’ a partial match, ‘#&’ and ‘...


11

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" "string1") If the replacement text contains \, followed by a lisp expression, it uses the value of that expression as the replacement. In this case the expression is ...


10

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 makes it a group. That means that for example, \([[:digit:]]+\)? would match either 123 or some empty string, and that you can use \1 (assuming it is your only ...


10

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 with the prefix key: C-u M-| Enter uniq --count The contents of the buffer will be replaced by: 3 THIS IS LINE A 2 THIS IS LINE B 1 THIS IS LINE C You ...


10

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 selection." (interactive "sEnter tag name: ") (let (p1 p2 inputText) (if (use-region-p) (progn (setq p1 (region-beginning) ) ...


9

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 starting position


9

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-expression replacement throughout buffer." (interactive (let ((args (query-replace-read-args "Replace" t))) (setcdr (cdr args) nil) ; remove third value ...


9

You can use query-replace-regexp (C-M-%). Replace ^\(.*\)$ with <li>\1</li>.


9

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 Manual for more.


9

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) (replace-match "world"))


9

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, variants of this command.


9

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 syntax table; in programming language modes it's meant to be what the language treats as a symbol or identifier. You can also use \<Vector\> or \bVector\...


8

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 foo with bar: M-% foo RET bar RET ! This makes use of the fact that query-replace will work on the active region instead of the whole buffer if there is one. ...


8

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 to replace all instances of one or more space with a single space. Finally, if you do end up learning to use regular expressions, I recommend using https://...


8

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, if you provide it with a prefix argument. So just use C-u M-% to query-replace words. C-h f query-replace tells you this (note the part in bold): query-...


7

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 the first line (ie, the begin line in your example).


7

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 newline character) Press ! to replace all occurrences


7

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_"]) ("&&" ["_AND_"]) ("|" ["_BITOR_"]) ("||" ["_OR_"]) ("!" ["_NOT_"]) ("!=" ["_NOT_EQ_"])) Activating it with C-u C-\ C++-operator RET. Note that you can still ...


7

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 converting the string into a list of one-character strings: (mapcar 'string (replace-regexp-in-string "_" "\\_" "file_01.jpg" t t)) This returns: ("f" "i" "l" "e" "\\...


7

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 characters into the group \1. .\{3\} match three characters (will discard) \(.*3.0e-4\) match the rest of the line, ensuring 3.0e-4 and capture in \2


7

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 or query-replace-regexp. But you don't need a macro at all to perform the same replacement in several files. Open the directory containing the files in Dired (...


7

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 describing. You'd use el-search-query-replace and provide the pattern `(set (make-local-variable ',var) ,val) and replace it with `(setq-local ,var ,val)


7

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 (let ((common (query-replace-read-args (concat "Query swap" (if current-prefix-arg (if (eq current-...


7

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 the replacement. For example, the following replaces "Vector" with "Array" unless it was "VectorBase", in which case it 'keeps' it as "VectorBase" (i.e. replaces ...


6

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 preserve the case pattern of each occurrence. Thus, the command M-x replace-string <RET> foo <RET> bar <RET> replaces a lower case foo ...


6

Emacs macros playback everything the user enters over the course of the macro, unless they escape the macro. The way to escape a macro in progress is with C-x q kbd-macro-query. Specifically I believe you need to enter recursive edit, which can prompt for a command in minibuffer before continuing the macro. I think you want to define your query-replace ...


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