InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Doing More with less in bash
Bash

Doing More with less in bash

InfinityCoder February 19, 2017

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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Indenting Here-Documents in bash
The here-document is great, but it’s messing up your shell …

Indenting Here-Documents in bash

Getting Yes or No Input in bash
You need to get a simple yes or no input …

Getting Yes or No Input in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap