简体   繁体   English

在 Emacs 中搜索标记(选定)的文本

[英]Searching for marked (selected) text in Emacs

I use emacs for viewing and editing code and other text files.我使用 emacs 来查看和编辑代码和其他文本文件。 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?如我可以在缓冲区中标记一些文本并执行 Cs 或 Cr 并能够使用标记的文本进行搜索而无需实际输入整个搜索文本吗?

Thank you,谢谢,

Rohit罗希特

@Alex nails it. @Alex 说得通。

Another option I use quite often is Cs Cw to search for the word after the current mark.我经常使用的另一个选项是Cs Cw来搜索当前标记之后的单词。 Hitting Cw repeatedly increases the search with additional words (eg, Cs Cw Cw Cw searches for the 3 words after the current mark).反复点击Cw会增加对附加词的搜索(例如, Cs Cw Cw Cw搜索当前标记之后的 3 个词)。

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.类似地, Cs Ms Ce搜索当前标记之后的行的其余部分,而Cs CMy搜索标记之后的字符。 These are both repeatable in the same way (the former by somewhat-awkwardly repeating Ms Ce after Cs ).这些都可以以相同的方式重复(前者通过在Cs之后有点尴尬地重复Ms Ce )。

Yes.是的。 MW (to get a copy of the selected text) Cs <RET> Cy <RET> . MW (获取所选文本的副本) Cs <RET> Cy <RET> Then repeat Cs as needed.然后根据需要重复Cs Similarly for Cr .对于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:我正在使用以下内容,它没有必须键入多个连续 C 来查找以后出现的问题:

    (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.前面代码的缺点是选中的文本被复制到了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:添加以下钩子将使当前选择的文本成为用于 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 .提示:这很好地与expand-region配对。

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

There is a great function for this: isearch-forward-symbol-at-point .有一个很棒的功能: 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.然后您可以使用 Cs 或 Cr 移至下一个或上一个。

Note that it is an exact match: if you use it on hi it won't match chill for instance.请注意,它是完全匹配的:例如,如果您在hi上使用它,它将与chill不匹配。

I mapped if to command-f (mac OSX): (global-set-key (kbd "sf") 'isearch-forward-symbol-at-point) in the init file.我将 if 映射到 init 文件中的command-f (mac OSX): (global-set-key (kbd "sf") 'isearch-forward-symbol-at-point)

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”. “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 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:您可以通过执行 Ch k Cs 找到 Cs 帮助,它说:

Type DEL to cancel last input item from end of search string.键入 DEL 以取消搜索字符串末尾的最后一个输入项。 Type RET to exit, leaving point at location found.键入 RET 退出,将点留在找到的位置。 Type LFD (Cj) to match end of line.键入 LFD (Cj) 以匹配行尾。 Type Cs to search again forward, Cr to search again backward.键入 Cs 再次向前搜索,键入 Cr 再次向后搜索。 Type Cw to yank next word or character in buffer onto the end of the search string, and search for it.键入 Cw 将缓冲区中的下一个单词或字符拉到搜索字符串的末尾,然后搜索它。 Type CMw to delete character from end of search string.键入 CMw 以从搜索字符串的末尾删除字符。 Type CMy to yank char from buffer onto end of search string and search for it.键入 CMy 将 char 从缓冲区拉到搜索字符串的末尾并搜索它。 Type Ms Ce to yank rest of line onto end of search string and search for it.键入 Ms Ce 将剩余的行拉到搜索字符串的末尾并搜索它。 Type Cy to yank the last string of killed text.键入 Cy 以拉出最后一串被杀死的文本。 Type My to replace string just yanked into search prompt with string killed before it.键入 My 以将刚刚进入搜索提示的字符串替换为在它之前被杀死的字符串。 Type Cq to quote control character to search for it.键入 Cq 以引用控制字符进行搜索。 Type Cx 8 RET to add a character to search by Unicode name, with completion.键入 Cx 8 RET 以添加一个字符以按 Unicode 名称搜索,并完成。 Cg while searching or when search has failed cancels input back to what has been found successfully.搜索时或搜索失败时的 Cg 取消对已成功找到的内容的输入。 Cg when search is successful aborts and moves point to starting point.搜索成功时 Cg 中止并将点移动到起点。

If you try to exit with the search string still empty, it invokes nonincremental search.如果您尝试在搜索字符串仍为空的情况下退出,则会调用非增量搜索。

Type Mc to toggle search case-sensitivity.键入 Mc 以切换搜索是否区分大小写。 Type Ms i to toggle search in invisible text.键入 Ms i 以在不可见文本中切换搜索。 Type Mr to toggle regular-expression mode.键入 Mr 以切换正则表达式模式。 Type Ms w to toggle word mode.键入 Ms w 切换单词模式。 Type Ms _ to toggle symbol mode.键入 Ms _ 以切换符号模式。 Type Ms ' to toggle character folding.键入 Ms ' 切换字符折叠。

Type Ms SPC to toggle whitespace matching.键入 Ms SPC 以切换空白匹配。 In incremental searches, a space or spaces normally matches any whitespace defined by the variable 'search-whitespace-regexp';在增量搜索中,一个或多个空格通常匹配由变量“search-whitespace-regexp”定义的任何空格; see also the variables 'isearch-lax-whitespace' and 'isearch-regexp-lax-whitespace'.另请参见变量“isearch-lax-whitespace”和“isearch-regexp-lax-whitespace”。

Type Ms e to edit the search string in the minibuffer.键入 Ms e 以编辑 minibuffer 中的搜索字符串。

Also supported is a search ring of the previous 16 search strings.还支持前 16 个搜索字符串的搜索环。 Type Mn to search for the next item in the search ring.键入 Mn 以在搜索环中搜索下一项。 Type Mp to search for the previous item in the search ring.键入 Mp 以在搜索环中搜索上一项。 Type CMi to complete the search string using the search ring.键入 CMi 以使用搜索环完成搜索字符串。

Type M-% to run 'query-replace' with string to replace from last search string.键入 M-% 以使用要从最后一个搜索字符串替换的字符串运行“查询替换”。 Type CM-% to run 'query-replace-regexp' with the last search string.键入 CM-% 以使用最后一个搜索字符串运行“query-replace-regexp”。 Type Ms o to run 'occur' that shows the last search string.键入 Ms o 以运行显示最后一个搜索字符串的“发生”。 Type Ms hr to run 'highlight-regexp' that highlights the last search string.键入 Ms hr 以运行突出显示最后一个搜索字符串的“highlight-regexp”。

Type Ch b to display all Isearch key bindings.键入 Ch b 以显示所有 Isearch 键绑定。 Type Ch k to display documentation of Isearch key.键入 Ch k 以显示 Isearch 键的文档。 Type Ch m to display documentation of Isearch mode.键入 Ch m 以显示 Isearch 模式的文档。

Adopted Jackson's Answer for Swiper :采用杰克逊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 .此外,如果没有选定的区域,它只会退回到常规swiper-isearch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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