Emacs: copy current isearch-string

jplindstrom on 2006-11-06T19:52:37

In my .emacs I have this neat piece of code which I found on PerlMonks a while ago. It allows me to list all occurrences of the current isearch-string by typing Ctrl-o while searching.

I-search is really, truly useful (and along with having dabbrev-expand bound to Alt-Space is something I'd like to have working in all programs I use). The function above is a nice twist.

One thing I've wanted for a long time though is to be able to I-search (using Ctrl-w to expand the search string until I found the proper match) and then copy the string into the clipboard (or killing it in Emacs-speak).

I wouldn't be surprised if it's possible to actually do that already, but I haven't found anything after quite a bit of searching. So I hacked together this by adapting the occurence code:

;; Kill from isearch (defun isearch-kill-string () "Make the current isearch-string the latest kill in the kill ring." (interactive) (let ((case-fold-search isearch-case-fold-search)) (kill-new (if isearch-regexp isearch-string (regexp-quote isearch-string)))))

(define-key isearch-mode-map (kbd "C-k") 'isearch-kill-string)
Feel free to code review the elisp, I'm sure it can be improved in one way or the other.


intent?

educated_foo on 2006-11-07T00:50:07

It seems strange to me that you're pulling the isearch string (and quoting it) rather than the matching buffer text. Is that what you really want to do? If not, I think you could instead snap up the text between '(point)' and 'isearch-other-end'.

Re:intent?

jplindstrom on 2006-11-07T10:59:01

Good point! I was looking for the equivalent of isearch-other-end but couldn't find anything among all the names.

Thanks!