We’re rewarding the question askers & reputations are being recalculated! Read more.
13

(customize-set-variable case-fold-search nil) Or bind that variable (option) in your own command that is otherwise just a wrapper around query-replace. This has the advantage that it doesn't change the variable value for general use, outside of query-replacing. See also variable case-replace, which controls case for the replacement text. See also C-h f ...


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 ...


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

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... When you want to re-use one of the previous replacements (say, Adam again) e and then M-p or <up> to cycle back through the replacement history


8

\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 a capture group as a number; so you just need to change \2 to \#2


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

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 (\b). Replace with the first word only. Case sensitivity is handled by options case-fold-search and case-replace.


5

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-replace-regexp. If that works, you can just bind it to Q to have Q act the same as it used to.


5

Let's say you already did a query-replace of thing1 with thing2. Here is how it goes if you want to use thing1 as the first argument again. It is not automatic but you don't have to type in the whole thing1 either. The benefit of this approach is more more when doing query-replace-regexp and you need to repeat a very complex regexp. Call query-replace or ...


5

Customize option query-replace-from-history-variable (or query-replace-to-history-variable, or both). Point to search-ring or regexp-search-ring instead of query-replace-history.


5

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 that you can use M-r any time between the C-s and the M-% to toggle between string search and regexp search. You can similarly use M-c to toggle the case-...


4

In Emacs Lisp string literals, backslash is an escape character. To express a single literal backslash character, as required by many regular expression constructs, you need two backslashes, eg: "\\([[:nonascii:][:ascii:]]*\\)xxx" You don't need the extra backslashes when you run query-replace-regexp interactively because you're not entering a string ...


4

Use replace-regexp (note the absence of the query- prefix). But also, note the ! keybinding when running query-replace-regexp. It will replace all occurences beyond point without further questions.


4

Here is a snippet to do it: (defun my-global-ex-search () (interactive) (let ((evil-ex-substitute-global t)) (evil-ex "%s/"))) (define-key evil-normal-state-map "S" 'my-global-ex-search) The idea is to define an interactive function (can be called by the user) to call evil-ex with the initial input and evil-ex-substitute to t. Then we bind this ...


4

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-loop with dec = (if keep 0 1) for line being the hash-key of bunches using (hash-value positions) do (puthash line ...


4

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 we create a group with \( and \). That group consists of fclose(f.). The . represents "any character". The "to-string" contains \1 This is a backreference to ...


4

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. Replace AAAA by VectorBase. This works for replace-all and query-replace. It's pretty fail-safe and doesn't require any complex matching or fancy replacement ...


4

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 (match-string 1)) (string-to-number (match-string 2)) (string-to-number (match-string 3))))) (...


3

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 backslash before asterisk. Because the asterisk is a meta char, it means 0 or more items. To insert a space before the first non-white-space character after 2 ...


3

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 captured group. Here I am using [a-zA-Z] to represent a single letter using the normal unix conventions. It would be better if I said [[:alpha:]] instead. If you want ...


3

You do not even need to place the cursor on that {. Using the built-in query-replace-regexp command (bound to C-M-% by default), you can do that \color{red}\sout{...} replacement either one-by-one by reviewing each replacement or in one fell swoop. Here's how to do those replacements in the whole document instantly. Go to the beginning of the document by ...


3

Use "\" to quote "\": (defun my-replace () (interactive) (query-replace-regexp "\\([[:nonascii:][:ascii:]]*\\)xxx" "\\1replacedText" nil (point-min) (point-max)))


3

Try C-M-% instead of M-%. You want query-replace-regexp, not query-replace.


3

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 string literal "\\.". This string literal evaluates to the string object \., a two-character string. That's because in a string literal, a backslash quotes the ...


3

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-expression, e.g. (replace-regexp-in-string "\\(foo\\).*\\'" "bar" " foo foo" nil nil 1) => " bar foo" To replace the first match (if any) in each line of ...


3

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, typing 'M-p' will now show previous replacements as "FROM SEP TO", where FROM and TO are the original text and its replacement, and SEP is an arrow string ...


3

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 option is to write function which will call query-replace with most recent kill ring entries and bind it to some key. Then just: Copy string Copy newstring ...


3

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-visible when you're done. Alternatively, define your own search or query-replace function that binds the variable and then invokes regular Isearch or query-replace. ...


2

Please try in GNU Emacs 24.4 or 24.5 where it should work as you want.


2

I'm guessing that what you want to do is this: Find all files whose names match *.mod and whose contents contain the word solve. Replace the word solve in each of those files with the name of the file. If so, one answer might be to use Q in Dired, searching for solve, and to use a replacement expression such as \,(buffer-file-name), to replace solve with ...


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