Linux distributions nowadays have a default LESSOPEN set to /usr/bin/lesspipe which allows one to easily inspect e.g. zipped files or PDF files.

Now I wanted to see whether I can add syntax highlighting to less. This is easily accomplished with source-highlight. I used LESSOPEN="| /usr/bin/source-highlight -i %s -q -f esc" and it works fine.

But now the problem becomes: How do I get the behavior of both lesspipe and source-highlight? Simply appending one after the other doesn't work:

$ export LESSOPEN="| /usr/bin/lesspipe %s | /usr/bin/source-highlight -i %s -q -f esc"
$ less foo
Invalid LESSOPEN variable

It also doesn't make sense so this is not really surprising.

Is it possible to set this up without writing elaborate scripts? The best case would be to have chaining, i.e., provide syntax highlight for a zipped file as well.

  • What are actual pipe1 and pipe2 you used? – cuonglm Mar 20 '15 at 17:31
  • @cuonglm I added the commands. – musiKk Mar 20 '15 at 19:24
  • Try export LESSOPEN="| /usr/bin/lesspipe %s | /usr/bin/source-highlight -q -f esc", this will prevent the error, but I think won't work as you expected. – cuonglm Mar 20 '15 at 19:27

The command in the LESSOPEN variable after the initial pipe is executed by the shell indicated by the SHELL environment variable and must produce the desired content on its standard output.

You can pass the output of lesspipe as input to another command that reads from standard input and writes to standard output. However, in order for source-highlight to read from its standard input, it needs to be told what language to highlight for, so source-highlight doesn't work this way.

When lesspipe doesn't recognized a file format, it prints nothing, which less interprets as an indication to read the original file. If you have ifne from Joey Hess's moreutils, you can use it to detect this case and try source-highlight instead. You'll need to save the original file name in a variable so that you can use it twice.

LESSOPEN='|file=%s; /usr/bin/lesspipe "$file" | /usr/bin/ifne -n /usr/bin/source-highlight -i "$file" -o STDOUT 2>/dev/null'

Note that you'll need to include -r or -R in $LESS or type -r or -R after starting less in order for less to render the colors.

  • Thanks! Did not know about lessutils yet, they look nice. We're 90% there. Syntax highlight does not yet work for files that went through lesspipe but I'm sure I can make it work somehow. – musiKk Mar 23 '15 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.