简体   繁体   中英

Emacs Search exact word

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

I think you are searching for the Word Search feature, activated with Ms w .

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

Needs a regular-expression based search, for example

Mx 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 Cc : for example:

(global-set-key [(control c) (\:)] 'my-re-search-forward)

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 Ms , and Ms . . Depending on your version of Emacs, you may be able to use the built-in command isearch-forward-symbol-at-point (bound to Ms . by default).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM