0

I used to have the below function from https://www.emacswiki.org/emacs/SearchAtPoint#h5o-6 that allows to search for symbol under the cursor by typing C-s C-w, and by typing C-w, I would append the following symbol to the search and so on. This was so convenient. Somehow, after updating emacs, things stopped working. Also, thingatpt is not available from MELPA but required here.

Any idea how to fix that or if there is an alternative?

;; Move to beginning of word before yanking word in isearch-mode.
;; Make C-s C-w and C-r C-w act like Vim's g* and g#, keeping Emacs'
;; C-s C-w [C-w] [C-w]... behaviour.

(require 'thingatpt)

(defun my-isearch-yank-word-or-char-from-beginning ()
  "Move to beginning of word before yanking word in isearch-mode."
  (interactive)
  ;; Making this work after a search string is entered by user
  ;; is too hard to do, so work only when search string is empty.
  (if (= 0 (length isearch-string))
      (beginning-of-thing 'word))
  (isearch-yank-word-or-char)
  ;; Revert to 'isearch-yank-word-or-char for subsequent calls
  (substitute-key-definition 'my-isearch-yank-word-or-char-from-beginning 
                 'isearch-yank-word-or-char
                 isearch-mode-map))

(add-hook 'isearch-mode-hook
 (lambda ()
   "Activate my customized Isearch word yank command."
   (substitute-key-definition 'isearch-yank-word-or-char 
                  'my-isearch-yank-word-or-char-from-beginning
                  isearch-mode-map)))
Drew
  • 75,699
  • 9
  • 109
  • 225
  • 1
    `thingatpt` is part of Emacs, so you don't need to get it from anywhere. I checked Emacs 28 and 29, but the doc string of `thing-at-point` says it's probably been there since Emacs 20.1, so I suspect it's there in earlier versions. What version of Emacs are you using? – NickD Nov 24 '22 at 15:16
  • Bisect your init file to find the culprit. – Drew Nov 24 '22 at 16:51
  • @NickD I have Emacs 28.2. The function `beginning-of-thing` doesn't seem to be part of `thing-at-point`. – klebsiella Nov 25 '22 at 08:03
  • It is. `C-h f beginning-of-thing` gives me `beginning-of-thing is a native compiled Lisp function in ‘thingatpt.el` in both 28 and 29. – NickD Nov 25 '22 at 14:53
  • Indeed. As I switched to Swiper lately, isearch is not the default. Also, `C-s M-j` from Swiper does the same job. – klebsiella Nov 28 '22 at 12:16

0 Answers0