How to do case insensitive search in Vim

SearchVimCase Insensitive

Search Problem Overview


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.

Search Solutions


Solution 1 - Search

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.

Solution 2 - Search

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'

Solution 3 - Search

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

Solution 4 - Search

You can issue the command

:set ignorecase

and after that your searches will be case-insensitive.

Solution 5 - Search

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

nmap <F9> :set ignorecase! ignorecase?

Solution 6 - Search

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

Solution 7 - Search

As others suggested:

:set ic

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

:set ic!

Solution 8 - Search

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

Solution 9 - Search

put this command in your vimrc file

set ic 

always do case insensitive search

Solution 10 - Search

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

/copyright\c

Solution 11 - Search

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

Solution 12 - Search

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.

Solution 13 - Search

Vim have 2 modes

1.edit mode

  1. normal mode( Esc )

Search will work for normal mode

/\c for case sensitive

/\csearch

Solution 14 - Search

You can set ignorecase by default, run this in shell

echo "set ic" >> ~/.vimrc

Solution 15 - Search

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

Solution 16 - Search

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,

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHaiyuan ZhangView Question on Stackoverflow
Solution 1 - SearchChinmay KanchiView Answer on Stackoverflow
Solution 2 - SearchDrAlView Answer on Stackoverflow
Solution 3 - SearchNathan FellmanView Answer on Stackoverflow
Solution 4 - SearchPaolo TedescoView Answer on Stackoverflow
Solution 5 - SearchvbdView Answer on Stackoverflow
Solution 6 - SearchMatthieuView Answer on Stackoverflow
Solution 7 - SearchThomasView Answer on Stackoverflow
Solution 8 - SearchMickView Answer on Stackoverflow
Solution 9 - SearchWALID BELRHALMIAView Answer on Stackoverflow
Solution 10 - SearchNick TsaiView Answer on Stackoverflow
Solution 11 - SearchYogesh JilhawarView Answer on Stackoverflow
Solution 12 - SearchpbogutView Answer on Stackoverflow
Solution 13 - SearchGowthaman DView Answer on Stackoverflow
Solution 14 - SearchSteely WingView Answer on Stackoverflow
Solution 15 - SearchDevWLView Answer on Stackoverflow
Solution 16 - SearchManuel LazoView Answer on Stackoverflow