How to Display man Pages in Color on Linux


A command prompt in a terminal window on a Linux computer.
Fatmawati Achmad Zaenuri/Shutterstock

If you want color highlighting in your guy webpages related to the syntax highlighting in an editor, there are two uncomplicated approaches you can attain it. We’ll display you the two!

Colour Highlighting

Color highlighting makes things less difficult to go through. It can make information pop, so you really don’t skim previous and skip them. Most contemporary editors support syntax highlighting, which utilizes color to identify and differentiate involving diverse elements of a programming language. Reserved text, variables, strings, and quantities are all colorized to make it easier to visually parse a web page or operate of code.

Possessing this element in the Linux gentleman pages would be extremely helpful. Despite favoring brevity, some man webpages are big, dense, and difficult to get by way of. Anything at all that can make it a lot easier to visually navigate them is a superior issue.

We’re likely to explain two means you can get a colorized influence in man pages. One particular consists of employing a unique pager to display screen them, even though the other demands passing a bunch of parameters to much less at operate time. The neatest way to do that is to produce a shell perform.

The most Pager

The most pager is a file viewer, like a lot more and fewer, with enhanced dealing with of quite huge data files. It also mechanically colorizes person internet pages.

To put in most on Ubuntu, use this command:

sudo apt-get set up most

To put in most on Fedora, kind:

sudo dnf install most

To set up most on Manjaro, you variety:

sudo pacman -Syu most

Established most as the Default Pager

To notify Linux to use most as the default pager, we have to export the benefit of the PAGER ecosystem variable.

We style the pursuing:

export PAGER=“most”

This only operates right until you close the terminal window, nevertheless. To make this modify long term, we have to insert it to the “.bashrc” file (we’ll make it the very last line in the file):

gedit .bashrc

We increase the line, help you save our improvements, and then close the editor.

To make the contents of the modified “.bashrc” file active, we near and reopen the terminal window.

To keep the terminal window open up, we’ll use the supply command, which can be shortened to a time period (.). This will make the shell examine the contents of the modified “.bashrc” file.

We variety the following:

. .bashrc

Color gentleman Webpages

Let’s open a person web site and see what it looks like:

person grep

The man page opens as normal, but it now has text highlighted in distinct shades.

Scroll down, and you’ll see how the distinct factors of the web site are colorized.

Using most is extremely comparable to using a lot less, but there are some variations. Push H in  most to see a list of keybindings and their functions.

Applying Color with much less

If you really don’t want to put in a different pager or have to find out new keystrokes, there’s a trick you can use to pressure significantly less to use shade. There are distinctive approaches you can do this, but we’ll cover the fastest and most straightforward approach.

This strategy makes use of the American Countrywide Specifications Institute (ANSI) coloration codes to command the onscreen consequences connected with the outdated and typically defunct termcap configurations.

These were being after employed to specify how computer terminals of distinctive would make and models need to interpret display commands. Software program offers also had their own termcap settings, and significantly less does, way too.

Listed here are the definitions of the less termcap options:

  • Less_TERMCAP_md: Begin bold outcome (double-vibrant).
  • Much less_TERMCAP_me: End daring impact.
  • A lot less_TERMCAP_us: Begin underline outcome.
  • Fewer_TERMCAP_ue: Prevent underline impact.
  • Fewer_TERMCAP_so: Begin stand-out influence (identical to reverse text).
  • Fewer_TERMCAP_se: End stand-out influence (equivalent to reverse textual content).

Yet again, we’ll set these to command color mixtures employing the American National Conventional Institute (ANSI) color codes.

The structure of the coloration code is simple to study once you have an understanding of it:

  • The “e” at the starting identifies the sequence as a regulate code or escape sequence.
  • The “m” at the end of the sequence command indicates the finish of the command. It also causes the handle code to be actioned.
  • The figures in between the “[” and “m” dictate which colors will be used. The colors are identified by number. Some numbers represent background colors and some represent foreground (text) colors.

These are the codes we’ll use to start a color sequence, and how to turn them all off:

  • ‘e[01;31m: Black background, red text.
  • ‘e[01;32m: Black background, green text.
  • ‘e[45;93m: Magenta background, bright yellow text.
  • ’‘e[0m’: Turn off all effects.

We’re going to wrap all of this in a shell function we’ll call man. It will set these values for us, and then call the real man program.

If you’ve already got some shell functions defined in another file, you can add this one to that file. Otherwise, copy the following text into the bottom of your “.bashrc” file:

man() 
    LESS_TERMCAP_md=$'e[01;31m' 
    LESS_TERMCAP_me=$'e[0m' 
    LESS_TERMCAP_us=$'e[01;32m' 
    LESS_TERMCAP_ue=$'e[0m' 
    LESS_TERMCAP_so=$'e[45;93m' 
    LESS_TERMCAP_se=$'e[0m' 

    command man "$@"
gedit .bashrc

Paste the function at the bottom of your “.bashrc” file.

Save your changes and close the editor. Now, we need to read the “.bashrc” file to make the shell function active, so we type:

. .bashrc

Now, when we start a man page, it will be colorized in less:

man chmod

The man page opens with color highlighting.

In retrospect, yellow on magenta might not have been the best idea. Thankfully, you can tweak the color codes to your liking.

RELATED: How to Create Aliases and Shell Functions on Linux

It’s Not Just Pretty

It’s easy to scroll through a long man page and miss an important piece of information, like an option or parameter, because it’s lost in a sea of text.

Now, parameter and option names will be highlighted and much easier for you to spot.

Exit mobile version