Emacs 搜索精确单词

13

大部分时间我都在使用Verilog编程语言,我的最爱编辑器是Emacs。

在vi(vim)中有一个特性我很喜欢,但我不知道如何在Emacs中实现。

我希望能够进行精确单词搜索,比如说 - 假设我有这段文本:

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;

当我搜索axi时,我希望只匹配第4行。今天的Ctrl-S搜索将匹配每个axi实例。

在Vim中,做法是按下单词上的*或/ \。

Emacs中是否有类似的功能?

谢谢,Jony

3个回答

11
我想您正在寻找单词搜索功能,通过M-s w激活。
您可以有两种方式使用它:简单地发出M-s w,然后键入要搜索的单词。 或者为了获得类似于vim中的*,您可以使用isearch开始搜索,使用C-s C-w(这将搜索光标下的单词),然后使用M-s w将搜索切换到整个单词模式。

谢谢你的回答,但是当我按下M-s(Meta键是我的左Alt键)时,我得到了跳转到行:我该怎么办? - Jonathan
@Jonathan,也许你已经将 M-s 绑定到了其他功能上。 C-h k M-s 返回什么? - Chris
Chris, `goto-line'是一个交互式的编译Lisp函数,从“/tools/install/common/xemacs-21.4.22/lisp/simple.elc”加载。我该如何覆盖它?或者禁用它? - Jonathan
2
这是最佳答案。如果由于您的初始化文件中的某些更改而未将M-s w绑定到此键(因为某些原因),请尝试C-s M-s w。请参阅Emacs手册,节点Word Search。当然,如果您使用的是XEmacs,则可能会有所不同。 - Drew
这也可以找到像 _word 这样的单词。 - alper

8

需要基于正则表达式的搜索,例如

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

请参阅 Emacs Lisp 信息文件, 节点34.3.1.3:正则表达式中的反斜杠构造

作为命令运行:

(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)))

将其绑定到C-c :上,例如:
(global-set-key [(control c) (\:)] 'my-re-search-forward)

嗨,Andreas,感谢您的出色回答!我对Lisp一无所知,您知道如何编写并将其绑定到键吗?我的意思是,我想要一个键映射,它可以打开isearch-forward-regexp,然后我输入文本,它会自动添加_<my_text>[^_]。 - Jonathan
@Jonathan Done。顺便说一下,isearch-forward-regexp是一个为交互使用而设计的命令。从Emacs Lisp内部,re-search-forward经常很有用。 - Andreas Röhler
1
对我来说没有下划线也可以。例如:\<axi\>而不是\_<axi\_> - vineeshvs
1
@vineeshvs 想必你是对的,谢谢。这只是我的习惯,喜欢搜索符号。 - Andreas Röhler
1
这并不是替换所有出现的单词,只替换所选单词的一部分。 - alper

2

我在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)

如您所见,我将这些命令绑定到 M-s ,M-s .。 根据您使用的Emacs版本,您可以使用内置命令isearch-forward-symbol-at-point(默认绑定到M-s .)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接