You probably know about less: it is a standard tool that allows scrolling up and down in documents that do not fit on a single screen. Less has a very handy feature, which can be turned on by invoking it with the
-i flag. This causes less to ignore case when searching. For example, ‘udf’ will find ‘udf’, ‘UDF’, ‘UdF’, and any other combination of upper-case and lower-case. If you’re used to searching in a web browser, this is probably what you want. But less is even more clever than that. If your search pattern contains upper-case letters, the ignore-case feature will be disabled. So if you’re looking for ‘QXml’, you will not be bothered by matches for the lower-case ‘qxml’. (This is equivalent to ignorecase + smartcase in vim.)
So how do we take this useful feature and make it permanent, so that we don’t have to remember to type
less -i every time? We could create an
alias less='less -i'. But there are tools (such as git-log) that invoke less on their own, and they will not know about the ignore-case option. It would be better if we could tell less that we always want that feature on, regardless of startup flags. This article will teach you how to do that.
On startup, less reads a configuration file, which on Unix systems is in
~/.less. For some ungodly reason, this is a binary file, and you have to use the lesskey program to generate it. First, put the following in your
LESS = -i -R
-i stands for the ignore-case option we discussed above, and
-R is something to handle color control characters correctly (e.g. for git-log). After that, run lesskey. It will read your
~/.lesskey and generate
~/.less. Now you can start less without any flags, and it will run with ignore-case turned on. Lesskey can be used for more complicated things, such as setting up new key bindings. Read the manual page for more details.
Some time after I set this up, I forgot all about it. I came to expect that this feature would always be on. One day I ran less as root to look at some logs, and I couldn’t find what I was looking for. Oops. Let’s make this a system-wide setting. You could repeat the same procedure for root and any other users on your system, or you could copy your
~/.less file to
/etc/sysless, which is the system-wide lesskey.
- In case you ever need to disable ignore-case temporarily, you can start less with the
-+ioption, or you can type
-+iin less after it started. Other options can be enabled (dash) or disabled (dash plus) in the same way.
- Less uses many of vi’s keyboard shortcuts. If you find yourself at a weird keyboard where Home and End do not work,
Gwill do the job.
F(for ‘follow’) in less will allow you to see new messages that are appended to a log, for example.