Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do case insensitive search in 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.

like image 500
Haiyuan Zhang Avatar asked Feb 18 '10 09:02

Haiyuan Zhang


People also ask

How do you grep a case-insensitive?

Case Insensitive Search By default, grep is case sensitive. This means that the uppercase and lowercase characters are treated as distinct. To ignore case when searching, invoke grep with the -i option (or --ignore-case ).

How do you do a case-insensitive search in Unix?

Case-insensitive file searching with the find command The key to that case-insensitive search is the use of the -iname option, which is only one character different from the -name option. The -iname option is what makes the search case-insensitive.

What is Smartcase in Vim?

Case-insensitive search in Vim. If you want this behavior by default, you can turn on the option: set ignorecase. There's also a so-called "smartcase" ( :help smartcase ) which works as case-insensitive if you only use lowercase letters; otherwise, it will search in case-sensitive mode.

Is VI case-sensitive?

Searches normally are case-sensitive: a search for "china" will not find "China." If you want vi to ignore case during a search, type :set ic . To change it back to the default, case-sensitive mode, type :set noic . If vi finds the requested string, the cursor will stop at its first occurrence.


2 Answers

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.

like image 122
Chinmay Kanchi Avatar answered Sep 27 '22 21:09

Chinmay Kanchi


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' 
like image 23
DrAl Avatar answered Sep 27 '22 23:09

DrAl