Searching for marked (selected) text in Emacs

后端 未结 8 468
北恋
北恋 2021-01-31 01:50

I use emacs for viewing and editing code and other text files. I wanted to know if there is a way to search forward or backward for text which is marked in the current buffer. S

8条回答
  •  不知归路
    2021-01-31 02:40

    Other answers describe how to search for copied text, or how to search for the word at point. But none of them actually describe how to "search with the marked text."

    Adding the following hook will make it so that the currently-selected text is the text used for an isearch:

    (defun jrh-isearch-with-region ()
      "Use region as the isearch text."
      (when mark-active
        (let ((region (funcall region-extract-function nil)))
          (deactivate-mark)
          (isearch-push-state)
          (isearch-yank-string region))))
    
    (add-hook 'isearch-mode-hook #'jrh-isearch-with-region)
    

    Tip: This pairs nicely with expand-region.

提交回复
热议问题