简体   繁体   English

Emacs搜索确切的单词

[英]Emacs Search exact word

I'm working in verilog most of the time, and my favorite editor is emacs. 我大部分时间都在verilog工作,而我最喜欢的编辑器就是emacs。

There a feature in vi (vim) I like but I don't know how to do it in emacs 我喜欢vi(vim)中的一个功能,但我不知道如何在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 . 当搜索axi我想仅在第4行获得匹配。今天Ctrl-S搜索将匹配axi每个实例。

In vim the way to do it is press * on the word or /\\. 在vim中,这样做的方法是在单词或/ \\上按*。

Is there something similar in emacs? emacs中有类似的东西吗?

Thanks allot, Jony 谢谢分配,Jony

I think you are searching for the Word Search feature, activated with Ms w . 我想您正在搜索使用Ms w激活的单词搜索功能。

You can use it in two ways: simply issue Ms w , then type the word to search. 您可以通过两种方式使用它:只需发出Ms w ,然后键入要搜索的单词。 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. 或者为了在vim中获得与*相似的东西,你可以用issarch开始搜索,用Cs Cw (这会搜索光标下的单词),然后Ms w将搜索切换到整个单词模式。

Needs a regular-expression based search, for example 例如,需要基于正则表达式的搜索

Mx isearch-forward-regexp RET \\_<axi\\_> RET Mx isearch-forward-regexp RET \\_<axi\\_> RET

See Emacs Lisp Info-file, node 34.3.1.3: Backslash Constructs in Regular Expressions 请参阅Emacs Lisp信息文件,节点34.3.1.3:正则表达式中的反斜杠构造

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: 我没有在Emacs中找到相当于vim *的内置函数,但我确实设法编写了这两个可能适合你的命令:

(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 . 如你所见,我将这些命令绑定给MsMs。 . 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). 根据您的Emacs版本,您可以使用内置命令isearch-forward-symbol-at-point (默认情况下绑定到Ms。 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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