Unix Tip: Make ‘less’ more friendly

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 ~/.lesskey:

#env
LESS = -i -R

Here -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.

Bonus tips:

  • In case you ever need to disable ignore-case temporarily, you can start less with the -+i option, or you can type -+i in 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, g and G will do the job.
  • Pressing F (for ‘follow’) in less will allow you to see new messages that are appended to a log, for example.

3 Responses to Unix Tip: Make ‘less’ more friendly

  1. Alex says:

    Handy indeed, thanks for sharing!

    I especially like the smart case sensitivity; it makes sense – be case sensitive only when you have mixed case search patterns. Very humane, if you ask me.

  2. Ion Grosu says:

    Salut,

    După cum ştii în curînd are loc cea mai mare conferinţă ICT din Moldova. Moldova ICT Summit 2011. Organizatorii au decis să ofere acces liber la toate conferinţele pentru 5 bloggeri. Pentru a putea fi unul din cei 5 bloggeri acreditaţi te invităm să participi la un concurs simplu. Scrii despre IT şi despre conferinţă şi primeşti acces la eveniment şi posibilitatea de a cîştiga 5 instruiri în valoare de sute de euro fiecare.

    Sper să-ţi prindă bine această informaţie.

    Detalii pe site-ul evenimentului la categoria Media – Bloggeri. Cine ştie google are patru ochi :)

  3. Vibhav Sinha says:

    Or one can just use the LESS environment variable for saving less configurations.

%d bloggers like this: