C-s for sequential searching. When I want to delete the current match, backspace will go to the previous match instead. How can I delete the current match?
M-x query-replace is one of the way to achieve what you are looking for. Just replace by an empty string.
If you want to delete the current match and nothing else, the following would do:
(defun isearch-kill-current () (interactive) (delete-region isearch-other-end (point))) (define-key isearch-mode-map (kbd "<deletechar>") 'isearch-kill-current)
It doesn't interrupt the search, just deletes the currently selected match and lets you proceed with your next forward
C-s or anything similar.
Isearch has no built-in command to act on the current match as a unit.
You can press
RET to stop searching and edit at the current point location, which is the end of the current match (or the beginning if searching backwards). But there's no automatic way to go to the beginning of the match.
The following little function exits isearch, like
RET, and additionally sets the mark at the beginning of the match (the point is at the end; swap beginning and end when searching backwards). I demonstrate how to assign it to
(defun isearch-mark-current-match-and-exit () (interactive) (isearch-exit) (push-mark isearch-other-end)) (define-key isearch-mode-map [?\M-m] 'isearch-mark-current-match-and-exit)
Once you're done editing, you can resume the previous search by pressing
Alternatively, to delete some occurrences, replace them by the empty string. As suggested by Nsukami _, run
query-replace, enter the string to search, then enter the empty string as a replacement. Type
n for each match depending on whether you want to delete it or keep it.
If you started from incremental search, you can switch to a
query-replace operation by pressing
M-% during the incremental search. The current isearch string will be used as the string to replace.
To expand on the last paragraph of @Gilles' answer:
You can turn an ongoing
isearch into a
- As you’re doing
isearch, if you decide you would like to erase some entries, simply hit
M-%(which is Emacs’s binding for
query-replace) and it will transform you ongoing
isearchinto query replace.
- The current search term will remain the same and you will be prompted for a replacement.
RETfor no replacement.
yon the the matches you want to erase, and
non those you don’t.
It will start asking at current match, so it’s a great tool to use midway through an
If you use library Isearch+ then you can do this:
C-SPCwhile searching, to tell Isearch to select the last search hit as the region.
C-SPCactually toggles option
isearchp-set-region-flag, which when non-
nilselects the last search hit visited.
C-x owhile searching, to pause Isearch; do whatever you want (e.g. replace the selected last search hit); and then hit
C-M-cto resume Isearch.
C-x oopens a recursive editing session, where you can do anything you like (including search for something different). Using
C-M-ccloses the recursive editing session and resumes the search (from the current position where you hit
C-M-RETto perform a specified action on any search hit you visit. Just repeat
C-M-RETto continue visiting and acting on subsequent search hits.
The default action is to replace the search hit. This means you can use
C-M-RETto replace search hits on demand. Provide a prefix arg to
C-M-RETat any time to change the replacement string (the default is "", which just deletes the search hit). This means you can provide different replacements for different search hits.
With a negative prefix arg,
C-M-RETchanges searching itself (e.g.
C-s) so that it automatically replaces whenever a search hit is visited. With a positive prefix arg N, you replace the next N search hits. With a zero prefix arg (e.g.
C-0), you replace all remaining search hits.
(To use a prefix arg with Isearch you must set option
On-demand replacement (with
C-M-RET) can use
query-replace-regexp-style replacement. That is, you can use
\?. You can use
C-M-`while searching to toggle whether such constructs should be interpreted or taken literally (as plain text).
See the Isearch+ doc for more about replacement during Isearch.