简体   繁体   中英

Searching for marked (selected) text in Emacs

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 Cs or Cr and be able to search with the marked text without actually typing in the whole search text?

Thank you,

Rohit

@Alex nails it.

Another option I use quite often is Cs Cw to search for the word after the current mark. Hitting Cw repeatedly increases the search with additional words (eg, Cs Cw Cw Cw searches for the 3 words after the current mark).

Similarly, Cs Ms Ce searches for the rest of the line after the current mark and Cs CMy searches for the character after the mark. These are both repeatable in the same way (the former by somewhat-awkwardly repeating Ms Ce after Cs ).

Yes. MW (to get a copy of the selected text) Cs <RET> Cy <RET> . Then repeat Cs as needed. Similarly for Cr .

I am using the following which does not have the problem of having to type more then one successive Cs 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)

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 .

执行此操作的最短键序列是M - w C - s M - y

There is a great function for this: isearch-forward-symbol-at-point . It highlights all occurrences of the word where your point is located - no need to place the point at the beginning of the word. Then you can move to next or previous with Cs or Cr.

Note that it is an exact match: if you use it on hi it won't match chill for instance.

I mapped if to command-f (mac OSX): (global-set-key (kbd "sf") 'isearch-forward-symbol-at-point) in the init file.

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

You can find Cs help by doing Ch k Cs, and it says:

Type DEL to cancel last input item from end of search string. Type RET to exit, leaving point at location found. Type LFD (Cj) to match end of line. Type Cs to search again forward, Cr to search again backward. Type Cw to yank next word or character in buffer onto the end of the search string, and search for it. Type CMw to delete character from end of search string. Type CMy to yank char from buffer onto end of search string and search for it. Type Ms Ce to yank rest of line onto end of search string and search for it. Type Cy to yank the last string of killed text. Type My to replace string just yanked into search prompt with string killed before it. Type Cq to quote control character to search for it. Type Cx 8 RET to add a character to search by Unicode name, with completion. Cg while searching or when search has failed cancels input back to what has been found successfully. Cg when search is successful aborts and moves point to starting point.

If you try to exit with the search string still empty, it invokes nonincremental search.

Type Mc to toggle search case-sensitivity. Type Ms i to toggle search in invisible text. Type Mr to toggle regular-expression mode. Type Ms w to toggle word mode. Type Ms _ to toggle symbol mode. Type Ms ' to toggle character folding.

Type Ms SPC to toggle whitespace matching. In incremental searches, a space or spaces normally matches any whitespace defined by the variable 'search-whitespace-regexp'; see also the variables 'isearch-lax-whitespace' and 'isearch-regexp-lax-whitespace'.

Type Ms e to edit the search string in the minibuffer.

Also supported is a search ring of the previous 16 search strings. Type Mn to search for the next item in the search ring. Type Mp to search for the previous item in the search ring. Type CMi to complete the search string using the search ring.

Type M-% to run 'query-replace' with string to replace from last search string. Type CM-% to run 'query-replace-regexp' with the last search string. Type Ms o to run 'occur' that shows the last search string. Type Ms hr to run 'highlight-regexp' that highlights the last search string.

Type Ch b to display all Isearch key bindings. Type Ch k to display documentation of Isearch key. Type Ch m to display documentation of Isearch mode.

Adopted Jackson's Answer for Swiper :

 (defun swiper-isearch-selected ()
    "Use region as the isearch text."
    (interactive)
    (if mark-active
      (swiper-isearch (funcall region-extract-function nil))
      (swiper-isearch)))

Additionally, if there is no selected region, it just falls back to the regular swiper-isearch .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM