Nicolas Petton

Nicolas Petton

Web developer, Lisper, Smalltalker & Emacs maniac.

isearch thing

Wouldn't it be awesome if when using isearch, I could search for the symbol under the cursor, or the active region if any?

Unfortunately, AFAIK isearch can search for the next word (with C-w), but not the symbol at point or the region contents. This small snippet fixes the problem!

Note: it overrides C-t in isearch-mode, but as I never use it, I do not mind :)

(defun symbol-name-at-point ()
  (let ((symbol (symbol-at-point)))
    (if symbol
        (symbol-name symbol)
      "")))

(defun current-thing ()
  "Return the current \"thing\":
- if the region is active, return the region's text and deactivate the mark
- else return the symbol at point or the empty string."
  (let ((thing (if (region-active-p)
                   (buffer-substring (region-beginning) (region-end))
                 (symbol-name-at-point))))
    (deactivate-mark)
    thing))

(defun isearch-thing ()
  "Search for the current \"thing\":
- if the region is active, return the region's text and deactivate the mark
- else return the symbol at point or the empty string."
  (interactive)
  (isearch-yank-string (current-thing)))

(define-key isearch-mode-map (kbd "C-t") #'isearch-thing)
comments powered by Disqus