我如何模仿Vim';s*在GNU Emacs中搜索?
在Vim中,正常模式下的*键搜索光标下的单词。在GNU Emacs中,最接近的本机等价物是:我如何模仿Vim';s*在GNU Emacs中搜索?,vim,emacs,elisp,dot-emacs,Vim,Emacs,Elisp,Dot Emacs,在Vim中,正常模式下的*键搜索光标下的单词。在GNU Emacs中,最接近的本机等价物是: C-s C-w 但这并不完全相同。它打开增量搜索小缓冲区,并从当前缓冲区中的光标复制到单词的末尾。在Vim,你会搜索整个单词,即使你在单词的中间,当你按**。 我做了一点elisp来做类似的事情: (defun find-word-under-cursor (arg) (interactive "p") (if (looking-at "\\<") () (re-search-backw
C-s C-w
但这并不完全相同。它打开增量搜索小缓冲区,并从当前缓冲区中的光标复制到单词的末尾。在Vim,你会搜索整个单词,即使你在单词的中间,当你按**。
我做了一点elisp来做类似的事情:
(defun find-word-under-cursor (arg)
(interactive "p")
(if (looking-at "\\<") () (re-search-backward "\\<" (point-min)))
(isearch-forward))
(在光标(arg)下定义查找单词)
(交互式“p”)
(如果(查看“\\我还没有尝试过,但是有一些代码叫做Grep-O-Matic。使用此代码,您应该能够在isearch模式下执行C-*
(define-key isearch-mode-map [?\C-*] 'kmk-isearch-yank-thing)
(defun kmk-isearch-yank-thing ()
"Pull next thing from buffer into search string."
(interactive)
(let ((string (regexp-quote (thing-at-point 'word))))
(setq isearch-string
(concat isearch-string "\\")
isearch-message
(concat isearch-message
(mapconcat 'isearch-text-char-description
string ""))
;; Don't move cursor in reverse search.
isearch-yank-flag t))
(setq isearch-regexp t isearch-word nil isearch-success t isearch-adjusted t)
(isearch-search-and-update))
(定义关键isearch模式映射[?\C-*]“kmk isearch yank thing”)
(defun kmk isearch yank thing()
“将下一个内容从缓冲区拉入搜索字符串。”
(互动)
(let((字符串(regexp引号(thing at point'word)))
(setq isearch字符串
(连续搜索字符串“\\”)
isearch消息
(concat isearch消息
(mapconcat’i搜索文本字符说明
字符串“”))
在反向搜索中不要移动光标。
isearch yank flag(美国国旗)
(setq isearch regexp t isearch word nil isearch success t isearch adjusted t)
(isearch搜索和更新))
有很多方法可以做到这一点:
根据您对我的第一个答案的反馈,这个如何:
(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)
(在()
(互动)
(以交互方式调用“isearch forward regexp))
(我的搜索引擎拉了拉单词钩()
(当(等于此命令“my isearch word at point”)
(let((字符串(concat“\\”))
(如果(和i)搜索案例折叠搜索
(等式“非美国佬搜索大写))
(setq字符串(下行字符串)))
(setq isearch字符串
isearch消息
(concat isearch消息
(mapconcat’i搜索文本字符说明
字符串“”))
isearch yank flag(美国国旗)
(isearch搜索和更新)))
(添加hook“isearch模式hook”我的isearch yank单词hook)
scottfrazer的答案对我来说效果很好,除了以“_”(或者其他非单词字符?)结尾的单词。我发现,根据emacs的版本,light symbol模式的代码对单词边界使用了不同的正则表达式,这为我解决了这个问题。下面是修改后的代码:
(defconst my-isearch-rx-start
(if (< emacs-major-version 22)
"\\<"
"\\_<")
"Start-of-symbol regular expression marker.")
(defconst my-isearch-rx-end
(if (< emacs-major-version 22)
"\\>"
"\\_>")
"End-of-symbol regular expression marker.")
(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 my-isearch-rx-start
(buffer-substring-no-properties
(progn (skip-syntax-backward "w_") (point))
(progn (skip-syntax-forward "w_") (point)))
my-isearch-rx-end)))
(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)
(定义我的isearch接收启动
(如果(
;以下是我的版本:模拟Visual Studio/Windows密钥绑定
;C-F3-从该点开始搜索单词
;F3向前搜索,换档F3向后
(设置我的搜索结束为零)
(取消我的搜索功能(目录)
(互动)
(let*((文本(汽车搜索环))newpoint)
(当我的搜索结束时)
(转到字符(如果(=方向1)(最小点)(最大点)))
(setq我的搜索结束为零)
(setq newpoint(向前搜索文本nil t dir))
(如果是newpoint
(设置标记(如果(=方向1)(-newpoint(长度文本))
(+newpoint(长度文本)))
(消息“搜索失败:%s”文本)(叮当声)
(设置我的搜索换行文本)))
(取消我的搜索前进()(交互式)(我的搜索功能1))
(取消我的搜索bwd()(交互式)(我的搜索功能-1))
(德芬把东西拽进了搜索()
(互动)
(让((文本)(如果标记为活动
(缓冲区子字符串无属性(区域开始)(区域结束))
(或(当前单词“”))
(当(>(长度文本)0)(isearch更新环文本)(setq my search wrap nil)
(我的搜索fwd)))
(全局设置键(kbd“”)“我的搜索fwd”;类似Visual Studio的搜索键
(全局设置键(kbd“”)我的搜索bwd)
(全局设置键(kbd“”)“将对象拖动到搜索中)
如何使用内置命令M-b C-s C-w(单词开始、搜索、单词搜索)emacs扩展提供此功能。特别是,推荐的.emacrc
设置:
(require 'highlight-symbol)
(global-set-key [(control f3)] 'highlight-symbol-at-point)
(global-set-key [f3] 'highlight-symbol-next)
(global-set-key [(shift f3)] 'highlight-symbol-prev)
允许在当前点(F3)跳到下一个符号、跳到上一个符号(Shift+F3)或高亮显示与光标下的符号匹配的符号(Ctrl+F3)。如果光标位于单词中间,则命令将继续执行正确的操作
与vim的超级明星不同,突出显示符号和在符号之间跳跃是绑定到两个不同的命令上的。我个人不介意分开,但是如果你想精确匹配vim的行为,你可以在同一个按键下绑定这两个命令。博客的米奇重新引入了一个很酷的““lib提供了M-n
和M-p
的全局绑定,用于在缓冲区中的光标下导航符号。不会影响搜索寄存器,因此它不是一个*
的替代品,而是一个解决导航问题的聪明且可用的替代品。这非常类似(在功能上,如果不是形式上)我曾经有过这样的经历——问题是多重用途不能很好地与isearch配合使用
(require 'highlight-symbol)
(global-set-key [(control f3)] 'highlight-symbol-at-point)
(global-set-key [f3] 'highlight-symbol-next)
(global-set-key [(shift f3)] 'highlight-symbol-prev)