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

查看:94
本文介绍了如何在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 *.

我已经煮了一点点做一些类似的事情:

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

在开始搜索之前,这个字符向后移动到单词的开头。我把它绑定到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.

任何想法?任何人都可以提供任何改进的精灵?还是还有其他一些我忽视的方法?

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天全站免登陆