如何在 GNU Emacs 中模拟 Vim 的 * 搜索? [英] How can I emulate Vim's * search in GNU Emacs?

查看:22
本文介绍了如何在 GNU Emacs 中模拟 Vim 的 * 搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Vim the * key in normal mode searches for the word under the cursor. In GNU Emacs the closest native equivalent would be:

C-s C-w

但这并不完全相同.它打开增量搜索迷你缓冲区,并从当前缓冲区中的光标复制到单词的末尾.在 Vim 中,您将搜索整个单词,即使按 * 键时您在单词中间也是如此.

But that isn't quite the same. It opens up the incremental search mini buffer and copies from the cursor in the current buffer to the end of the word. In Vim you'd search for the whole word, even if you are in the middle of the word when you press *.

我编写了一些 elisp 来做类似的事情:

I've cooked up a bit of elisp to do something similar:

(defun find-word-under-cursor (arg)
  (interactive "p")
  (if (looking-at "\<") () (re-search-backward "\<" (point-min)))
  (isearch-forward))

在启动 isearch 之前小跑到单词的开头.我将它绑定到 C-+,它很容易在我的键盘上输入并且类似于 *,所以当我输入 C-+ Cw 时,它会从单词的开头复制到搜索迷你-缓冲.

That trots backwards to the start of the word before firing up isearch. I've bound it to C-+, which is easy to type on my keyboard and similar to *, so when I type C-+ C-w it copies from the start of the word to the search mini-buffer.

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

However, this still isn't perfect. Ideally it would regexp search for "<" word ">" to not show partial matches (searching for the word "bar" shouldn't match "foobar", just "bar" on its own). I tried using search-forward-regexp and concat'ing <> but this doesn't wrap in the file, doesn't highlight matches and is generally pretty lame. An isearch-* function seems the best bet, but these don't behave well when scripted.

有什么想法吗?任何人都可以提供对 elisp 的任何改进吗?或者有没有我忽略的其他方式?

Any ideas? Can anyone offer any improvements to the bit of elisp? Or is there some other way that I've overlooked?

推荐答案

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

Based on your feedback to my first answer, how about this:

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

这篇关于如何在 GNU Emacs 中模拟 Vim 的 * 搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆