Saturday, August 20, 2011

LESSOPEN Powers Up Less

A really useful feature of the Unix less pager is LESSOPEN which is the "input preprocessor" for less. This is a script, defined in the LESSOPEN environment variable, which is invoked before the file is opened. It gives you the chance to modify the way the contents of the file are displayed. Why would you want to do this? The most common reason is to uncompress files before you view them, allowing you to less GZ files. But it also allows you to list the contents of zip files and other archives. I like to use it to format XML files and to view Java class files by invoking jad.

You can download a really useful LESSOPEN script from and then extend it if necessary.

To use it, simply add export LESSOPEN="|/path/to/bin/ %s" to your bashrc.

You can then less:

  • directories
  • compressed files
  • archives, to list the files contained in them
  • files contained in archives e.g. less
  • binary files

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.