1

I use 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?

  • What do you mean by "delete a match"? Do you want to delete the buffer text that is found? If so, and if you want to be queried about whether to delete each such match after the cursor, then use query-replace(-regexp), not Isearch. – Drew Sep 30 '14 at 22:08
4

It seems, M-x query-replace is one of the way to achieve what you are looking for. Just replace by an empty string.

  • is it sequential? I need to check if I want to delete each match. – Tim Sep 30 '14 at 21:50
  • @Tim: it is probably faster to try it out than to wait for a response. ;-) But yes, it is sequential. It will run from POINT to the end of the buffer, stopping at each match in sequence. – nispio Sep 30 '14 at 22:08
  • You call M-x query-replace, then you give the string you want to replace, the replacement string, and you hit enter. you'll have to confirm by 'y' for each occurence found. Hitting '!' will replace it all, without asking for confirmation. Why not give it a try? :) – Nsukami _ Sep 30 '14 at 22:09
  • 1
    @Tim You should read the documentation sometimes. – Gilles 'SO- stop being evil' Sep 30 '14 at 22:10
  • 1
    You can directly jump from isearch to query-replace with M-%, and the from string will be filled with the search term. – Juancho Oct 1 '14 at 18:37
3

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.

2

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

(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 C-s C-s.

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

2

To expand on the last paragraph of @Gilles' answer:

You can turn an ongoing isearch into a query-replace search.

  1. 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 isearch into query replace.
  2. The current search term will remain the same and you will be prompted for a replacement.
  3. Hit RET for no replacement.
  4. Hit y on the the matches you want to erase, and n on those you don’t.

It will start asking at current match, so it’s a great tool to use midway through an isearch.

  • Gilles actually included that as the last paragraph of his answer, albeit in briefer form. – Jonathan Leech-Pepin Oct 1 '14 at 13:15
  • @JonathanLeech-Pepin True! I missed that. – Malabarba Oct 1 '14 at 14:16
2

If you use library Isearch+ then you can do this:

  1. Hit C-SPC while searching, to tell Isearch to select the last search hit as the region.

    C-SPC actually toggles option isearchp-set-region-flag, which when non-nil selects the last search hit visited.

  2. Hit C-x o while searching, to pause Isearch; do whatever you want (e.g. replace the selected last search hit); and then hit C-M-c to resume Isearch.

    C-x o opens a recursive editing session, where you can do anything you like (including search for something different). Using C-M-c closes the recursive editing session and resumes the search (from the current position where you hit C-M-c).

  3. Use C-M-RET to perform a specified action on any search hit you visit. Just repeat C-M-RET to continue visiting and acting on subsequent search hits.

    The default action is to replace the search hit. This means you can use C-M-RET to replace search hits on demand. Provide a prefix arg to C-M-RET at 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.

  4. With a negative prefix arg, C-M-RET changes 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 isearch-allow-prefix or isearch-allow-scroll to non-nil.)

  5. On-demand replacement (with C-M-RET) can use query-replace-regexp-style replacement. That is, you can use \&, \=\N, \#, \,, and \?. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.