Using regular expressions in “less”

case sensitivitylessregular expression

I'm trying to use a regular expression in the man page of Bash by using less.

I press / in less to enter a pattern, and I type z and press the Enter. I expected it to not match upper-case z (Z), but it does.

How do I make it not match Z? What kind of regular expressions are these that are not case sensitive?

Best Answer

It's explained in the man page for less.

The default action for REs is to ignore case if there are no uppercase characters present, but to act case-sensitively otherwise.

There are three modes available within less:

  1. Case context dependent: a search or RE without uppercase characters is considered to be case-insensitive, but a search or RE containing at least one uppercase character is considered to be case-sensitive. Examples: abc will match abc and aBC, but aBc will only match aBc and not abc or ABC. This is the default setting.
  2. Case sensitive: a search or RE pays full regard to the case of any letter. Example: abC will match only abC and not abc or ABC.
  3. Case insensitive: a search or RE pays no regard to the case of any letter. Example: abC will match any of abc, abC, or ABC.

You can toggle case sensitive comparisons with -I, and case context sensitive comparisons with -i.

The control can be specified in three ways:

  • On the command line, for example less -I bigfile.txt.
  • In the environment, for example export LESS=-i and later less bigfile.txt.
  • Within less itself, for example by starting less bigfile.txt and then typing -i.