如何在 Vim 中进行不区分大小写的搜索
我想搜索大写单词,例如文件中的 COPYRIGHT。我尝试执行如下搜索:
/copyright/i # Doesn't work
但它不起作用。我知道在 Perl 中,如果我将 i
标志赋予正则表达式,它将把正则表达式变成不区分大小写的正则表达式。看来 Vim 有自己的方式来指示不区分大小写的正则表达式。
I'd like to search for an upper case word, for example COPYRIGHT in a file. I tried performing a search like:
/copyright/i # Doesn't work
but it doesn't work. I know that in Perl, if I give the i
flag into a regex it will turn the regex into a case-insensitive regex. It seems that Vim has its own way to indicate a case-insensitive regex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您可以在模式中的任何位置使用
\c
转义序列。例如:/\ccopyright
或/copyright\c
甚至/copyri\cght
执行相反操作(区分大小写 em> 匹配),请使用
\C
(大写 C)。You can use the
\c
escape sequence anywhere in the pattern. For example:/\ccopyright
or/copyright\c
or even/copyri\cght
To do the inverse (case sensitive matching), use
\C
(capital C) instead.除了对
\c
和ignorecase
的建议之外,我发现smartcase
非常有用。如果您搜索包含大写字符的内容,它将进行区分大小写的搜索;如果您搜索纯小写的内容,它将执行不区分大小写的搜索。您可以使用\c
和\C
来覆盖它:请参阅:
As well as the suggestions for
\c
andignorecase
, I find thesmartcase
very useful. If you search for something containing uppercase characters, it will do a case sensitive search; if you search for something purely lowercase, it will do a case insensitive search. You can use\c
and\C
to override this:See:
您可以在搜索之前在 Vim 中设置
ic
选项:要返回区分大小写的搜索,请使用:
ic
是ignorecase
的简写You can set the
ic
option in Vim before the search:To go back to case-sensitive searches use:
ic
is shorthand forignorecase
您可以发出该命令
,之后您的搜索将不区分大小写。
You can issue the command
and after that your searches will be case-insensitive.
您可以在
vimrc
中使用这些命令:setignorecase
- 所有搜索都将不区分大小写set smartcase
- 如果满足以下条件,您的搜索将区分大小写它包含一个大写字母如果您想使用
smartcase
提供的功能,则需要设置ignorecase
。我最近写了一篇关于 Vim 搜索命令的文章(内置命令和最好的搜索插件有效)。
You can use in your
vimrc
those commands:set ignorecase
- All your searches will be case insensitiveset smartcase
- Your search will be case sensitive if it contains an uppercase letterYou need to set
ignorecase
if you want to use whatsmartcase
provides.I wrote recently an article about Vim search commands (both built in command and the best plugins to search efficiently).
要在区分大小写和不区分大小写的搜索之间切换,我在 .vimrc
nmap中使用此映射。 :设置忽略大小写!忽略大小写?
To switch between case sensitive and insensitive search I use this mapping in my .vimrc
nmap <F9> :set ignorecase! ignorecase?
默认情况下,vi 中的所有搜索都区分大小写。要进行不区分大小写的搜索,请进入命令模式(按 Escape 键),然后键入 -
:设置忽略大小写
您还可以输入 -
:set ic
作为缩写。要更改回区分大小写的模式,请键入-
命令模式下
:set noignorecase
或:set noic
By default, all searches in vi are case-sensitive. To do a case-insensitive search, go into command mode (press Escape), and type-
:set ignorecase
You can also type -
:set ic
as an abbreviation.To change back to case-sensitive mode, type-
:set noignorecase
or:set noic
in command mode好的旧
vim[grep]
命令..The good old
vim[grep]
command..正如其他人所建议的:
但最酷的是您可以通过以下方式切换此类模式:
As others suggested:
But the cool stuff is You can toggle such modes with:
我更喜欢在搜索字符串末尾使用
\c
:I prefer to use
\c
at the end of the search string:将此命令放入 vimrc 文件中
始终执行不区分大小写的搜索
put this command in your vimrc file
always do case insensitive search
正如 @huyz 提到的,有时所需的行为是使用不区分大小写的搜索,但使用区分大小写的替换。我的解决方案是:
当您点击
/
或?
时,它总是会添加\c
进行不区分大小写的搜索。As @huyz mention sometimes desired behavior is using case-insensitive searches but case-sensitive substitutions. My solution for that:
With that always when you hit
/
or?
it will add\c
for case-insensitive search.Vim 有 2 种模式
1.编辑模式
搜索适用于正常模式
/\c 区分大小写
/\c搜索
Vim have 2 modes
1.edit mode
Search will work for normal mode
/\c for case sensitive
/\csearch
你可以默认设置
ignorecase
,在shell中运行它You can set
ignorecase
by default, run this in shell请注意,在表达式中放置修饰符(例如“\c”)是有区别的:
无论接受的答案如何,它表明在正则表达式模式中放置 modyfier 的位置没有区别,它看起来确实很重要。
示例文本:
匹配
\c^.*A?.*$
^\c.*A?.*$
^.*\cA?.*$
^.*A\c?.*$
将输出:
不匹配
^.\c*A?.*$
^.*A?\c.*$
^.*A?.\c*$
^.*A?.*$\c
将输出:
vim -version
VIM - Vi IMproved 8.2(2019 年 12 月 12 日,2020 年 6 月 1 日 06:42:35 编译)包含的补丁:1-869
Note it is a difference where you place modifiers such as "\c" in your expresion:
Regardless from the accepted answers, which states that it is no difference of where to place modyfier in a regex pattern, its looks like it actually does matter.
example text:
Match
\c^.*A?.*$
^\c.*A?.*$
^.*\cA?.*$
^.*A\c?.*$
will output:
No match
^.\c*A?.*$
^.*A?\c.*$
^.*A?.\c*$
^.*A?.*$\c
will output:
vim -version
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Jun 1 2020 06:42:35)Included patches: 1-869
一些重要的信息,如果你想了解更多关于 vim 命令的信息,如下所述,你可以尝试以下步骤:
< img src="https://i.sstatic.net/pPzr5.png" alt="在此处输入图像描述">
我真的希望所提供的信息对某人有所帮助。
此致,
Some important information, if u want to find out more about the commands of vim, as mentioned below u can give a try the following steps :
I really hope the information provided would be helpful for someone.
Best regards,