Friday, April 29, 2011

How to extend the "less" command behavior?

In normal cases, I suppose everyone only uses the "less" command to read the content of plain text files.

But on Gentoo, it seems that the less command line is a little more powerful than the one I use on other Linux distributions, or Cygwin, Mac OS X... the less command on Gentoo is able to list the content of compressed archive files, the ELF header and other information of executable files, and maybe more... I can't remember on what platform it was, but I think it is also possible to read PDF files with some versions of "less" (or maybe was it just a dream?).

So I wonder if there is a way to easily make extensions for the less command, for example to make it able to read (display in a nice way) some format of binary files. I searched for information about this on Google, but didn't find anything.

From stackoverflow
  • less is open, if you don't like the one you have, just grab a more up to date one.

  • According to this page and this page, less already has PDF support, at least in that version

    I tried less'ing a couple different PDF files, at it looks like some are graphics based and some are text based. I was able to read the text based ones, but not so much the graphics ones as they were all commands an not so much text.

  • Just did a "man less" on my Mac. less can work with input preprocessors. The man page had an example of setting up a preprocessor shell script to work with archives. The preprocessor recognizes the extension and fed the uncompressed file into less. You specify the preprocessor script with environment variables LESSOPEN and LESSCLOSE.

    Maybe your full-featured system had some scripts specified by LESSOPEN. Look in your environment for LESSOPEN.

    Cool, I learned something new looking for an answer.

    Note: I usually use "readelf -h" to dump ELF headers. Very useful when I am cross-compiling and need to verify the library format.

  • Have you looked at lesspipe.sh?

    Actually, lesspipe.sh only processes file or url before piping to less. BTW, I'm not using this, but I often type ps2ascii some.pdf | less to read pdf files.

  • Try "man lessfile" and "man lesspipe" for documentation on this behaviour.

    On Ubuntu, less processes PDFs into plain text by default. (I actually find this annoying since when I type "less something.pdf" it's usually because I want to look at the raw PDF. Easily worked around with "less < something.pdf" for the single-file case, though.)

0 comments:

Post a Comment