24

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?

Mike B
  • 8,769
  • 24
  • 70
  • 96
  • 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](http://unix.stackexchange.com/q/116395). – 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?](http://unix.stackexchange.com/q/124282) and here: [Case insensitive search in man pages](http://unix.stackexchange.com/q/101295). 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

3 Answers3

33

Use the \c escape sequence:

/foo\c

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

  • 8
    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 03:13
29

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.

Random832
  • 10,476
  • 1
  • 34
  • 40
Janis
  • 14,014
  • 3
  • 25
  • 42
  • 10
    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
  • 4
    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
3

In your .vimrc file, include

set ignorecase

or

set ic

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

todgru
  • 31
  • 3
  • 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