Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 648
richq Avatar asked Feb 26 '09 08:02

richq


People also ask

How do I search for a phrase in Emacs?

Simple Searches Emacs also offers a simple, or nonincremental, search. To use a more straightforward search, type C-s RETURN or select Search from the Search menu. Type the search string, press RETURN, and Emacs begins the search. Simply press C-s again to repeat the search.

How do I search backwards in Emacs?

To search backwards, use C-r ( isearch-backward ) instead of C-s to start the search. A backward search finds matches that end before the starting point, just as a forward search finds matches that begin after it.


1 Answers

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) 
like image 67
scottfrazer Avatar answered Oct 09 '22 15:10

scottfrazer