如何在 Vim 中进行不区分大小写的搜索

发布于 2024-08-22 04:32:08 字数 210 浏览 8 评论 0原文

我想搜索大写单词,例如文件中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

所有深爱都是秘密 2024-08-29 04:32:08

您可以在模式中的任何位置使用 \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.

抹茶夏天i‖ 2024-08-29 04:32:08

除了对 \cignorecase 的建议之外,我发现 smartcase 非常有用。如果您搜索包含大写字符的内容,它将进行区分大小写的搜索;如果您搜索纯小写的内容,它将执行不区分大小写的搜索。您可以使用 \c\C 来覆盖它:

:set ignorecase
:set smartcase
/copyright      " Case insensitive
/Copyright      " Case sensitive
/copyright\C    " Case sensitive
/Copyright\c    " Case insensitive

请参阅:

:help /\c
:help /\C
:help 'smartcase'

As well as the suggestions for \c and ignorecase, I find the smartcase 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:

:set ignorecase
:set smartcase
/copyright      " Case insensitive
/Copyright      " Case sensitive
/copyright\C    " Case sensitive
/Copyright\c    " Case insensitive

See:

:help /\c
:help /\C
:help 'smartcase'
人生戏 2024-08-29 04:32:08

您可以在搜索之前在 Vim 中设置 ic 选项:

:set ic

要返回区分大小写的搜索,请使用:

:set noic

icignorecase 的简写

You can set the ic option in Vim before the search:

:set ic

To go back to case-sensitive searches use:

:set noic

ic is shorthand for ignorecase

反目相谮 2024-08-29 04:32:08

您可以发出该命令

:set ignorecase

,之后您的搜索将不区分大小写。

You can issue the command

:set ignorecase

and after that your searches will be case-insensitive.

赴月观长安 2024-08-29 04:32:08

您可以在 vimrc 中使用这些命令:

  • setignorecase - 所有搜索都将不区分大小写
  • set smartcase - 如果满足以下条件,您的搜索将区分大小写它包含一个大写字母

如果您想使用 smartcase 提供的功能,则需要设置 ignorecase

我最近写了一篇关于 Vim 搜索命令的文章(内置命令和最好的搜索插件有效)。

You can use in your vimrc those commands:

  • set ignorecase - All your searches will be case insensitive
  • set smartcase - Your search will be case sensitive if it contains an uppercase letter

You need to set ignorecase if you want to use what smartcase provides.

I wrote recently an article about Vim search commands (both built in command and the best plugins to search efficiently).

白云不回头 2024-08-29 04:32:08

要在区分大小写和不区分大小写的搜索之间切换,我在 .vimrc

nmap中使用此映射。 :设置忽略大小写!忽略大小写?

To switch between case sensitive and insensitive search I use this mapping in my .vimrc

nmap <F9> :set ignorecase! ignorecase?

寒冷纷飞旳雪 2024-08-29 04:32:08

默认情况下,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

画离情绘悲伤 2024-08-29 04:32:08

好的旧 vim[grep] 命令..

:vimgrep /example\c/ &
  • \c 不区分大小写
  • \C 区分大小写
  • % 是在当前缓冲区中搜索

在此处输入图像描述

The good old vim[grep] command..

:vimgrep /example\c/ &
  • \c for case insensitive
  • \C for case sensitive
  • % is to search in the current buffer

enter image description here

山有枢 2024-08-29 04:32:08

正如其他人所建议的:

:set ic

但最酷的是您可以通过以下方式切换此类模式:

:set ic!

As others suggested:

:set ic

But the cool stuff is You can toggle such modes with:

:set ic!
浊酒尽余欢 2024-08-29 04:32:08

我更喜欢在搜索字符串末尾使用 \c

/copyright\c

I prefer to use \c at the end of the search string:

/copyright\c
别闹i 2024-08-29 04:32:08

将此命令放入 vimrc 文件中

set ic 

始终执行不区分大小写的搜索

put this command in your vimrc file

set ic 

always do case insensitive search

递刀给你 2024-08-29 04:32:08

正如 @huyz 提到的,有时所需的行为是使用不区分大小写的搜索,但使用区分大小写的替换。我的解决方案是:

nnoremap / /\c
nnoremap ? ?\c

当您点击 /? 时,它总是会添加 \c 进行不区分大小写的搜索。

As @huyz mention sometimes desired behavior is using case-insensitive searches but case-sensitive substitutions. My solution for that:

nnoremap / /\c
nnoremap ? ?\c

With that always when you hit / or ? it will add \c for case-insensitive search.

倾其所爱 2024-08-29 04:32:08

Vim 有 2 种模式

1.编辑模式

  1. 正常模式( Esc )

搜索适用于正常模式

/\c 区分大小写

/\c搜索

Vim have 2 modes

1.edit mode

  1. normal mode( Esc )

Search will work for normal mode

/\c for case sensitive

/\csearch

清秋悲枫 2024-08-29 04:32:08

你可以默认设置ignorecase,在shell中运行它

echo "set ic" >> ~/.vimrc

You can set ignorecase by default, run this in shell

echo "set ic" >> ~/.vimrc
轮廓§ 2024-08-29 04:32:08

请注意,在表达式中放置修饰符(例如“\c”)是有区别的:

您可以在模式中的任何位置使用 \c 转义序列

无论接受的答案如何,它表明在正则表达式模式中放置 modyfier 的位置没有区别,它看起来确实很重要。

示例文本:

asdasdasdasdasd wiktor asdasdasdasd   
adasdasdasd wiktor asdasda ahjkjlkhjkl
asdasd asd asdasdasdasd iuuuu -       
asdjkkkkkkkaopbsdasda                 
wiktor ----(---------------------)--  

匹配

\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:

You can use the \c escape sequence anywhere in the pattern

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:

asdasdasdasdasd wiktor asdasdasdasd   
adasdasdasd wiktor asdasda ahjkjlkhjkl
asdasd asd asdasdasdasd iuuuu -       
asdjkkkkkkkaopbsdasda                 
wiktor ----(---------------------)--  

Match

\c^.*A?.*$
^\c.*A?.*$
^.*\cA?.*$
^.*A\c?.*$

will output:
enter image description here

No match

^.\c*A?.*$
^.*A?\c.*$
^.*A?.\c*$
^.*A?.*$\c

will output:
enter image description here
enter image description here

  • vim -version VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Jun 1 2020 06:42:35)
    Included patches: 1-869
傾城如夢未必闌珊 2024-08-29 04:32:08

一些重要的信息,如果你想了解更多关于 vim 命令的信息,如下所述,你可以尝试以下步骤:

  • 调用命令“help”,后跟一个空格,然后用 TAB 键补全单词,一旦你找到正确的命令按回车键。
:help ignorecase
  • 将显示如下信息:

在此处输入图像描述

  • 您将能够向前和向后移动,还可以观看简短命令,例如“ignorecase”( 'ic' ) 的情况。此外,另一个简短的示例可能是“smartcase”(“scs”等)的情况:

< img src="https://i.sstatic.net/pPzr5.png" alt="在此处输入图像描述">

  • 要离开文档,只需照常输入“:q”即可返回到“命令模式”。
:q

我真的希望所提供的信息对某人有所帮助。

此致,

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 :

  • invoke the command "help" follow by a space and then complete the word with TAB key, once u find the right command press return key.
:help ignorecase
  • information like the following will be displayed :

enter image description here

  • you will be able to move forward and backward and also watch the short command, such as the case of "ignorecase" ( 'ic' ). In addition, another short example could be the case of 'smartcase' ('scs' and some more) :

enter image description here

  • In order to leave of the documentation just type ":q" as usual and you will return to "command mode" .
:q

I really hope the information provided would be helpful for someone.

Best regards,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文