قالب وردپرس درنا توس
Home / Tips and Tricks / How to display man pages in color on Linux

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 marking in your man pages that are similar to the syntax highlight in an editor, there are two simple ways you can achieve this. We show you both!

paint mark

Color marking makes things easier to read. It can cause details to spread, so you do not skim past and miss them. Most modern editors support syntax lighting, which uses color to identify and distinguish between different elements of a programming language. Reserved words, variables, strings, and numbers are all colored to make it easier to visually analyze a page or function of the code.

Has this feature in Linux man pages would be very helpful. Although they favored brevity, some man the sides are large, dense and difficult to get through. Anything that makes it easier to navigate them is fine.

We will describe two ways you can get a colored effect man pages. One involves using another pager to display them, while the other requires sending a couple of parameters to less while driving. The best way to do this is to create a scale function.

The most pager is a file viewer more and less, with improved handling of very wide files. It also colors automatically man pages.

To install most on Ubuntu, use this command:

sudo apt-get install most

sudo apt-get install most in a terminal window.

To install most on Fedora, write:

sudo dnf install most

sudo dnf installs most in a terminal window.

To install most on Manjaro you write:

sudo pacman -Syu most

sudo pacman -Syu mostly in a terminal window.

To say Linux to use most As a standard finder, we need to export the value of PAGER environmental factor.

We write the following:

export PAGER=“most”

export PAGER =

However, this only works until you close the terminal window. To make this change permanent, we need to add it to the “.bashrc” file (we make it the last line of the file):

gedit .bashrc

gedit .bashrc in a terminal window.

We add the line, save our changes, and then close the editor.

.bashrc in a gedit editor.

To make the contents of the modified “.bashrc” file active, we close and open the terminal window again.

To keep the terminal window open, we use source command, which can be shortened to a period (.). This will cause the shell to read the contents of the modified “.bashrc” file.

We write the following:

. .bashrc

.  .bashrc in a terminal window.

Color man pages

Let’s open one man page and see what it looks like:

man grep

they grabbed a terminal window.

The man the page opens as usual, but it now has text highlighted in different colors.

male page with color marking.

Scroll down to see how the different elements on the page color.

A section of a man page with color marking in a terminal window.

Using most very similar to use less, but there are some differences. Press H in most to see a list of key chains and their functions.

The most pager help screen in a terminal window.

Use colors with less

If you do not want to install another pager or need to learn new keystrokes, there is a trick you can use to force less to use color. There are various ways you can do this, but we cover the fastest and easiest method.

This method uses the American National Standards Institute (ANSI) color codes to control the effects on the screen associated with the old and mostly disabled termcap settings.

These were used once to specify how computer terminals of different makes and models should interpret display commands. Software packages also had their own termcap settings and less do it too.

Here are the definitions of less termcap settings:

  • LESS_TERMCAP_md: Start bold effect (double-light).
  • LESS_TERMCAP_me: Stop bold effect.
  • LESS_TERMCAP_us: Start the underlining effect.
  • LESS_TERMCAP_ue: Stop underlining effect.
  • LESS_TERMCAP_so: Start protruding effect (similar to reverse text).
  • LESS_TERMCAP_se: Stop outstanding effect (similar to reverse text).

Again, we will set these to check color combinations using the American National Standard Institute (ANSI) color codes.

The format of the color code is easy to read once you understand it:

  • ” E” initially identifies the sequence as a control code or flight sequence.
  • “M” at the end of the sequence command indicates the end of the command. It also causes the control code to be fixed.
  • The numbers between “[“and”m”indicatethecolorstobeusedThecolorsareidentifiedbynumbersCertainnumbersrepresentbackgroundcolorsandothersrepresentforegroundcolors(text)[“och”m”angervilkafärgersomskaanvändasFärgernaidentifierasmednummerVissanummerrepresenterarbakgrundsfärgerochandrarepresenterarförgrundsfärger(text)[”and“m”dictatewhichcolorswillbeusedThecolorsareidentifiedbynumberSomenumbersrepresentbackgroundcolorsandsomerepresentforeground(text)colors

Here are the codes we will use to start a color sequence and how to turn them all off:

  • ‘ E(01; 31 m[01;31m[01;31m: Black background, red text.
  • ‘ E(01; 32 m[01;32m[01;32m: Black background, green text.
  • ‘ E(45; 93 m[45;93m[45;93m: Magenta background, light yellow text.
  • ” E[0m[0m[0m[0m‘: Turn off all effects.

We lose all this in a scale function we will call man. It will set these values ​​for us and then call it real man program.

If you have already defined some scale functions in another file, you can add it to that file. Otherwise, copy the following text at 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

gedit .bashrc in a terminal window.

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

manshell function in the gedit editor.

Save your changes and close the editor. Now we need to read the “.bashrc” file to enable the scale function, so we write:

. .bashrc

.  .bashrc in a terminal window.

Now that we’re starting one man page, it will be colored less:

man chmod

Run

The man page opens with color marking.

A colored man side on smaller in a terminal window.

In retrospect, yellow on magenta may not have been the best idea. Thankfully, you can fine-tune the color codes to your liking.

RELATED: How to create aliases and scale functions on Linux

It’s not just beautiful

It is easy to scroll through for a long time man page and miss important information, as an option or parameter, because it gets lost in a sea of ​​text.

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




Source link