16

I occasionally search through files in vim or less using / or ? but as far as I can tell, the search patterns are case sensitive.

So for example, /foo won't find the same things that /FOO will. Is there an way way to make it less strict? How can I search in vim or less for a pattern that is NOT case sensitive?

  • 2
    In less searches are case insensitive unless you use a string that contains an upper case letter. So, foo will match Foo and FoO but Foo will only match Foo and not foo etc. Also see here: less is always case-insensitive. – terdon Mar 24 '15 at 17:48
  • 1
    @terdon Interesting! I'm struggling to understand the "why" of having less behave that way but I suppose that's a topic for another day. Thanks! – Mike B Mar 24 '15 at 17:51
  • Well, for one thing, I find it exceedingly useful when searching through man pages. I'd hate to have it case sensitive. There are quite a few posts about this, also have a look here: Can I force `man` to do lower case sensitive matching? and here: Case insensitive search in man pages. In fact, if you don't mind, I'll remove less from your question since that is covered quite nicely in the other posts and the answers you've received explain how to do it in vi. – terdon Mar 24 '15 at 17:53
  • @terdon Fine by me, although please consider leaving this comment dialogue in as I tend to use both less and vi and that info is valuable. – Mike B Mar 24 '15 at 18:03
  • As a point of clarification, I am indeed using vim (even though I'm typing vi). Apologies for the confusion. – Mike B Mar 24 '15 at 22:54
19

In vi or vim you can ignore case by :set ic, and all subsequent searches will consider the setting until you reset it by :set noic. In less there are options -i and -I to ignore case.

  • 8
    I would also like to recommend :set smartcase, in order to only search case sensitive when at least on uppercase character is used. – Bernhard Mar 24 '15 at 19:34
  • 3
    Just for the record, smartcase is vim-only. But OP's "vi" is likely vim, so it's worth mentioning/trying. – Random832 Mar 24 '15 at 19:40
  • @Random832 Yup. Correct. Updated the original question to mention vim instead of vi. – Mike B Mar 24 '15 at 22:54
25

Use the \c escape sequence:

/foo\c

See also: https://stackoverflow.com/questions/2287440/how-to-do-case-insensitive-search-in-vim

  • 6
    Note that this can go anywhere in the pattern, so if you decide after starting to type that you want the pattern to be case-insensitive, you can just add \c wherever you are—e.g., /some_very_lo\cng_text. – wchargin Mar 25 '15 at 3:13
2

In your .vimrc file, include

set ignorecase

or

set ic

To re-enable case sensitivity, use :set ic!.

  • 1
    Hi! This looks OK, but it doesn't add much to the already-accepted answer, now, does it? – dhag Mar 26 '15 at 20:39
  • 1
    @dhag all of the answers provide a different way of accomplishing the same task. Setting the value in .vimrc allows the setting to persist for other vim sessions. – todgru Mar 26 '15 at 21:34
  • @dhag you're right in that this does nothing to solve the OP's issue in less. – todgru Mar 26 '15 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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