如何在Emacs中搜索模式的第n个出现?

yve*_*mes 5 emacs

我试图尽可能地避免使用elisp.我想我能够在Elisp中实现我的问题的解决方案,但这不是我想要的.

我正在寻找缓冲区中第n个字符串的出现.例如,在第4次出现之后foo,我试过了C-u C-s foo.但C-s不解释前缀.

在Emacs中有一个简单/优雅的键序列来执行该工作吗?

pmr*_*pmr 6

search-forward是一个搜索下一个字符串的简单函数.它还接受COUNT搜索下一次COUNT连续出现的可选参数.

不幸的是,您无法使用前缀参数调用它,因为它会查询输入.

你已经猜到了答案:把一些elisp扔在一起.

此函数会查询您的字符串和计数并执行搜索:

(defun search-forward-count (string count)
  (interactive "sString: \nnCount: ")
  (re-search-forward string nil nil count))
Run Code Online (Sandbox Code Playgroud)

此函数查询您的字符串并使用prefix参数作为其计数:

(defun search-forward-prefix (count string)
  (interactive "p\nsString: ")
  (re-search-forward string nil nil count))
Run Code Online (Sandbox Code Playgroud)