如果活动emacs,则搜索区域



有没有办法告诉isearch搜索活动区域(如果有的话)?否则会定期提示输入字符串。

编辑:27.10.12

我最终使用了以下功能:

(defun er/isearch-word-at-point ()
(interactive)
(call-interactively 'isearch-forward-regexp))
(defun er/isearch-yank-word-hook ()
(when (equal this-command 'er/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))))
(defun er/isearch-yank-region ()
(interactive)
(isearch-yank-internal (lambda () (mark))))
(define-key isearch-mode-map (kbd "C-r") 'er/isearch-yank-region)
(define-key isearch-mode-map (kbd "C-t") 'er/isearch-word-at-point)

第一个是我在网上找到的一个函数,将光标下的单词标记为搜索词(类似于vim中的*和#),然后直接跳到下一个单词,第二个是@Oleg Pavliv的答案。

编辑#2

事实上,为什么不把它们两者结合起来获得超甜呢?好吧

(defun er/isearch-word-or-region-at-point ()
(interactive)
(if (region-active-p)
(isearch-yank-internal (lambda () (mark)))
(call-interactively 'isearch-forward-regexp)))
(defun er/isearch-yank-word-hook ()
(when (equal this-command 'er/isearch-word-or-region-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 'er/isearch-yank-word-hook)
(define-key isearch-mode-map (kbd "C-r") 'er/isearch-word-or-region-at-point)

好问题。

在Emacs中似乎没有这种可能性。你可以自己实现

(defun isearch-yank-region ()
(interactive)
(isearch-yank-internal (lambda () (mark))))

(define-key isearch-mode-map "C-r" 'isearch-yank-region)

现在您选择了一个区域,调用增量搜索C-s并拖动区域C-r。然后您可以继续增量搜索C-s

实现您想要的目标的一个简单方法是在搜索之前将当前区域保存到kill环中,然后将其作为搜索项猛拉到迷你缓冲区中。您可以通过执行以下密钥序列来完成此操作:

M-w C-s M-y

翻译过来就是:

M-w:将区域保存为已被杀死,但不会杀死它。

C-s:进行增量前向搜索。

M-y:猛拉最后一个终止文本字符串(或者更确切地说,是M-wbefore保存的内容)。

打字有点像手指舞,但如果你经常使用它,它就会成为第二天性。这个通用解决方案的好处在于,它只使用了Emacs中已经内置的基本功能。没有必要建议任何功能。

在Emacs 28.1中,您可以使用绑定到M-s M-.isearch-forward-thing-at-point

下面是我的小函数,可以通过以下方式使用isearch:如果区域处于活动状态,则使用它,否则只运行isearch-forward。我希望它能对某人有所帮助:

(defun aleksei/isearch-region-or-forward ()
"Do incremental search forward, use region if it's active"
(interactive)
(if (use-region-p)
(isearch-forward-thing-at-point)
(isearch-forward)))
(global-set-key (kbd "C-f") 'aleksei/isearch-region-or-forward)

最新更新