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.
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.
Answered 2023-09-20 21:00:26
\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. - anyone 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. - anyone set smartcase
which will automatically switch to a case-sensitive search if you use any capital letters. - anyone set smartcase
applies only when set ignorecase
is already active. I was stumped on this for a while. See Vim Tips. - anyone \c
versus \C
- anyone 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'
Answered 2023-09-20 21:00:26
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. - anyone smartcase
to work, you also need set ignorecase
. Great tip though, thanks! - anyone :%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. - anyone I
flag on a substitution to force the pattern to be case-sensitive. Like :%s/lowercasesearch/replaceString/gI
. - anyone After "*" and "#" you can make 'smartcase' used by doing a "/" command, recalling the search pattern from history and hitting <Enter>.
- anyone 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
Answered 2023-09-20 21:00:26
\c
doesn't work in vi. - anyone You can issue the command
:set ignorecase
and after that your searches will be case-insensitive.
Answered 2023-09-20 21:00:26
You can use in your vimrc
those commands:
set ignorecase
- All your searches will be case insensitiveset smartcase
- Your search will be case sensitive if it contains an uppercase letterYou 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).
Answered 2023-09-20 21:00:26
set smartcase
does not perform case insensitive searches if I do not use uppercase letters...is that normal? - anyone To switch between case sensitive and insensitive search I use this mapping in my .vimrc
nmap <F9> :set ignorecase! ignorecase?
Answered 2023-09-20 21:00:26
ignorecase?
shows you the current state of the flag. (in the command line) - anyone ignorecase?
so you know the current state. - anyone <Cr>
at the end, so the command is validated, and you don't have to hit the enter key (at least with neovim). - anyone vim[grep]
command..:vimgrep /example\c/ &
Answered 2023-09-20 21:00:26
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
Answered 2023-09-20 21:00:26
:set ic!
will toggle - anyone As others suggested:
:set ic
But the cool stuff is You can toggle such modes with:
:set ic!
Answered 2023-09-20 21:00:26
I prefer to use \c
at the end of the search string:
/copyright\c
Answered 2023-09-20 21:00:26
put this command in your vimrc file
set ic
always do case insensitive search
Answered 2023-09-20 21:00:26
set noic
. - anyone 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.
Answered 2023-09-20 21:00:26
smartcase
option? - anyone smartcase
- anyone Vim have 2 modes
1.edit mode
Search will work for normal mode
/\c for case sensitive
/\csearch
Answered 2023-09-20 21:00:26
You can set ignorecase
by default, run this in shell
echo "set ic" >> ~/.vimrc
Answered 2023-09-20 21:00:26
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 ----(---------------------)--
\c^.*A?.*$
^\c.*A?.*$
^.*\cA?.*$
^.*A\c?.*$
^.\c*A?.*$
^.*A?\c.*$
^.*A?.\c*$
^.*A?.*$\c
vim -version
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Jun 1 2020 06:42:35)
Included patches: 1-869Answered 2023-09-20 21:00:26
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 :
:help ignorecase
:q
I really hope the information provided would be helpful for someone.
Best regards,
Answered 2023-09-20 21:00:26