Searching for marked (selected) text in Emacs

后端 未结 8 448
北恋
北恋 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:47

    I am using the following which does not have the problem of having to type more then one successive C-s to find later occurences:

        (defun search-selection (beg end)
          "search for selected text"
          (interactive "r")
          (kill-ring-save beg end)
          (isearch-mode t nil nil nil)
          (isearch-yank-pop)
        )
        (define-key global-map (kbd "<C-f3>") 'search-selection)
    

    The disadvantage of the previous code is that the selected text is copied to the stretch. The following code does not have this problem:

        (defun search-selection (beg end)
          "search for selected text"
          (interactive "r")
          (let (
                (selection (buffer-substring-no-properties beg end))
               )
            (deactivate-mark)
            (isearch-mode t nil nil nil)
            (isearch-yank-string selection)
          )
        )
        (define-key global-map (kbd "<C-f3>") 'search-selection)
    
    0 讨论(0)
  • 2021-01-31 02:47

    The answers above (including the accepted one) are too cumbersome IMHO. I found the following info and like it well better:

    “Ctrl+s Ctrl+w”. This will search the current word, but you must move your cursor to the beginning of the word first.

    http://xah-forum.blogspot.com/2009/08/search-word-under-cursor-in-emacs.html

    0 讨论(0)
提交回复
热议问题