Searching for marked (selected) text in Emacs

21,007

Solution 1

Yes. M-W (to get a copy of the selected text) C-s <RET> C-y <RET>. Then repeat C-s as needed. Similarly for C-r.

Solution 2

@Alex nails it.

Another option I use quite often is C-s C-w to search for the word after the current mark. Hitting C-w repeatedly increases the search with additional words (e.g., C-s C-w C-w C-w searches for the 3 words after the current mark).

Similarly, C-s M-s C-e searches for the rest of the line after the current mark and C-s C-M-y searches for the character after the mark. These are both repeatable in the same way (the former by somewhat-awkwardly repeating M-s C-e after C-s).

Solution 3

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)

Solution 4

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.

Solution 5

The shortest key sequence to do this is M-w C-s M-y.

Share:
21,007
user2792801
Author by

user2792801

Updated on July 09, 2022

Comments

  • user2792801
    user2792801 almost 2 years

    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. Similar to what I can do in notepad or wordpad. As in can I mark some text in the buffer and do a C-s or C-r and be able to search with the marked text without actually typing in the whole search text?

    Thank you,

    Rohit

  • Alex Coventry
    Alex Coventry over 15 years
    Glad I could help. Incidentally, the relevant section of the manual is "20.1 Basics of Incremental Search". C-h i d m emacs<RET> g Basic Isearch <RET> manpagez.com/info/emacs/emacs_95.php
  • maxywb
    maxywb over 10 years
    It's worth noting this searches for the lower case of the word even if you're highlighting all uppercase text.
  • fbmd
    fbmd over 9 years
    That is how I already did it ... I am somewhat surprised there isn't a single built-in command for this.
  • dougkramer
    dougkramer about 9 years
    Likewise, C-r C-w searches backward ("r" = reverse) for the word after the mark. And C-r C-w C-w searches backward for the two words after the mark.
  • Jackson
    Jackson over 8 years
    This doesn't explain how to search for marked text. This explains how to search for copied text.
  • Lorem Ipsum
    Lorem Ipsum over 6 years
    This does (nearly) precisely what the question asks for. The only difference is that isearch-forward-symbol-at-point automatically selects the text at point. This can be problematic if what you consider a word differs from what Emacs considers a word. Still useful, though. The default binding on GNU Emacs 24.5.1 is M-s ..
  • nephewtom
    nephewtom over 6 years
    Well, that is exactly what @chris-conway pointed on stackoverflow.com/a/203026/316232.
  • Dave Liepmann
    Dave Liepmann over 6 years
    This is a keystroke, not a command, which means it's meaningless in any environment that happens to override that. It sends us on a wild goose chase to figure out what command you're trying to refer to.
  • Arch Stanton
    Arch Stanton about 6 years
    The function C-s C-y used to be bound to is now bound to C-s M-s C-e (since Emacs 24.1) [source].
  • Arch Stanton
    Arch Stanton over 5 years
    I think it works better if you make two modifications: add (goto-char (region-beginning)) before (deactivate-mark) to prevent moving straight to the next match when the function is called, and replace (isearch-push-state) with (isearch-update); with (isearch-push-state), if it's called repeatedly on the same piece of text it stops highlighting the other occurrences.