3

I want isearch-forward-symbol-at-point to also save to the kill-ring. How can I accomplish that please?

1 Answer 1

2

Try this.

(defun my-isearch-forward-symbol-at-point ()
  "`isearch-forward-symbol-at-point', but copy symbol name to `kill-ring'."
  (interactive)
  (isearch-forward-symbol nil 1)
  (let* ((bounds  (find-tag-default-bounds))
         (string  (and bounds  (buffer-substring-no-properties
                                 (car bounds) (cdr bounds)))))
    (cond
     (string
      (kill-new string)
      (when (< (car bounds) (point))(goto-char (car bounds)))
      (isearch-yank-string string))
     (t
      (setq isearch-error "No symbol at point")
      (isearch-push-state)
      (isearch-update)))))

(define-key isearch-mode-map "." 'my-isearch-forward-symbol-at-point)
(define-key search-map       "." 'my-isearch-forward-symbol-at-point)

All I did was copy the definition of isearch-forward-symbol-at-point and insert a call to kill-new.

4
  • thanks but how is this to be invoked? after hitting C-s and then . isearch just searches for . occurences. I would rather have the copying as a side effect of hitting M-w .
    – CD86
    Commented Jan 10, 2023 at 8:33
  • Sorry, I pasted the wrong code I forgot the (kill-new string), which puts the string into the kill-ring. Please try the updated code.
    – Drew
    Commented Jan 10, 2023 at 16:42
  • thank you, but how can I automatically invoke this when pressing M-s . isearch-forward-symbol-at-point ? I dont want to have to press C-s .
    – CD86
    Commented Jan 10, 2023 at 20:10
  • I added the binding to search-map as well. You can also add it to query-replace-map...
    – Drew
    Commented Jan 10, 2023 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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