ZapToISearch

Just like zapping to a character is a fast way to delete text, zapping to a word or regexp found by isearch is equally powerful (isearch is a fast way to move around). An important issue that arises when using isearch to delete text is that most of the time the user has to exit isearch-mode (RET), manually move to the beginning of the (now defunct) isearch pattern (potentially multiple keys) and then type C-w. This is because the majority of the time, the expression that is being searched for is not part of the region to be deleted (this is entirely empirical by the way, but it makes sense too: many times we delete large regions of text between words).

Similarly to the problem of ZapUpToChar, it would be useful to have a function available in isearch mode to automatically delete the active region EXCEPT for the search pattern. I built this function, zap-to-isearch for this purpose, to be bound in isearch mode:

  (defun zap-to-isearch (rbeg rend)
    "Kill the region between the mark and the closest portion of
  the isearch match string. The behaviour is meant to be analogous
  to zap-to-char; let's call it zap-to-isearch. The deleted region
  does not include the isearch word. This is meant to be bound only
  in isearch mode.
  The point of this function is that oftentimes you want to delete
  some portion of text, one end of which happens to be an active
  isearch word. The observation to make is that if you use isearch
  a lot to move the cursor around (as you should, it is much more
  efficient than using the arrows), it happens a lot that you could
  just delete the active region between the mark and the point, not
  include the isearch word."
    (interactive "r")
    (when (not mark-active)
      (error "Mark is not active"))
    (let* ((isearch-bounds (list isearch-other-end (point)))
           (ismin (apply 'min isearch-bounds))
           (ismax (apply 'max isearch-bounds))
           )
      (if (< (mark) ismin)
          (kill-region (mark) ismin)
        (if (> (mark) ismax)
            (kill-region ismax (mark))
          (error "Internal error in isearch kill function.")))
      (isearch-exit)
      ))
  (define-key isearch-mode-map [(meta z)] 'zap-to-isearch))

Another very useful binding for isearch, that is very close to that, is one that exits isearch but leaves point (and thus the region) at the “other end” of the isearch match:

  (defun isearch-exit-other-end (rbeg rend)
    "Exit isearch, but at the other end of the search string.
  This is useful when followed by an immediate kill."
    (interactive "r")
    (isearch-exit)
    (goto-char isearch-other-end))
  (define-key isearch-mode-map [(control return)] 'isearch-exit-other-end)

See also: KillISearchMatch

--MartinBlais