Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"super star" or find the word under the cursor equivalent in emacs

I'm a vim user and have recently been trying out emacs for fun. I find that the feature I'm missing most so far from vim is the "super star" (find the word under the cursor by typing *) feature, and I have yet to find the equivalent in emacs. If it's not built in, what would I need to add to my emacs file to get something similar?

like image 664
mmrobins Avatar asked Nov 21 '09 09:11

mmrobins


4 Answers

As pointed out by paldepind, isearch-forward-symbol-at-point (M-s., by default) is a close equivalent to * in Vim. This function is available starting in GNU Emacs 24.4; if your Emacs is different or older, read on for alternatives.

Usually I just do (M-b ...) C-s C-w ... C-s. That is:

  1. M-b to move to beginning of word(s) of interest
    • zero or more of these
  2. C-s to start an I-Search
  3. C-w to yank the word(s) starting at point
    • one or more of these
  4. C-s to find the next match
  5. more C-s to find later matches
  6. RET to exit the I-search at the most recent match
    • or a bunch of C-g to abort back to the original starting location

Here is a go at integrating it into I-Search (invoked via C-s and C-r; use C-h k C-s for info on isearch).

(require "thingatpt")
(require "isearch")
(define-key isearch-mode-map (kbd "C-*")
  (lambda ()
    "Reset current isearch to a word-mode search of the word under point."
    (interactive)
    (setq isearch-word t
          isearch-string ""
          isearch-message "")
    (isearch-yank-string (word-at-point))))

Integrating it into I-Search takes advantage of its word matching and case sensitivity settings (C-s M-c C-* would do a case-sensitive search on the word under point).

like image 60
Chris Johnsen Avatar answered Nov 03 '22 05:11

Chris Johnsen


Try C-sC-w

like image 17
outis Avatar answered Nov 03 '22 05:11

outis


Here is a start:

(global-set-key (kbd "C-*")
  (lambda ()
    (interactive)
    (re-search-forward (format "\\b%s\\b" (thing-at-point 'word)))))
like image 10
huaiyuan Avatar answered Nov 03 '22 03:11

huaiyuan


These days there is also Smart Scan, a lightweight add-on package that provides this functionality.

It is available from MELPA; instructions for adding MELPA to the list of enabled package-archives are here.

To install it:

M-x package-install RET smartscan RET

You can then enable it via

(global-smartscan-mode t) ;; Turn on Smart Scan globally

The default key bindings for searching forward and backward are M-n and M-p, respectively.


PS: If you are interested, the original blog post introducing this package is here.

like image 7
itsjeyd Avatar answered Nov 03 '22 04:11

itsjeyd