【问题标题】:Emacs Search exact wordEmacs 搜索确切的单词
【发布时间】:2015-05-19 14:09:18
【问题描述】:

我大部分时间都在用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中有类似的东西吗?

谢谢你,乔尼

【问题讨论】:

    标签: emacs xemacs


    【解决方案1】:

    我认为您正在搜索Word Search 功能,使用M-s w 激活。

    您可以通过两种方式使用它:只需发出M-s w,然后输入要搜索的单词。 或者要在 vim 中获得类似于 * 的内容,您可以使用 isearch 开始搜索,使用 C-s C-w(搜索光标下的单词),然后 M-s w 将搜索切换到全词模式。

    【讨论】:

    • 感谢您的回答,但是当我按 M-s(Meta 是我的左 alt)时,我得到了 goto 行:我该怎么办?
    • @Jonathan,也许你有 M-s 绑定到别的东西。 C-h k M-s 给了什么?
    • 克里斯,`goto-line' 是一个交互式编译的 Lisp 函数——从“/tools/install/common/xemacs-21.4.22/lisp/simple.elc”加载我如何覆盖它?还是禁用它?
    • 这是最好的答案。如果由于某种原因M-s w 未绑定到此(因为您的 init 文件中的某些内容已更改它),请尝试C-s M-s w。请参阅 Emacs 手册,节点 Word Search。当然,如果您使用的是 XEmacs,那可能会有所不同。
    • 这也能找到像_word这样的词
    【解决方案2】:

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

    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)
    

    【讨论】:

    • 您好安德烈亚斯,感谢您的出色回答!我对lisp一无所知,你知道如何写这个并将它绑定到一个键上吗?我的意思是我想要打开 isearch-forward-regexp 的键盘映射,我输入一个文本,它会自动添加 _[^_]
    • @Jonathan Done。顺便说一句,isearch-forward-regexp 是一个为交互式使用而设计的命令。从 Emacs Lisp 内部重新搜索转发通常很有用。
    • 在没有_ 的情况下为我工作。例如:\&lt;axi\&gt; 而不是 \_&lt;axi\_&gt;
    • @vineeshvs 认为你是对的,谢谢。只是我搜索符号的习惯。
    • 这不会替换所有出现的单词,向上替换所选单词的一部分
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多