In emacs incremental search, how can I cut the highlighted selection to the kill ring?

72 Views Asked by At

In Emacs's incremental search (C-s), C-w will append the next word to the search. I can then copy the highlighted text to the kill ring using M-w, but because C-w is in incremental-search mode and appending the next word, I can't use it to cut the highlighted selection to the kill ring.

Is there a quick way to exit incremental search but keep the search text highlighted at point so I can use C-w to kill it? Or an incremental search command I missed that will do this?

--- Update: From the comments I think my question wasn't totally clear. I'd like to be able to cut from the buffer any particular instance of the search string while doing an incremental search. So, C-s, C-w C-w to append words, C-s C-s to find an occurrence, then cut that instance. Or, just C-s C-w C-w, and then cut, to use isearch as a shortcut to highlight the text at point that I'd like to cut. Regarding the comments about M-e, that doesn't cut the text from the buffer, which was the heart of my original question - how to cut as opposed to copy.

3

There are 3 best solutions below

0
phils On BEST ANSWER
(define-key isearch-mode-map (kbd "M-s M-w") #'my-isearch-copy-match)
(define-key isearch-mode-map (kbd "M-s C-w") #'my-isearch-kill-match)

(defun my-isearch-copy-match ()
  "Copy the currently-matched text to the kill ring."
  (interactive)
  (kill-new (buffer-substring (min (point) isearch-other-end)
                              (max (point) isearch-other-end))))

(defun my-isearch-kill-match ()
  "Kill the currently-matched text."
  (interactive)
  (kill-region (min (point) isearch-other-end)
               (max (point) isearch-other-end)))

If you want these commands to automatically drop out of the isearch, then add a call to (isearch-exit) at the end of each function.

The M-sM-w key binding shadows eww-search-words in the global keymap, but it just seems like the correct binding for this command (and it's easy enough to drop out of isearch normally with RET if you ever want to call the eww command whilst isearching).

1
Drew On

Use library Isearch+ (isearch+.el), customize option isearchp-set-region-flag to ON (non-nil). When ON the search match is selected as the region when you exit Isearch. Then you can use C-w to kill it to the kill-ring.

You can use M-s M-SPC anytime during Isearch to toggle isearchp-set-region-flag ON/OFF.

isearchp-set-region-flag is a variable defined in isearch+.el.

Its value is nil

Documentation:

Non-nil means set region around search target.

This is used only for Transient Mark mode. You can toggle this using M-s M-SPC during Isearch.

You can customize this variable.


If you just want to copy the search string (not any particular match for it in the searched buffer) to the kill-ring, you can do that with M-w anytime during Isearch.

Without Isearch+ you can use M-e to edit the search string, and copy it to the kill-ring in that edit buffer.

4
Davis Herring On

You can just use M-e to edit the search string (which, modulo flexible matches for whitespace or accents or so, is the same as the matched string) and then kill that (e.g., with C-a C-k for single lines).