How to do case insensitive search in Vim

我的未来我决定 提交于 2019-11-30 10:02:02

问题


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.


回答1:


You need to use the \c escape sequence. So:

/\ccopyright

To do the inverse (case sensitive matching), use \C instead.




回答2:


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'



回答3:


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




回答4:


You can issue the command

:set ignorecase

and after that your searches will be case-insensitive.




回答5:


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

nmap <F9> :set ignorecase! ignorecase?




回答6:


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).




回答7:


As others suggested:

:set ic

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

:set ic!



回答8:


put this command in your vimrc file

set ic 

always do case insensitive search




回答9:


The good old vim[grep] command..

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




回答10:


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

/copyright\c



回答11:


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.




回答12:


Vim have 2 modes

1.edit mode

  1. normal mode( Esc )

Search will work for normal mode

/\c for case sensitive

/\csearch




回答13:


You can set ignorecase by default, run this in shell

echo "set ic" >> ~/.vimrc


来源:https://stackoverflow.com/questions/2287440/how-to-do-case-insensitive-search-in-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!