3

while isearch-forward run as a command, the context I had typed will hightlight in the current buffer, but while run query-replace don't hightlight that, how can i make it hightlight?

1
  • You can try anzu or visual-regexp, I'm using the former, it not also highlights matched string, but also previews replaced string. Commented Jun 12, 2015 at 15:06

3 Answers 3

2

Use isearch-query-replace. It highlights the string to be replaced.

Sign up to request clarification or add additional context in comments.

Comments

1
  1. It sounds like you are saying that query-replace does not highlight all of the matching occurrences. Is that right? It should highlight them. If it does not, then try starting Emacs without your init file: emacs -Q. If that shows no lack of highlighting then recursively bisect your init file to find the culprit.

  2. @Rocky mentioned isearch-query-replace. That doesn't change highlighting (which should already be turned on), but what it does do is let you start query-replacing while you are isearching, using the last search string as the pattern for the text to be matched by query-replace.

  3. An alternative to query-replace, useful especially if you have relatively few replacements you want to make and there are lots of matches, is to use on-demand replacement while isearching. For that you need library Isearch+.

    To replace any given search hit on demand, just hit C-M-RET. With a prefix arg, C-M-RET prompts you for the replacement text (the default is to replace with no text, which means to delete the hit). You can thus change the replacement text anytime, within the same Isearch invocation.

    After replacing the search hit, C-M-RET moves to the next one. So you can just use it repeatedly if you want to replace several successive search hits. Or use C-s to skip replacing the current hit and move to the next one.

    On-demand Isearch replacement works also for regexp searching, and just as for query-replacing, the replacement text can be either inserted literally, as is, or interpreted as in query-replace-regexp. In the latter case, you can use \&, \=\N, \#, \, and \?. You can use C-M-` anytime during Isearch to toggle whether replacement text is used literally or interpreted per the special regexp-replacement constructs.

10 Comments

I mean while I typing the pattern text which will be replaced later, query-replace don't hightlight the matching occurrences, it just hightlight the matching after the replaced text and replace text both input and an "Enter" typed.
OK, fair enough. Yes, query-replace doesn't highlight the search hits until you have entered both search and replacement patterns. Consider filing an enhancement request for it: M-x report-emacs-bug. But both Isearch followed by M-% (isearch-query-replace) and Isearch+ replacement-on-demand (C-M-RET) show you matches from the outset, and incrementally as you type.
Yes, Isearch followed by isearch-query-replace do this, but for query-replace-regexp that not work. Thanks a lot, i'll report a bug later.
Regexp Isearch followed by M-% works for me. Consider showing an example (in your question) of what you feel does not work wrt query-replace-regexp initiated from isearch.
Regexp Isearch followed by isearch-query-replace-regexp works right. But the cursor not jump to the next hightlight matching while input 'N', it just jump to next char as 'forward-char' do. I'm using "GNU Emacs 24.4.1 (i686-pc-mingw32)\n of 2014-10-25 on LEG570".
|
1

The following packages provide live highlighting and replacement previewing for query replacing, as well as additional features:

I currently use visual-regexp-steroids.el.

All three packages can be installed from MELPA.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.