1 lesspipe.sh, a preprocessor for less
2 ====================================
5 Author : Wolfgang Friebel, DESY (Wolfgang.Friebel AT desy.de)
8 Latest version available from:
9 http://freshmeat.net/projects/lesspipe/ or
10 http://sourceforge.net/projects/lesspipe/
12 The development version can be checked out using svn:
13 svn co svn://svn.code.sf.net/p/lesspipe/code/trunk
14 To report bugs or make proposals to improve lesspipe please contact
23 4. Supported file formats
24 4.1 Supported compression methods
25 4.2 List of preprocessed file types
26 4.3 Conversion of files with alternate character encoding
27 5. Syntax highlighting
28 5.1.1 List of supported languages
29 5.1.2 Syntax highlighting alternatives
30 5.2 Colored directory listing
31 5.3 Colored listing of tar file contents
32 6. Displaying files with special characters in the file name
34 8. Other documentation about lesspipe
36 9.1 URLs to some utilities
43 To browse files under UNIX the excellent viewer less [1] can be used. By
44 setting the environment variable LESSOPEN, less can be enhanced by external
45 filters to become even more powerful. Most Linux distributions come already
46 with a "lesspipe.sh" that covers the most common situations.
48 The input filter for less described here is called "lesspipe.sh". It is able
49 to process a wide variety of file formats. It enables users to deeply inspect
50 archives and to display the contents of files in archives without having to
51 unpack them before. That means file contents can be properly interpreted even
52 if the files are compressed and contained in a hierarchy of archives (often
53 found in RPM or DEB archives containing source tarballs). The filter is easily
54 extensible for new formats.
56 The input filter which is also called "lesspipe.sh" is written in a ksh
57 compatible language (ksh, bash, zsh) as one of these is nearly always installed
58 on UNIX systems and uses comparably few resources. Otherwise an implementation
59 in perl for example would have been somewhat simpler to code. The code looks
60 less clean than it could as it was tried to make the script compatible with
61 a number of old shells and applications especially found on non Linux systems.
63 The filter does different things depending on the file format. In most cases
64 it is determined on the output of the "file" command [2], [6], that recognizes
65 lots of formats. Only in a few cases the file suffix is used to determine what
66 to display. Up to date file descriptions are included in the "file" package.
67 Maintaining a list of file formats is therefore only a matter of keeping that
73 (see also the man page lesspipe.1)
75 To activate lesspipe.sh the environment variable LESSOPEN has to be defined
78 LESSOPEN="|lesspipe.sh %s"; export LESSOPEN (sh like shells)
79 setenv LESSOPEN "|lesspipe.sh %s" (csh, tcsh)
81 If lesspipe.sh is not in the UNIX search path or if the wrong lesspipe.sh is
82 found in the search path, then the full path to lesspipe.sh should be given
83 in the above commands.
85 As lesspipe.sh is accepting only a single argument, a hierarchical list of file
86 names has to be separated by a non blank character. A colon is rarely found
87 in file names, therefore it has been chosen as the separator character. If a
88 file name does however contain at least one isolated colon, the equal sign =
89 can be used as an alternate separator character. In that case the = character
90 has to reoccur as the last character of the argument. At each stage in
91 extracting files from such a hierarchy the file type is determined. This
92 guarantees a correct processing and display at each stage of the filtering.
94 To view files in multifile archives the following command can be used:
95 less archive_file:contained_file or
96 less archive_file=contained_file= (with = as separator)
97 This can be used to extract single files from a multifile archive:
98 less archive_file:contained_file > extracted_file
99 For extracting files less is not required, that can be done also using:
100 lesspipe.sh archive_file:contained_file > extracted_file
101 Even a file in a multifile archive that itself is contained in yet
102 another archive can be viewed this way:
103 less super_archive:archive_file:contained_file
105 The script is able to extract files up to a depth of 6 where applying a
106 decompression algorithm counts as a separate level. In a few rare cases the
107 file command does not recognize the correct format (especially with nroff).
108 In such cases the filtering can be suppressed by a trailing colon on the file
111 Display the last file in the file1:..:fileN chain in raw format:
112 Suppress input filtering: less file1:..:fileN: (append 1 colon)
113 Suppress decompression: less file1:..:fileN:: (append 2 colons)
115 Suppress syntax highlighting: less file1:..:fileN: (append 1 colon)
116 Syntax highlighting (see below) is only tried if less is called with -r or -R
117 and highlighting support was requested when generating lesspipe.sh !!!
119 Several environment variables can influence the behavior of lesspipe.sh.
121 LESSQUIET will suppress additional output not belonging to the file contents
122 if set to a non empty value.
124 LESS can be used to switch on colored less output (has to contain -r or -R).
126 LESSCOLORIZER can contain a syntax highlighting program different from
127 the code2color used by default (currently only pygmentize allowed).
129 Code for using LESS_ADVANCED_PREPROCESSOR is optionally generated (configure).
130 LESS_ADVANCED_PREPROCESSOR will switch on filtering methods for html, rtf, ps
131 files and files with alternate character encoding, if this variable is set.
132 Filtering these formats is also done if there is no LESS_ADVANCED_PREPROCESSOR
133 support (then this string is not contained in lesspipe.sh). Otherwise these
134 types of files will be shown unmodified.
139 bash or zsh or ksh (also pdksh, tested with version 5.2). Configure puts an
140 appropriate first line in the script
141 file (a version with an up to date magic file) (GNU file 4.xx recommended)
142 perl (for configure, code2color and tarcolor, lesspipe.sh works without it)
143 Standard UNIX programs like ar, cat, cut, dd, egrep, gzip, ln, ls, mkdir,
144 rm, sed, strings, tar, tput and further programs for special formats.
146 4. Supported file formats
147 =========================
149 Currently lesspipe.sh [3],[4] supports the following compression methods
150 and file types (i.e. the file contents gets transformed by lesspipe.sh):
152 4.1 Supported compression methods
153 ---------------------------------
154 gzip, compress, pack requires gzip
157 rar requires rar or unrar
160 lzma requires lzma (limited support only)
163 4.2 List of preprocessed file types
164 -----------------------------------
165 tar requires GNU tar and optionally tarcolor for coloring
166 nroff(mandoc) requires groff
167 ar library requires ar
168 jar archive requires fastjar or unzip
169 rar archive requires unrar or rar
170 7-zip archive requires 7za
171 lzip archive requires lzip
172 shared library requires nm
173 executable requires strings
174 directory displayed using ls -lA
175 RPM requires GNU cpio and rpm2cpio or rpmunpack, optionally rpm
176 Microsoft Word requires antiword or catdoc
177 MS Powerpoint requires ppthtml
178 MS Excel requires xlhtml
179 Debian requires ar, gzip and tar, shows more info if dpkg is installed
180 html requires html2text or elinks or links or lynx or w3m
181 pdf requires pdftotext or pdftohtml
182 perl requires pod2text
185 djvu requires djvutxt
186 ps requires pstotext or ps2ascii (from the gs package)
187 mp3 requires id3v2 or mp3info or mp3info2
188 jpg, png, gif requires identify
189 iso images requires isoinfo
190 MacOSX archive requires lsbom
191 MacOS X bom requires lsbom
192 MacOS X plist requires plutil
193 cab requires cabextract (version 1.0 or above)
194 gpg encrypted requires gpg
195 perl storable requires perl (and the perl modules Storable and Data::Dumper)
196 perl pod requires perldoc
197 OASIS Opendocument text documents (used for Openoffice, Libreoffice)
198 requires unzip and o3tohtml or sxw2txt (distributed together
201 4.3 Conversion of files with alternate character encoding
202 ---------------------------------------------------------
203 If the file utility reports text containing ISO-8859, UTF-8 or UTF-16 encoded
204 characters then the text will be transformed using iconv into the default
205 encoding. This does assume iconv has the right default which can be wrong
206 in some situations. It is checked if iconv would fail. Then the text is
207 displayed unmodified.
209 4.4 File formats currently not supported
210 ----------------------------------------
211 (code contributed but commented out)
213 jpeg and pbm graphics files to be displayed in ASCII art. The ASCII art
214 library works with overprinting that does not work properly within less.
215 Therefore the resulting quality of the converted picture is not satisfactory.
217 Display of video streams using mplayer with -aadriver (again ASCII art) is
218 considered abuse of less and also commented out.
220 looking at contents of DOS formatted disks by accessing the proper device file
222 5. Colorizing the output
223 ========================
225 ATTENTION: Syntax highlighting and other methods of colorizing the output
226 is only activated if the environment variable LESS is existing and contains
227 the option -R or -r or less is called with one of these options.
229 This guarantees, that instead of literal escape sequences colors are
230 displayed. The detection of the -r/-R presence at run time is rather
231 dependent on the operating system and may not work in all cases.
232 Putting the option in the LESS environment variable is guaranteed to
233 work. By installing the perl module Proc::ProcessTable the OS dependence
234 can be reduced as well.
236 The display of wrapped long lines and moving backward in a file using the
237 options -r/-R can give weird output. For an explanation see
238 http://www.greenwoodsoftware.com/less/faq.html#dashr
240 5.1 Syntax highlighting
241 -----------------------
242 Experimental support for syntax highlighting was added through a perl
243 script 'code2color' which is derived from code2html [5].
245 As syntax highlighting is rather resource intense it can be switched off by
246 appending a colon after the file name if the output was colorful. If the
247 wrong language was chosen for syntax highlighting then another one can be
248 forced by appending a colon and a suffix to the file name as follows (assuming
249 this is a file with perl syntax):
253 That works as well to force the call of code2color for a given language.
255 5.1.1 List of supported languages (code2color)
256 ----------------------------------------------
257 Text files for the following languages can be highlighted using code2color:
258 ada, asm, awk, c, c++, groff, html, xml, java, javascript, lisp, m4,
259 make, pascal, patch, perl, povray, python, ruby, shellscript, sql
260 The corresponding suffixes recognized by code2color are:
261 .ada .asm .inc .awk .c .h .cpp .cxx .groff .html .php .xml .java .js .lsp .m4
262 Makefile .pas .patch .diff .pm .pl .pod .pov .py .rb .sh .sql
264 5.1.2 Syntax highlighting alternatives
265 --------------------------------------
266 The enabling of syntax highlighting contains OS dependent code and is not
267 guaranteed to work (it was tested on Linux, Solaris, IRIX, HPUX, AIX,
268 MacOS X, Cygwin and FreeBSD). It is deactivated by default and not
269 recommended by me. It can be activated using "configure" or "make MODE=ask".
271 The function code2color contains code to guarantee that color codes are only
272 sent if less is called with one of the options -r or -R. To ensure that these
273 checks are always performed, alternate syntax colorizers will be called
274 from within code2color by setting the environment variable LESSCOLORIZER
275 to the name of another program. Currently only pygmentize (and code2color as
276 the default) is allowed. This can be changed in the first lines of code2color.
278 Much better syntax highlighting is obtained using the less emulation of vim:
279 The editor vim comes with a file less.sh, in my case located in
280 /usr/share/vim/vim73/macros. Assuming that file location
281 a function lessc (bash, zsh, ksh users)
283 lessc () { /usr/share/vim/vim73/macros/less.sh "$@"}
285 or an alias lessc (csh, tcsh users)
287 alias lessc /usr/share/vim/vim73/macros/less.sh
289 is defined and "lessc filename" is used to view the colorful file contents.
291 5.2 Colored Directory listing
292 -----------------------------
293 The conditions to display a colored listing are described above. Depending
294 on the operating system ls is then called with appropriate options to
295 produce colored output.
297 5.3 Colored listing of tar file contents
298 ----------------------------------------
299 As above less has to be called with -r or -R. If also the executable tarcolor
300 (contained in the lesspipe tar file, see also [7]) is installed, then the
301 listing of tar file contents is colored in a similar fashion as directory
304 6. Displaying files with special characters in the file name
305 ============================================================
307 Shell meta characters in file names: space (frequently used in windows
308 file names), the characters | & ; ( ) ` < > " ' # ~ = $ * ? [ ] or \
309 must be escaped by a \ when used in the shell, e.g. less a\ b.tar.gz:a\"b
310 will display the file a"b contained in the gzipped tar archive a b.tar.gz.
312 Files within an archive that do have an isolated colon in the name cannot
313 be displayed using the
314 archive_name:contained_file_name
315 notation. These files can be displayed using a notation with the alternate
316 separator character = as follows:
317 archive_name=contained_file_name=
318 Please note the trailing = which is required.
323 As a typical usage case it is shown how one could display the man page
324 "file.man" found in the Fedora10 RPM source archive file-4.26-3.fc10.src.rpm
326 The less command enhanced with the lesspipe.sh filter
328 less file-4.26-3.fc10.src.rpm
330 yields the following output
332 -rw-r--r-- 1 root root 584803 Sep 15 16:29 file-4.26.tar.gz
333 -rw-r--r-- 1 root root 17124 Oct 16 13:01 file.spec
337 less file-4.26-3.fc10.src.rpm:file-4.26.tar.gz
341 -rw-rw-r-- 10080/10080 16027 2008-08-30 12:01:41 file-4.26/doc/Makefile.in
342 -rw-rw-r-- 10080/10080 16097 2008-03-07 16:00:07 file-4.26/doc/file.man
343 -rw-rw-r-- 10080/10080 16943 2008-08-30 11:50:20 file-4.26/doc/magic.man
346 The desired man page can finally be viewed with
348 less file-4.26-3.fc10.src.rpm:file-4.26.tar.gz:file-4.26/doc/file.man
350 The subcomponents of the argument to less were easily obtained by cut and
351 paste using information contained in the previous lines of output.
352 Care has been taken to display the subcomponents already in the way
353 required by lesspipe, so that in most cases double clicking will select it.
354 If the nroff sources should have been displayed instead, appending
355 another colon at the end of the argument would have done the job:
357 less file-4.26-3.fc10.src.rpm:file-4.26.tar.gz:file-4.26/doc/file.man:
359 If the man page was compressed (e.g. as file.man.gz) it would have been
360 uncompressed anyway. To also disallow uncompressing the source file.man.gz
361 a second colon would have to be appended to the argument.
363 Even extracting single files from an archive is possible, like with
365 less file-4.26-3.fc10.src.rpm:file-4.26.tar.gz:file-4.26/src/file.c > file.c
367 Files with binary contents can be extracted as well:
369 less file-4.26-3.fc10.src.rpm:file-4.26.tar.gz:: > file-4.26.tar.gz
371 Here the two colons at the end of the argument are required to suppress the
372 unzipping of the resulting file and to extract the tar file instead of
375 Another interesting example is to get the dominating colors of a picture,
376 that contains a diagram with a few colors only. The command
380 does produce a lot of information, among others
383 720: ( 0, 0,127) #00007F
384 3032: (127,127,127) grey50
385 18935: ( 0, 0,255) blue
386 21480: ( 0,255, 0) lime
387 21041: ( 0,255,255) cyan
388 8719: (255, 0, 0) red
389 14476: (255, 0,255) magenta
390 8822: (255,183, 0) #FFB700
391 13608: (255,255, 0) yellow
392 49167: (255,255,255) white
395 Other interesting examples are the inspection of Java's .jar files or Debian
396 package contents without unpacking the files and even without having java
397 installed or without working necessarily on a Debian system.
399 The contrib directory does contain a less wrapper (a bash/zsh function) that
400 can be used to display URLs using less. This allows to pass the URL contents
401 through lesspipe as if it would be a local file.
403 8. Other documentation about lesspipe
404 =====================================
405 http://ref.cern.ch/CERN/CNL/2002/001/unix-less/
406 http://www.linux-magazine.com/issue/21/lesspipe.pdf
407 in bash cookbook (Ch. 8.15) by Carl Albing, Cameron Newham, J. P. Vossen
408 http://carloscosta.org/2008/07/05/how-to-get-more-from-less/
409 Documentation in german:
410 german.txt (distributed with lesspipe, not updated)
411 http://www.linux-magazin.de/Heft-Abo/Ausgaben/2001/01/Bessere-Sicht
412 http://www.linux-user.de/ausgabe/2002/04/060-ootb/lesspipe-1.html
417 (last checked: Jan 10 2013):
419 9.1 URLs to some utilities
420 --------------------------
421 antiword http://www.winfield.demon.nl/
422 html2text http://www.mbayer.de/html2text/
423 cabextract http://www.cabextract.org.uk/
424 7za https://sourceforge.net/projects/p7zip/
425 lzip http://download.savannah.gnu.org/releases/lzip/
426 dvi2tty http://www.ctan.org/tex-archive/dviware/dvi2tty/
427 unrtf http://ftp.gnu.org/gnu/unrtf/
428 id3v2 http://id3v2.sourceforge.net/
432 [1] http://www.greenwoodsoftware.com/less/ (less)
433 [2] ftp://ftp.astron.com/pub/file/ (file)
434 [3] http://freshmeat.net/projects/lesspipe/
435 [4] http://sourceforge.net/projects/lesspipe/
436 [5] http://www.palfrader.org/code2html/ (code2html)
437 [6] http://www.darwinsys.com/file/ (file)
438 [7] https://github.com/msabramo/tarcolor (tarcolor)
443 The script lesspipe.sh is constantly enhanced thanks to suggestions from
444 users. Among the additions to lesspipe.sh is the code to browse the ASCII
445 contents of Word or Openoffice files, to show characteristics of mp3 files
446 or to decode MacOS X formats.
448 Thanks to (in alphabetical order):
449 Marc Abramowitz: allow for color output when ls or tar commands are used
450 James Ahlborn: do not interpret .xml files as html
451 Sören Andersen: PPD files colorization requested
452 Andrew Barnert: shell syntax fix
453 Peter D. Barnes, Jr.: plist files for Mac OS X
454 Eduard Bloch: proposed support for ISO images
455 Mathieu Bouillaguet: add support for xz compression
456 Florian Cramer: MS Word, Openoffice support (o3read), ASCIIart, DjVu support
457 Philippe Defert: unattended installation
458 Antonio Diaz Diaz: proposed support for lzip
459 Bastian Fuchs: Issues using bash vs. sh
460 Carl Greco: enhanced output for .deb files
461 Stephan Hegel: suggested better 7za support
462 Michel Hermier: support for DESTDIR in the Makefile
463 Tobias Hoffmann: bug fix introduced in v 1.72
464 Christian Höltje: suggested to use LESSCOLORIZER like in the Gentoo distro
465 Jürgen Kahnert: display debian files without dpkg
466 Sebastian Kayser: suggested a less wrapper to display URLs
467 Ben Kibbey: works on FreeBSD
468 Peter Kostka: mktemp on MacOS fixes
469 Heinrich Kuettler: formatting, html via lynx
470 Vincent Lefèvre: runtime checks for shell, many enhancements, provided sxw2txt,
471 determine options for 'file' at runtime, display text in the proper encoding
472 David Leverton: detect helper programs at runtime
473 Jay Levitt: suggested to use enscript for highlighting support (see 4.2 above)
474 Vladimir Linek: inspired me to add ps and dvi support
475 Istvan Marko: speedup of the procedure
476 Markus Meyer: improved mp3 handling
477 Remi Mommsen: Mac OS X support
478 Derek B. Noonburg: PDF files support
479 Martin Otte: mktemp on MacOS fixes
480 Jim Pryor: many enhancements, bug fixes, restructuring of code, tar detection
481 Slaven Rezic: Cygwin support, bug fixes
482 Daniel Risacher: gpg support
483 Jens Schleusener: ksh syntax fixes
484 Ken Teague?: support more versions of file command
485 Paul Townsend: improved zip support for Solaris, bug fixes in configure
486 Petr Uzel: detect helper programs at runtime
487 Chelban Vasile: trap command not working under /bin/sh
488 Götz Waschk: suggested lzma support
489 Michael Wiedmann: Debian packages support
490 Dale Wijnand: Proposed the suppression of informal output