You’d like to take better advantage of the features of the less pager.
Read the less manpage and use the $LESS variable with ~/.lessfilter and ~/.lesspipe files.
less takes options from the $LESS variable, so rather than creating an alias with your favorite options, put them in that variable.
It takes both long and short options, and any command-line options will override the variable. We recommend using the long options in the $LESS variable since they are easy to read.
For example:
1 |
export LESS="--LONG-PROMPT --LINE-NUMBERS --ignore-case --QUIET" |
But that is just the beginning. less is expandable via input preprocessors, which are simply programs or scripts that pre-process the file that less is about to display.
This is handled by setting the $LESSOPEN and $LESSCLOSE environment variables appropriately.
You could build your own, but save yourself some time and look into Wolfgang Friebel’s lesspipe.sh available at http://www-zeuthen.desy.de/~friebel/unix/lesspipe.html (but see the discussion below first).
The script works by setting and exporting the $LESSOPEN environment variable when run by itself:
1 2 3 |
$ ./lesspipe.sh LESSOPEN="|./lesspipe.sh %s" export LESSOPEN |
So you simply run it in an eval statement, like eval $(/path/to/lessfilter.sh) or eval /path/to/lessfilter.sh
, and then use less as usual.
The list of supported formats for version 1.53 is:
gzip, compress, bzip2, zip, rar, tar, nroff, ar archive, pdf, ps, dvi, shared library, executable, directory, RPM, Microsoft Word, OpenOffice 1.x and OASIS (OpenDocument) formats, Debian, MP3 files, image formats (png, gif, jpeg, tiff, …), utf-16 text, iso images and filesystems on removable media via /dev/xxx
But there is a catch. These formats require various external tools, so not all features in the example lesspipe.sh will work if you don’t have them.
The package also contains ./configure (or make) scripts to generate a version of the filter that will work on your system, given the tools that you have available.
less is unique in that it is a GNU tool that was already installed by default on every single test system we tried—every one. Not even bash can say this.
And version differences aside, it works the same on all of them. Quite a claim to fame.
However, the same cannot be said for lesspipe* and less open filters. We found other versions, with wildly variable capabilities, besides the ones listed above.
• Red Hat has a /usr/bin/lesspipe.sh that can’t be used like eval lesspipe
.
• Debian has a /usr/bin/lesspipe that can be eval’ed and also supports additional filters via a ~/.lessfilter file.
• SUSE Linux has a /usr/bin/lessopen.sh that can’t be eval’ed.
• FreeBSD has a trivial /usr/bin/lesspipe.sh (no eval, .Z, .gz, or .bz2).
• Solaris, HP-UX, the other BSDs, and the Mac have nothing by default
To see if you already have one of these, try this on your systems. This Debian system has the Debian lesspipe installed but not in use (since $LESSOPEN is not defined):
1 2 3 |
$ type lesspipe.sh; type lesspipe; set | grep LESS -bash3: type: lesspipe.sh: not found lesspipe is /usr/bin/lesspipe |
This Ubuntu system has the Debian lesspipe installed and in use:
1 2 3 4 5 |
$ type lesspipe.sh; type lesspipe; set | grep LESS -bash: type: lesspipe.sh: not found lesspipe is hashed (/usr/bin/lesspipe) LESSCLOSE='/usr/bin/lesspipe %s %s' LESSOPEN='| /usr/bin/lesspipe %s' |
We recommend that you download, configure, and use Wolfgang Friebel’s lesspipe. sh because it’s the most capable.
We also recommend that you read the less manpage because it’s very interesting.