34

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?

3
  • 1
    i didn't know about that in VIM. Cool :) Nov 21, 2009 at 9:17
  • 3
    possible duplicate of stackoverflow.com/questions/589691/… Apr 2, 2010 at 17:50
  • 4
    I agree it's a duplicate, but it's easier to find for a vim user who knows the command as super star. The other answer never mentions the word 'star' which made it hard for me to find.
    – mmrobins
    Apr 2, 2010 at 20:56

6 Answers 6

42

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).

4
  • Any way to put all this piano combination into some single keypress more like in vim ?
    – Dfr
    Oct 2, 2014 at 12:33
  • 3
    What about using isearch-forward-symbol-at-point which by default is bound to C-s .
    – paldepind
    Nov 2, 2015 at 21:12
  • @paldepind: That looks nice; NEWS indicates that it originated in GNU Emacs 24.4 (October 2014). Nov 3, 2015 at 6:27
  • @ChrisJohnsen Yes. It's somewhat recent. I think updating the answer would be great!
    – paldepind
    Nov 3, 2015 at 14:15
17

Try C-sC-w

1
  • 12
    This is an incorrect answer. It assumes the point is at the start of the word while super star feature in Vim can be used from anywhere within the word. Feb 7, 2014 at 11:52
10

Here is a start:

(global-set-key (kbd "C-*")
  (lambda ()
    (interactive)
    (re-search-forward (format "\\b%s\\b" (thing-at-point 'word)))))
7

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.

4

if you're using viper (which works great) this should work similarly enough:

(require 'thingatpt)
(require 'viper)

(defun viper-search-word-at-point ()
  "Reset viper-s-string to word at point and start a forward viper search."
    (interactive)
    (setq viper-s-string (word-at-point))
    (viper-search viper-s-string 't nil))

(global-set-key (kbd "C-*") 'viper-search-word-at-point)
0
4

C-s C-w is ok but it's not strictly symbol search, but "word part at the right of point" search.

The correct answer is M-s . This starts symbol search with the symbol at point preloaded. Symbol search is a special mode of isearch that you can start with M-s _. If you're already in isearch (for example, after pressing C-s or C-r) you can also type M-s _ to enter symbol search.

I urge you to read the entire documentation of isearch since it's very powerful and versatile.

Your Answer

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

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