在 Vim 中搜索整个单词(破折号字符)
我知道要搜索整个单词,我应该使用 /\
。但对于破折号 (+U002d) 字符来说情况并非如此,并且 /\<-\>
总是失败。我也尝试了 /\<\%d45\>
但它也失败了。有人知道原因吗?
编辑2:正如@bobbogo提到的破折号不在'iskeyword'中,所以我添加:set isk+=-
和/\<-\>
> 有效!
Edit1: 我认为在 Vim 中 /\
仅对字母数字字符有效,我们不应该将它用于标点符号(请参阅 Edit2)。我应该改变我的问题并询问我们如何在整个世界中搜索标点符号,例如我希望我的搜索找到“a ? b”中的问号和“??”等模式和“abc?”不应该是有效的。
I know for searching a whole word I should use /\<mypattern\>
. But this is not true for dash (+U002d) character and /\<-\>
always fails. I also try /\<\%d45\>
and it fails too. anyone know the reason?
Edit2: As @bobbogo mentioned dash is not in 'iskeyword' so I add :set isk+=-
and /\<-\>
works!
Edit1: I think in Vim /\<word\>
only is valid for alphanumeric characters and we shouldn't use it for punctuation characters (see Edit2). I should change my question and ask how we can search punctuation character as a whole world for example I want my search found the question mark in "a ? b" and patterns like "??" and "abc?" shouldn't be valid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
\<
匹配非单词字符和单词字符之间的零宽度边界。什么是单词字符?它由 isk 选项 (:help isk
) 指定。由于
-
不在您的 isk 选项中,因此-
永远不能开始一个单词,因此\ <-
将永远匹配。我不知道你想要什么,但
/\>-\<
将匹配hello-word
中的破折号。\<
matches the zero-width boundary between a non-word character and a word character. What is a word character? It's specified by the isk option (:help isk
).Since
-
is not in your isk option, then-
can never start a word, thus\<-
will never match.I don't know what you want, but
/\>-\<
will match the dash inhello-word
.始终可以搜索正则表达式
\byyourwordhere\b
Could always search for the regex
\byourwordhere\b
正如OP所说。为了将破折号
-
包含到搜索中,只需执行:仅此而已。
示例:当您在
color-primary
的字母c
上按*
时,它将搜索整个变量名称,而不仅仅是对于颜色
。As OP said. In order to include dash
-
into search just execute:Thats all.
Example: When you press
*
over letterc
ofcolor-primary
it will search for entire variable name not just forcolor
.