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.
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 ).
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.
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.
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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With