Jun 242014
 

A not so common guide, article by   first posted on http://www.refining-linux.org

The UNIX philosophy is based on the DRY principle which declares: “Don’t repeat yourself”. Therefore, a program just does what it’s made for and uses libraries or even other programs on your system to do a more advanced job. Some of these auxiliary programs are user definable and probably you already know the EDITOR environment variable, which specifies your default editor for the console. This is used, e.g., for the command visudo, which opens your /etc/sudoers file safely, or by svn to input a commit comment. But another important component is your console pager, which is used to display textual content on the console.

As a system administrator or a more advanced Linux user you surely know man and how to use it. man shows you the (probably localized) contents of your system’s manual pages for a specific command or file, at least you might think so, but actually that’s wrong or let’s say: not completely the truth. man selects which manpage to show, but the display itself is managed by your pager. Of course, not only man makes use of the pager, also commands like mail use it (which might not exist on your system if you don’t have an MTA installed), so this is a very basic program.

I assume, you have already used your pager very often, which is nothing else than more, less or lv (which is also a good pager but very seldom installed by default and therefore rarely known). Most of the time, you use them to display the contents of log files or configurations, which you don’t want to alter, but that’s not all. As mentioned before, other programs use your pager as well and there is a way to configure which one to take and of course also how to use it.



Setting a pager

For this blog entry let’s stay with man, which is probably the most important program or at least the most often used program that uses a pager. By default, man uses /usr/bin/less -is as display engine, which starts less in in-case-sensitive mode and collapses sequent empty lines to one single line. On most distributions, the configuration for your manpage viewer is found at /etc/man.conf but we will leave this file untouched as it’s only specific for the man program. There is another more common way to set your pager all over your system and that’s the PAGER environment variable. In this variable you set which command to use as your pager. To use more, you would fill this variable as follows:

export PAGER="more"

If you now start man, you should see more as your manpage viewer. You could also use more or cat as your pager, that’s completely up to you. But how to pass parameters to this instance of more? I mentioned earlier in this text that PAGER contains a command, so you could do something like this:

export PAGER="/usr/bin/less -isM

but there is actually a better way. All three pager programs, more, less and lv, support passing parameters via separate environment variables. These variables are named LESS, MORE and LV, respectively. These variables can contain all the parameters passed to the specific program by default, not affecting your setting for PAGER. These variables will also be evaluated, when you execute your pager program independently (stand-alone). So to assign the parameters -isM to less, type in

export LESS="-isM"

Then start less (either stand-alone or with man or mail) and you should see the effect.

Note: specifically for man, you can also define the variable MANPAGER, which overrides PAGER.

Making the settings persistent

As yet you’ll loose your settings as soon as you close your current terminal. Thus, you should save your settings. You can do this by writing your variable export commands into your own .profile file for your native tty login shell or into .bashrc for non-login shells (like your Xterm). To make your settings available system wide, create a proper file in /etc/env.d/, /etc/profile.d/ or directly edit /etc/profile, depending on your distribution. To see an effect for your current X session, you have indeed to re-login.

Troubleshooting

There might be some issues when using a custom manpager. One big problem that could occur when using less or lv as a pager, is that your manual pages are displayed as garbage. That could look something like this:

MOUNT(8)                                          Linux Programmer's Manual                                          MOUNT(8)



ESC[1mNAMEESC[0m
       mount - mount a filesystem

ESC[1mSYNOPSISESC[0m
       ESC[1mmount ESC[22m[ESC[1m-lhVESC[22m]

       ESC[1mmount -a ESC[22m[ESC[1m-fFnrsvwESC[22m] [ESC[1m-t ESC[4mESC[22mvfstypeESC[24m] [ESC[1m-O ESC[4mESC[22moptlistESC[24m]

       ESC[1mmount ESC[22m[ESC[1m-fnrsvwESC[22m] [ESC[1m-o ESC[4mESC[22moptionESC[24m[ESC[1m,ESC[4mESC[22moptionESC[24m]...]  ESC[4mdeviceESC[24m|ESC[4mdirESC[0m

       ESC[1mmount ESC[22m[ESC[1m-fnrsvwESC[22m] [ESC[1m-t ESC[4mESC[22mvfstypeESC[24mESC[1m] ESC[22m[ESC[1m-o ESC[4mESC[22moptionsESC[24m] ESC[4mdeviceESC[24m ESC[4mdirESC[0m

ESC[1mDESCRIPTIONESC[0m
       All  files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted at ESC[1m/ESC[22m.  These files can
       be spread out over several devices. The ESC[1mmount ESC[22mcommand serves to attach the filesystem found on some device to the  big
       file tree. Conversely, the ESC[1mumountESC[22m(8) command will detach it again.

       The standard form of the ESC[1mmount ESC[22mcommand, is

              ESC[1mmount -t ESC[4mESC[22mtypeESC[24m ESC[4mdeviceESC[24m ESC[4mdirESC[0m

       This tells the kernel to attach the filesystem found on ESC[4mdeviceESC[24m (which is of type ESC[4mtypeESC[24m) at the directory ESC[4mdirESC[24m.  The pre‐
       vious contents (if any) and owner and mode of ESC[4mdirESC[24m become invisible, and as long as this  filesystem  remains  mounted,
       the pathname ESC[4mdirESC[24m refers to the root of the filesystem on ESC[4mdeviceESC[24m.

       ESC[1mThe listing and help.ESC[0m
              Three forms of invocation do not actually mount anything:

              ESC[1mmount -hESC[0m
                     prints a help message

              ESC[1mmount -VESC[0m
                     prints a version string

              ESC[1mmount ESC[22m[ESC[1m-lESC[22m] [ESC[1m-t ESC[4mESC[22mtypeESC[24m]
                     lists  all  mounted  filesystems  (of  type  ESC[4mtypeESC[24m).  The option -l adds the labels in this listing.  See
                     below.

for less or

MOUNT(8)                                                                          Linux Programmer's Manual                                                                          MOUNT(8)



^[[1mNAME^[[0m
       mount - mount a filesystem

^[[1mSYNOPSIS^[[0m
       ^[[1mmount ^[[22m[^[[1m-lhV^[[22m]

       ^[[1mmount -a ^[[22m[^[[1m-fFnrsvw^[[22m] [^[[1m-t ^[[4m^[[22mvfstype^[[24m] [^[[1m-O ^[[4m^[[22moptlist^[[24m]

       ^[[1mmount ^[[22m[^[[1m-fnrsvw^[[22m] [^[[1m-o ^[[4m^[[22moption^[[24m[^[[1m,^[[4m^[[22moption^[[24m]...]  ^[[4mdevice^[[24m|^[[4mdir^[[0m

       ^[[1mmount ^[[22m[^[[1m-fnrsvw^[[22m] [^[[1m-t ^[[4m^[[22mvfstype^[[24m^[[1m] ^[[22m[^[[1m-o ^[[4m^[[22moptions^[[24m] ^[[4mdevice^[[24m ^[[4mdir^[[0m

^[[1mDESCRIPTION^[[0m
       All  files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted at ^[[1m/^[[22m.  These files can be spread out over several devices. The ^[[1mmount ^[[22mcommand serves t
o
       attach the filesystem found on some device to the big file tree. Conversely, the ^[[1mumount^[[22m(8) command will detach it again.

       The standard form of the ^[[1mmount ^[[22mcommand, is

              ^[[1mmount -t ^[[4m^[[22mtype^[[24m ^[[4mdevice^[[24m ^[[4mdir^[[0m

       This tells the kernel to attach the filesystem found on ^[[4mdevice^[[24m (which is of type ^[[4mtype^[[24m) at the directory ^[[4mdir^[[24m.  The previous contents (if any) and owner and mode of ^[[4mdi
r^[[24m become  invisi‐
       ble, and as long as this filesystem remains mounted, the pathname ^[[4mdir^[[24m refers to the root of the filesystem on ^[[4mdevice^[[24m.

       ^[[1mThe listing and help.^[[0m
              Three forms of invocation do not actually mount anything:

              ^[[1mmount -h^[[0m
                     prints a help message

              ^[[1mmount -V^[[0m
                     prints a version string

              ^[[1mmount ^[[22m[^[[1m-l^[[22m] [^[[1m-t ^[[4m^[[22mtype^[[24m]
                     lists all mounted filesystems (of type ^[[4mtype^[[24m).  The option -l adds the labels in this listing.  See below.

for lv. Why does this happen? Actually, that’s no great mystery, it’s just that your pager does not display the escape sequences used for formatting properly. To get rid of this odd looking character soup, you have to pass the parameter -R to less or -c to lv, respectively.

# For less (additionally passed parameters -i, -s, -M and --shift 5)
export LESS="-isMR --shift 5"

# For lv
export LV="-c"

You’ll notice that you won’t have this problem with more and cat because these two programs don’t buffer your text for the sake of scrolling up and down, so the contents are directly passed to the console. The price you have to pay for this advantage is the ability of scrolling bidirectional.

Special case: sudo

You might also encounter that your man pages work pretty well but display the garbage above when switching to a sudo environment – even if you’ve explicitly set proper parameters. That’s because sudo does not inherit all your environment variables. To make your pager settings also available in sudo environment, edit your /etc/sudoers file. Type in

sudo visudo

and add PAGER and LESS/LV to the env_keep directive, so this line looks like this:

Defaults env_keep="DISPLAY XAUTHORITY EDITOR LESS LV PAGER"

This will preserve all the specified variables.


Popular Posts:

Flattr this!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*