2073

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.

0

16 Answers 16

2667

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.

7
  • 582
    Also, \c can appear anywhere in the pattern, so if you type a pattern and then decide you wanted a case-insensitive search, just add a \c at the end. Commented Feb 18, 2010 at 9:20
  • 334
    I like to add set ignorecase for case-insensitive searching in my vimrc, and I can use \C to do a case-sensitive search similar to what @AlokSinghal mentioned. Commented Aug 5, 2013 at 18:23
  • 212
    There's also set smartcase which will automatically switch to a case-sensitive search if you use any capital letters.
    – Zaz
    Commented Jun 5, 2015 at 22:22
  • 128
    Just want to add to Zaz's comment. set smartcase applies only when set ignorecase is already active. I was stumped on this for a while. See Vim Tips.
    – Tan Wang
    Commented Jul 8, 2016 at 20:58
  • 13
    @coderMe, It's the capitalization, \c versus \C Commented Oct 28, 2016 at 13:25
851

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'
6
  • 102
    The problem with ignorecase is that it affects substitutions as well as searches. I find that it makes sense to have (smart) case-insensitive searches but case-sensitive substitutions by default. But there's no way to do that that I know.
    – huyz
    Commented Jul 2, 2011 at 14:18
  • 174
    Worth noting that for smartcase to work, you also need set ignorecase. Great tip though, thanks!
    – Skilldrick
    Commented Mar 28, 2012 at 18:59
  • 15
    I believe you could just use a \C in your search expression for substitutions, like this: :%s/lowercasesearch\C/replaceString/g. This doesn't create the default functionality you desire, but it does allow you to force case-sensitivity for replacements while still benefiting from smartcase when searching. Commented Oct 15, 2012 at 23:44
  • 33
    You can also set the I flag on a substitution to force the pattern to be case-sensitive. Like :%s/lowercasesearch/replaceString/gI. Commented Aug 21, 2013 at 22:19
  • 1
    Note from the help page (useful if you are "*" addicted like me): After "*" and "#" you can make 'smartcase' used by doing a "/" command, recalling the search pattern from history and hitting <Enter>.
    – mcella
    Commented Nov 6, 2014 at 17:21
365

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

1
  • 2
    also your only option if you're unlucky enough to still use Vi instead of Vim. \c doesn't work in vi.
    – bluppfisk
    Commented Dec 12, 2019 at 13:09
71

You can issue the command

:set ignorecase

and after that your searches will be case-insensitive.

63

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

2
  • 2
    It seems that set smartcase does not perform case insensitive searches if I do not use uppercase letters...is that normal?
    – caneta
    Commented Feb 21, 2018 at 11:18
  • 2
    Ok, just read below that you have to both set ignorecase and smartcase to have it work. Sorry about that!
    – caneta
    Commented Feb 21, 2018 at 11:21
51

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

nmap <F9> :set ignorecase! ignorecase?

4
  • 17
    Yes, but ignorecase? shows you the current state of the flag. (in the command line) Commented Jan 22, 2016 at 22:10
  • 1
    Thank you! To me, this is the most useful answer on the page: a way to quickly toggle between the two modes depending on what you're searching for at that exact moment. Commented May 1, 2019 at 8:28
  • Yes, it's better to have ignorecase? so you know the current state.
    – COil
    Commented Feb 18, 2023 at 16:35
  • BTW, it's better to add <Cr> at the end, so the command is validated, and you don't have to hit the enter key (at least with neovim).
    – COil
    Commented Feb 18, 2023 at 16:42
33

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

1
  • 13
    :set ic! will toggle
    – robmsmt
    Commented Feb 8, 2021 at 18:38
29

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

3
  • Cannot open file "&" Commented Jun 13, 2018 at 7:41
  • I guess he they meant % instead of %.
    – oarfish
    Commented Apr 22, 2020 at 10:20
  • Typo in the actual command the correct one is as per the gif attached. vimgrep /example\c %
    – Santh
    Commented Apr 3 at 13:49
27

As others suggested:

:set ic

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

:set ic!
21

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

/copyright\c
19

put this command in your vimrc file

set ic 

always do case insensitive search

1
  • 4
    I don't know why but my vim is set to case-insensitive by default, to counter this. use set noic. Commented Mar 22, 2016 at 13:22
10

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.

2
  • 1
    Not a bad suggestion, but does it prevent you from using the smartcase option? Commented Feb 26, 2016 at 23:53
  • Looks like it would since \c overrides smartcase Commented Jan 10, 2017 at 1:57
5

Vim have 2 modes

1.edit mode

  1. normal mode( Esc )

Search will work for normal mode

/\c for case sensitive

/\csearch

1
  • What about visual mode?
    – user202729
    Commented Jun 27, 2018 at 4:18
3

You can set ignorecase by default, run this in shell

echo "set ic" >> ~/.vimrc
-1

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
-2

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,

Not the answer you're looking for? Browse other questions tagged or ask your own question.