Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jplindstrom (594)

  (email not shown publicly)

Journal of jplindstrom (594)

Monday November 06, 2006
02:52 PM

Emacs: copy current isearch-string

[ #31527 ]

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."
    (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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • 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'.
    • Good point! I was looking for the equivalent of isearch-other-end but couldn't find anything among all the names.