Searching for marked (selected) text in Emacs

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

    You can find C-s help by doing C-h k C-s, 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 (C-j) to match end of line. Type C-s to search again forward, C-r to search again backward. Type C-w to yank next word or character in buffer onto the end of the search string, and search for it. Type C-M-w to delete character from end of search string. Type C-M-y to yank char from buffer onto end of search string and search for it. Type M-s C-e to yank rest of line onto end of search string and search for it. Type C-y to yank the last string of killed text. Type M-y to replace string just yanked into search prompt with string killed before it. Type C-q to quote control character to search for it. Type C-x 8 RET to add a character to search by Unicode name, with completion. C-g while searching or when search has failed cancels input back to what has been found successfully. C-g 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 M-c to toggle search case-sensitivity. Type M-s i to toggle search in invisible text. Type M-r to toggle regular-expression mode. Type M-s w to toggle word mode. Type M-s _ to toggle symbol mode. Type M-s ' to toggle character folding.

    Type M-s 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 M-s e to edit the search string in the minibuffer.

    Also supported is a search ring of the previous 16 search strings. Type M-n to search for the next item in the search ring. Type M-p to search for the previous item in the search ring. Type C-M-i to complete the search string using the search ring.

    Type M-% to run ‘query-replace’ with string to replace from last search string. Type C-M-% to run ‘query-replace-regexp’ with the last search string. Type M-s o to run ‘occur’ that shows the last search string. Type M-s h r to run ‘highlight-regexp’ that highlights the last search string.

    Type C-h b to display all Isearch key bindings. Type C-h k to display documentation of Isearch key. Type C-h m to display documentation of Isearch mode.

    0 讨论(0)
  • 2021-01-31 02:25

    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 C-s or C-r.

    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 "s-f") 'isearch-forward-symbol-at-point) in the init file.

    0 讨论(0)
  • 2021-01-31 02:32

    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.

    0 讨论(0)
  • 2021-01-31 02:35

    @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).

    0 讨论(0)
  • 2021-01-31 02:36

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

    0 讨论(0)
  • 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.

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