gpt4 book ai didi

vim - 如何在 GNU Emacs 中模拟 Vim 的 * 搜索?

转载 作者:行者123 更新时间:2023-12-03 08:49:14 25 4
gpt4 key购买 nike

在 Vim 中,正常模式下的 * 键搜索光标下的单词。在 GNU Emacs 中,最接近的原生等价物是:

C-s C-w

但这不完全一样。它打开增量搜索迷你缓冲区并从当前缓冲区中的光标复制到单词的末尾。在 Vim 中,您会搜索整个单词,即使您在按 * 时位于单词的中间。

我已经做了一些 elisp 来做类似的事情:
(defun find-word-under-cursor (arg)
(interactive "p")
(if (looking-at "\\<") () (re-search-backward "\\<" (point-min)))
(isearch-forward))

在启动 isearch 之前,它会倒退到单词的开头。我已经将它绑定(bind)到 C-+,它很容易在我的键盘上键入,并且类似于 *,所以当我键入 C-+ C-w 时它从单词的开头复制到搜索迷你缓冲区。

然而,这仍然不完美。理想情况下,它将正则表达式搜索 "\<" word "\>"不显示部分匹配(搜索单词“bar”不应该匹配“foobar”,只匹配“bar”)。我尝试使用 search-forward-regexp 和 concat'ing\<> 但这不会包含在文件中,不会突出显示匹配项并且通常很蹩脚。 isearch-* 函数似乎是最好的选择,但这些函数在编写脚本时表现不佳。

有任何想法吗?任何人都可以对 elisp 提供任何改进吗?还是有其他我忽略的方法?

最佳答案

根据您对我的第一个答案的反馈,这个怎么样:

(defun my-isearch-word-at-point ()
(interactive)
(call-interactively 'isearch-forward-regexp))

(defun my-isearch-yank-word-hook ()
(when (equal this-command 'my-isearch-word-at-point)
(let ((string (concat "\\<"
(buffer-substring-no-properties
(progn (skip-syntax-backward "w_") (point))
(progn (skip-syntax-forward "w_") (point)))
"\\>")))
(if (and isearch-case-fold-search
(eq 'not-yanks search-upper-case))
(setq string (downcase string)))
(setq isearch-string string
isearch-message
(concat isearch-message
(mapconcat 'isearch-text-char-description
string ""))
isearch-yank-flag t)
(isearch-search-and-update))))

(add-hook 'isearch-mode-hook 'my-isearch-yank-word-hook)

关于vim - 如何在 GNU Emacs 中模拟 Vim 的 * 搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/589691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com