22 april 2016

The 'one day one manpage concept' is simple: I randomly select one manpage and explore the associated command.

Today, I will describe the lesspipe utility. First, a man lesspipe :

lessfile, lesspipe - "input preprocessor" for less. lessfile and lesspipe are programs that can be used to modify the way the contents of a file are displayed in less

Lesspipe allows to manipulate content of a file before sending it to less. Why ? The answer is easy. have you ever tried to launch less on an image, a .deb, .rpm, .tar.gz…​ ? You may have some problems. Lesspipe allow you (among others things) to enrich less to explore new file types with it.

To activate Lesspipe, just launch (or add it on your .bachrc or equivalent) :

eval "$(lesspipe)"

How it works ?

Launch lesspipe on a terminal. You will have :

export LESSOPEN="| /usr/bin/lesspipe %s";
export LESSCLOSE="/usr/bin/lesspipe %s %s";

Two variables are defined: LESSOPEN and LESSCLOSE.

The less manpage indicates, in the INPUT PREPROCESSOR section, that the LESSOPEN variable is used to activate a preprocessor for less. This proprocessor receives the file name sent to less, creates a temporary file and output the temporary file name. Next, less will use the temporary file.

The preprocessor script can also directly send to less information, without creating a temporary file. In this case, the stdout of the LESSOPEN script will be sent to less. The less man page says:

It is also possible to set up an input preprocessor to pipe the file data directly to less, rather than putting the data into a replacement file.

[…​] To use an input pipe, make the first character in the LESSOPEN environment variable a vertical bar (|) to signify that the input pre-processor is an input pipe.

In our case, lesspipe will not create a temporary file, because the LESSOPEN variable begins with |.

So, when less is called, the /usr/bin/lesspipe script will be called with the file name as a parameter. Let’s try to call /usr/bin/lesspipe manually, on a file for example:

$ lesspipe myimage.png
myimage.png PNG 254x29 1920x1080+12+134 8-bit sRGB 3.69KB 0.000u 0:00.000

Lesspipe returns some information about the image. These information will be passed to less and displayed.

For LESSCLOSE, the less man page says:

When less closes a file opened in such a way, it will call another program, called the input postprocessor, which may perform any desired clean-up action.

LESSCLOSE takes care of the cleanup. The script referenced by LESSCLOSE receives 2 parameters: The original file name (on which less was called), and the temporary file name (generated by the LESSOPEN script if needed).

We can no use less on many file types. For example on .tar.gz their contents, or .deb, .rpm, .png…​ files.


source-highlight add syntax highlighting to less. For install it (for debian), just do :

sudo apt-get install source-highlight

You will now have this script: /usr/share/source-highlight/src-hilite-lesspipe.sh

You can now use this script as a less preprocessor:

export LESSOPEN="| /usr/share/source-highlight/src-hilite-lesspipe.sh %s"

less -R [your file] will now open the file with syntax highlighting.

In conclusion, with these two variables (LESSOPEN and LESSCLOSE), you can easily write your own preprocessor scripts !

Top of page