简体   繁体   English

emacs空间正则表达式搜索

[英]emacs space regex search

I want to search for x spaces at the start of a line, using interactive regular search ^SPC< selects all lines starting with a variable number of spaces. 我想在一行的开头搜索x个空格,使用交互式常规搜索^SPC<选择以可变数量的空格开头的所有行。 According to emacs wiki 根据emacs Wiki

 (setq search-whitespace-regexp " ")

Should solve this, but this reverses behavior: it selects one space when i'm entering multiple spaces in my search. 应该解决这个问题,但是这会逆转行为:当我在搜索中输入多个空格时,它将选择一个空格。 I do not remember this behavior from earlier emacs (using 23.2 now). 我不记得以前的emacs(现在使用23.2)的这种行为。 Is there a way to make interactive search select one space when entered one space and x spaces when entered x spaces? 有没有一种方法可以使交互式搜索在输入一个空格时选择一个空格,而在输入x个空格时选择x个空格?

cheers Jeroen 欢呼耶隆

I think the behavior you observe is the intended one. 我认为您观察到的行为是预期的行为。 From the documentation of search-whitespace-regexp : search-whitespace-regexp的文档中:

If non-nil, regular expression to match a sequence of whitespace chars. 如果不为null,则使用正则表达式来匹配一系列空白字符。 [...] When you put a space or spaces in the incremental regexp, it stands for this. [...]当您在增量正则表达式中放置一个或多个空格时,就代表了这一点。

Note the second sentence - whenever you put a single (or multiple) space character(s) in your regex, that gets interpreted as if you entered the value of search-whitespace-regexp . 请注意第二句话-每当您在正则表达式中添加单个(或多个)空格字符时,该解释就如同您输入search-whitespace-regexp的值一样。 Since you defined that variable to be a single space character, one or multiple space character(s) in your regexp will only match a single space character in your buffer. 由于您已将该变量定义为单个空格字符,因此正则表达式中的一个或多个空格字符将仅与缓冲区中的单个空格字符匹配。

Probably the easiest way to achieve what you want is thus to simply set the variable to nil , in which case space characters are no longer treated in a special way in interactive regexp searches. 因此,可能最简单的方法是将变量设置为nil ,在这种情况下,在交互式正则表达式搜索中不再以特殊方式处理空格字符。 A single space character in your regex will only match a single space character in the buffer. 正则表达式中的单个空格字符将仅与缓冲区中的单个空格字符匹配。

(setq search-whitespace-regexp nil)

您可以尝试^[ ]\\{5\\}<以获得以5个空格开头的行,然后是<

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

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