13

I'm working in verilog most of the time, and my favorite editor is emacs.

There a feature in vi (vim) I like but I don't know how to do it in emacs

I would like to do an exact word search, for example - let's say I have this text:

1. wire                   axi_bvalid = bvalid;
2. wire                   axi_bready; // assigned later
3. assign                 axi_cross_fifo_pop = axi_bvalid & axi_bready 
4. wire                   axi = 1'b1;

when searching for axi i would like to get match only on line 4. Today Ctrl-S search will match every instance of axi.

In vim the way to do it is press * on the word or /\.

Is there something similar in emacs?

Thanks allot, Jony

ipd
  • 5,674
  • 3
  • 34
  • 49
Jonathan
  • 285
  • 3
  • 9

3 Answers3

11

I think you are searching for the Word Search feature, activated with M-s w.

You can use it in two ways: simply issue M-s w, then type the word to search. Or to get something similar to * in vim, you can start a search with isearch, with C-s C-w (this searches for the word under cursor), then M-s w to toggle the search to whole word mode.

tonio
  • 10,355
  • 2
  • 46
  • 60
  • Thanks for the answer, but When i press M-s (Meta is my left alt) i get the goto line: What should i do? – Jonathan May 19 '15 at 14:56
  • @Jonathan, perhaps you have `M-s` bound to something else. What does `C-h k M-s` give? – ChrisGPT was on strike May 19 '15 at 16:04
  • Chris, `goto-line' is an interactive compiled Lisp function -- loaded from "/tools/install/common/xemacs-21.4.22/lisp/simple.elc" how can i override it? or disable it? – Jonathan May 19 '15 at 16:17
  • 2
    This is the best answer. If for some reason `M-s w` is not bound to this (because something in your init file has changed it), try `C-s M-s w`. See the Emacs manual, node [`Word Search`](http://www.gnu.org/software/emacs/manual/html_node/emacs/Word-Search.html). Of course, if you are using XEmacs then it might be different. – Drew May 19 '15 at 17:56
  • This also finds words like `_word` – alper Apr 28 '20 at 13:37
8

Needs a regular-expression based search, for example

M-x isearch-forward-regexp RET \_<axi\_> RET

See Emacs Lisp Info-file, node 34.3.1.3: Backslash Constructs in Regular Expressions

Running as command:

(defun my-re-search-forward (&optional word)
  "Searches for the last copied solitary WORD, unless WORD is given. "
  (interactive)
  (let ((word (or word (car kill-ring))))
    (re-search-forward (concat "\\_<" word "\\_>") nil t 1)
    (set-mark (point))
    (goto-char (match-beginning 0))
    (exchange-point-and-mark)))

Bind it to C-c : for example:

(global-set-key [(control c) (\:)] 'my-re-search-forward)
Andreas Röhler
  • 4,804
  • 14
  • 18
  • Hi Andreas, Thanks for the great answer! I have no clue about lisp, do you know how to write this and bind it to a key? i mean i would like to have keymap that opens isearch-forward-regexp and i enter a text and its auto add the \_[^_] – Jonathan May 20 '15 at 08:05
  • @Jonathan Done. BTW isearch-forward-regexp is an command designed for interactive use. From inside Emacs Lisp re-search-forward quite often is useful. – Andreas Röhler May 20 '15 at 09:33
  • 1
    Works for me without the `_`. Eg: `\` instead of `\_` – vineeshvs May 03 '19 at 07:36
  • 1
    @vineeshvs Think you are right, thanks. Just my habit to search for symbols. – Andreas Röhler May 13 '19 at 12:02
  • 1
    This does not replace all the words if they show up, up part of the selected word to replace – alper Apr 28 '20 at 13:30
2

I didn't find a built-in function in Emacs that is equivalent to vim's *, but I did manage to write these two commands which may suite you:

(defun my-isearch-forward-word-at-point ()
  "Search for word at point."
  (interactive)
  (let ((word (thing-at-point 'word t))
        (bounds (bounds-of-thing-at-point 'word)))
    (if word
        (progn
          (isearch-mode t nil nil nil t)
          (when (< (car bounds) (point))
            (goto-char (car bounds)))
          (isearch-yank-string word))
      (user-error "No word at point"))))

(defun my-isearch-forward-symbol-at-point ()
  "Search for symbol at point."
  (interactive)
  (let ((symbol (thing-at-point 'symbol t))
        (bounds (bounds-of-thing-at-point 'symbol)))
    (if symbol
        (progn
          (isearch-mode t nil nil nil 'isearch-symbol-regexp)
          (when (< (car bounds) (point))
            (goto-char (car bounds)))
          (isearch-yank-string symbol))
      (user-error "No symbol at point"))))

(global-set-key (kbd "M-s ,") 'my-isearch-forward-word-at-point)
(global-set-key (kbd "M-s .") 'my-isearch-forward-symbol-at-point)

As you see, I bound these command to M-s , and M-s .. Depending on your version of Emacs, you may be able to use the built-in command isearch-forward-symbol-at-point (bound to M-s . by default).

bmag
  • 196
  • 3