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?
how could I using query-replace like isearch-forward while typing something
434 views Asked by Redefl AtThere are 3 answers
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.@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 byquery-replace
.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 useC-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 useC-M-`
anytime during Isearch to toggle whether replacement text is used literally or interpreted per the special regexp-replacement constructs.
The following packages provide live highlighting and replacement previewing for query replacing, as well as additional features:
- https://github.com/syohex/emacs-anzu
- https://github.com/benma/visual-regexp.el
- https://github.com/benma/visual-regexp-steroids.el
I currently use visual-regexp-steroids.el
.
All three packages can be installed from MELPA.
Use
isearch-query-replace
. It highlights the string to be replaced.