How can I emulate Vim's * search in GNU Emacs?
Asked Answered
W

10

33

In Vim the * key in normal mode searches for the word under the cursor. In GNU Emacs the closest native equivalent would be:

C-s C-w

But that isn't quite the same. It opens up the incremental search mini buffer and copies from the cursor in the current buffer to the end of the word. In Vim you'd search for the whole word, even if you are in the middle of the word when you press *.

I've cooked up a bit of elisp to do something similar:

(defun find-word-under-cursor (arg)
  (interactive "p")
  (if (looking-at "\\<") () (re-search-backward "\\<" (point-min)))
  (isearch-forward))

That trots backwards to the start of the word before firing up isearch. I've bound it to C-+, which is easy to type on my keyboard and similar to *, so when I type C-+ C-w it copies from the start of the word to the search mini-buffer.

However, this still isn't perfect. Ideally it would regexp search for "\<" word "\>" to not show partial matches (searching for the word "bar" shouldn't match "foobar", just "bar" on its own). I tried using search-forward-regexp and concat'ing \<> but this doesn't wrap in the file, doesn't highlight matches and is generally pretty lame. An isearch-* function seems the best bet, but these don't behave well when scripted.

Any ideas? Can anyone offer any improvements to the bit of elisp? Or is there some other way that I've overlooked?

Wilhelm answered 26/2, 2009 at 8:49 Comment(3)
Yes, Emacs lets you hook into isearch to do this, but the highlight-symbols package is even better. I find keeping symbols central to my current thought process super helpful. Also, you get one key search and your point stays constant relative to the beginning of the symbol.Lugansk
#1775505Vendace
Since Emacs 24.4 (released on 20 Oct 2014), this function is now available as the interactive command isearch-forward-symbol-at-point which can be invoked with the global key sequence M-s . I have posted an answer at https://mcmap.net/q/444530/-how-can-i-emulate-vim-39-s-search-in-gnu-emacs about it.Windom
P
14

Based on your feedback to my first answer, how about this:

(defun my-isearch-word-at-point ()
  (interactive)
  (call-interactively 'isearch-forward-regexp))

(defun my-isearch-yank-word-hook ()
  (when (equal this-command 'my-isearch-word-at-point)
    (let ((string (concat "\\<"
                          (buffer-substring-no-properties
                           (progn (skip-syntax-backward "w_") (point))
                           (progn (skip-syntax-forward "w_") (point)))
                          "\\>")))
      (if (and isearch-case-fold-search
               (eq 'not-yanks search-upper-case))
          (setq string (downcase string)))
      (setq isearch-string string
            isearch-message
            (concat isearch-message
                    (mapconcat 'isearch-text-char-description
                               string ""))
            isearch-yank-flag t)
      (isearch-search-and-update))))

(add-hook 'isearch-mode-hook 'my-isearch-yank-word-hook)
Pajamas answered 26/2, 2009 at 15:7 Comment(4)
Dies on me if there are no matches later in the file than the current one. Backtrace and repro instructions here.Glochidiate
This fails when run on <word>.Kenway
It works well first time, but gives errors on repeated use: Wrong type argument: arrayp, nilMelanoid
How is this different from the built in isearch-forward-symbol-at-point?Vestibule
H
7

The highlight symbol emacs extension provides this functionality. In particular, the recommend .emacsrc setup:

(require 'highlight-symbol)

(global-set-key [(control f3)] 'highlight-symbol-at-point)
(global-set-key [f3] 'highlight-symbol-next)
(global-set-key [(shift f3)] 'highlight-symbol-prev)

Allows jumping to the next symbol at the current point (F3), jumping to the previous symbol (Shift+F3) or highlighting symbols matching the one under the cursor (Ctrl+F3). The commands continue to do the right thing if your cursor is mid-word.

Unlike vim's super star, highlighting symbols and jumping between symbols are bound to two different commands. I personally don't mind the separation, but you could bind the two commands under the same keystroke if you wanted to precisely match vim's behaviour.

Hallsy answered 11/6, 2011 at 5:55 Comment(0)
P
5

There are lots of ways to do this:

http://www.emacswiki.org/emacs/SearchAtPoint

Pajamas answered 26/2, 2009 at 12:41 Comment(1)
JimBlandy's solution is sooo close, but it doesn't actually "search", it hacks on the search interface. You can't jump to previous/next and doesn't store the word in the buffer. Argh!Wilhelm
E
3

scottfrazer's answer works well for me, except for words that end in '_' (or perhaps other non-word characters?). I found that the code for light-symbol mode was using a different regex for word boundary depending on the version of emacs, and that fixed it for me. Here is the modified code:

(defconst my-isearch-rx-start
  (if (< emacs-major-version 22)
      "\\<"
    "\\_<")
  "Start-of-symbol regular expression marker.")

(defconst my-isearch-rx-end
  (if (< emacs-major-version 22)
      "\\>"
    "\\_>")
  "End-of-symbol regular expression marker.")

(defun my-isearch-word-at-point ()
  (interactive)
  (call-interactively 'isearch-forward-regexp))

(defun my-isearch-yank-word-hook ()
  (when (equal this-command 'my-isearch-word-at-point)
    (let ((string (concat my-isearch-rx-start
                          (buffer-substring-no-properties
                           (progn (skip-syntax-backward "w_") (point))
                           (progn (skip-syntax-forward "w_") (point)))
                          my-isearch-rx-end)))
      (if (and isearch-case-fold-search
               (eq 'not-yanks search-upper-case))
          (setq string (downcase string)))
      (setq isearch-string string
            isearch-message
            (concat isearch-message
                    (mapconcat 'isearch-text-char-description
                               string ""))
            isearch-yank-flag t)
      (isearch-search-and-update))))

(add-hook 'isearch-mode-hook 'my-isearch-yank-word-hook)
Expectorate answered 2/9, 2009 at 19:19 Comment(0)
J
2

How about built in commands M-b C-s C-w (start of word,search,word search)

Jer answered 11/5, 2010 at 18:49 Comment(1)
It ain't really as good as just a single keystroke, is it? :-)Wilhelm
F
2

Mickey of Mastering Emacs blog reintroduced a cool "Smart Scan" lib that gives global bindings of M-n and M-p for navigating symbols under the cursor in the buffer. Doesn't affect search register so it's not a * replacement as is, but a clever and usable alternative to the navigation problem.

Fablan answered 5/11, 2013 at 9:2 Comment(0)
B
0

I have not tried it but there is some code here called Grep-O-Matic.

Berylberyle answered 26/2, 2009 at 9:14 Comment(0)
H
0

With this you should be able to do C-* while in isearch mode.

(define-key isearch-mode-map [?\C-*] 'kmk-isearch-yank-thing)

(defun kmk-isearch-yank-thing ()
  "Pull next thing from buffer into search string."
  (interactive)
  (let ((string (regexp-quote (thing-at-point 'word))))
    (setq isearch-string 
      (concat isearch-string "\\")
      isearch-message
      (concat isearch-message
          (mapconcat 'isearch-text-char-description
                 string ""))
      ;; Don't move cursor in reverse search.
      isearch-yank-flag t))
  (setq isearch-regexp t isearch-word nil isearch-success t isearch-adjusted t)
  (isearch-search-and-update))
Haggi answered 26/2, 2009 at 9:30 Comment(1)
This is pretty similar (in function, if not form) to what I had at one point - the problem is that multiple uses don't play well with isearch. There's no way to "clean up" isearch before running the C-* again. You end up with search1search2search3 all concatonated mysteriously.Wilhelm
H
0
;Here is my version: Emulates Visual Studio/Windows key bindings 
; C-F3 - Start searching the word at the point
; F3 searches forward and Shift F3 goes reverse

(setq my-search-wrap nil)

(defun my-search-func (dir)
  (interactive)
  (let* ((text (car search-ring)) newpoint)
        (when my-search-wrap  
             (goto-char (if (= dir 1) (point-min) (point-max)))
             (setq my-search-wrap nil))
        (setq newpoint (search-forward text nil t dir))
        (if newpoint
          (set-mark (if (= dir 1) (- newpoint (length text))
                         (+ newpoint (length text))))
          (message "Search Failed: %s" text) (ding)
          (setq my-search-wrap text))))

(defun my-search-fwd () (interactive) (my-search-func 1))
(defun my-search-bwd () (interactive) (my-search-func -1))

(defun yank-thing-into-search ()
   (interactive)
   (let ((text (if mark-active
          (buffer-substring-no-properties (region-beginning)(region-end))
                 (or (current-word) ""))))
     (when (> (length text) 0) (isearch-update-ring text) (setq my-search-wrap nil)
            (my-search-fwd))))
(global-set-key (kbd "")    'my-search-fwd)            ; Visual Studio like search keys
(global-set-key (kbd "")  'my-search-bwd)
(global-set-key (kbd "")  'yank-thing-into-search)

Haematoblast answered 24/1, 2010 at 21:59 Comment(0)
W
0

Since Emacs 24.4, searching a symbol under the cursor is available with the global key sequence M-s .

Here is more information about this key sequence and the function it invokes obtained using the describe system of Emacs (C-h k M-s .):

M-s . runs the command isearch-forward-symbol-at-point (found in
global-map), which is an interactive compiled Lisp function in
‘isearch.el’.

It is bound to M-s ., <menu-bar> <edit> <search> <i-search>
<isearch-forward-symbol-at-point>.

(isearch-forward-symbol-at-point &optional ARG)

Do incremental search forward for a symbol found near point.
Like ordinary incremental search except that the symbol found at point
is added to the search string initially as a regexp surrounded
by symbol boundary constructs \_< and \_>.
See the command ‘isearch-forward-symbol’ for more information.
With a prefix argument, search for ARGth symbol forward if ARG is
positive, or search for ARGth symbol backward if ARG is negative.

  Probably introduced at or before Emacs version 24.4.
Windom answered 12/2, 2023 at 8:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.