Wednesday, October 7, 2009

Colored man pages

I found a really interesting post about in a brazilian site named DICAS-L, and thought it would be really helpful to have it here, for me or anyone else. Man pages may be read with colored keywords, what helps understanding the (usually complicated) text within.

It should be good to first understand the idea behind pagers. They are tools that ease the reading of long texts, as man pages are.

For Unix, probably the oldest pager is more, created by Daniel Halbert, in 1978. With ENTER the text is rolled up line by line, with SPACE we can read the next page, and for search the "/" character is used, as it is in vim. Thus, more became very important for Unix operator.

After, less was created by Mark Nudelman, from 1983 to 1985, offering more resources than more. The names were meant to be a joke, obviously. less allows the use of arrow keys for "navigating" the document, and much more interesting features than it's predecessor.

And finally, John Davis, from MIT, creates most, a pager with more features than the well known less. It is the basis of our article.

MAN pages
I strongly believe that MAN pages are simultaneously the best and the worst Unix documentation source, because (generally) they are written by the software author, but at the same time, not only it does not offers (many) examples, as it mostly make wide use of technical jargon, which can be complicated to newbies. Another good point is that most of man pages are written in English, and usually not localized, or at least as it should.

As said before, man pages are huge, and a pager should be a great help. pager is the default pager, and it is a symlink to some pager, as we can see:
$ whereis pager
pager: /usr/bin/pager
$ file /usr/bin/pager
/usr/bin/pager: symbolic link to `/etc/alternatives/pager'
$ file /etc/alternatives/pager
/etc/alternatives/pager: symbolic link to `/usr/bin/less'
We will replace less by most. What we need to do is to declare it with the environment variable MANPAGER.

As usual, installing a software is really easy, specially in Debian-based distros:
sudo aptitude install most
To manualliy configure most as man pager, type the following:
$ export MANPAGER="/usr/bin/most -s"
But, by finishing your user session, this configuration vanishes. We really need to make it permanent, and we do so making smooth changes to start scripts:

If this pager must be ready only for the current user:
$ echo 'export MANPAGER="/usr/bin/most -s"' >> ~/.bashrc
If this pager must be ready for all users:
$ sudo su -
# echo 'export MANPAGER="/usr/bin/most -s"' >> /etc/profile
# exit
From now on, man pages are all colored. Some tips about most
  • b - bottom of the text
  • t - top of the text
  • / - search for
  • q - quit the text

Of course, we should always try to edit the files above (~/.bashrc or /etc/profile) and add those lines manually, with correct comments, and on. Enjoy it !

Don't forget to let down here what you thought about this article: useful, interesting or unuseful for you ?
Comments are welcome.