If you want to highlight color in your man pages similar to syntax highlighting in an editor, there are two easy ways to accomplish this. We’ll show you both!Even in our age of fast PCs, many people use removable or network drives which can be
Color highlighting
Color highlighting makes it easy to read. It can bring out the details, so you don’t miss them and miss them. Most modern editors support syntax highlighting, which uses color to identify and differentiate between different elements of a programming language. Reserved words, variables, strings, and numbers are all colored to aid visual analysis of a page or code function.
Having this functionality in Linux man pages would be extremely useful. Despite a preference for brevity, some pages are large, dense, and difficult to navigate. Anything that makes their visual navigation easier is a good thing.
We will describe two ways to achieve a colorized effect in man pages. One is to use another pager to display them, while the other requires passing a bunch of parameters to less at runtime. The best way to do this is to create a shell function.
The most pager
The plus pager is a file viewer, like more and less, with better handling of very large files. It also automatically colors man pages.
To install most on Ubuntu, use this command:
sudo apt-get install most
To install most on Fedora, type:
sudo dnf install most
To install most on Manjaro, you type:
sudo pacman -Syu most
Set most as default pager
To tell Linux to use most as the default pager, we need to export the value of the PAGER environment variable.
We type the following:
export PAGER=”most”
It only works until you close the terminal window. To make this change permanent, we need to add it to the “.bashrc” file (we’ll make it the last line of the file):
gedit .bashrc
We add the row, save our changes, then close the editor.
To make the contents of the modified “.bashrc” file active, we close and reopen the terminal window.
To keep the terminal window open, we’ll be using the source command, which can be shortened to a period (.). This will force the shell to read the contents of the modified “.bashrc” file.
We type the following:
. .bashrc
Color man pages
Let’s open a man page and see what it looks like:
man grep
The man page opens as usual, but the text is now highlighted in different colors.
Scroll down and you will see how the different elements on the page are colored.
Using most is very similar to using less, but there are some differences. Press H most to display a list of keyboard shortcuts and their functions.
Use color with less
If you don’t want to set up another pager or need to learn new keystrokes, there is a trick you can use to force the color down. There are a number of ways you can do this, but we’ll cover the quickest and easiest way.
This method uses the U.S. National Institute of Standards (ANSI) color codes to control the onscreen effects associated with the old and mostly deceased termcap settings.
These were once used to specify how computer terminals of different makes and models should interpret display commands. The software packages also had their own termcap parameters, and less did too.
Here are the definitions of the less termcap settings:
- LESS_TERMCAP_md: Launch a bold effect (double-gloss).
- LESS_TERMCAP_me: Stop the fat effect.
- LESS_TERMCAP_us: Start the underline effect.
- LESS_TERMCAP_ue: Turns off the underline effect.
- LESS_TERMCAP_so: Start a highlight effect (similar to inverted text).
- LESS_TERMCAP_se: Turns off the highlighting effect (similar to inverted text).
Again, we’ll set them to control the color combinations using the American National Standard Institute (ANSI) color codes.
The color code format is easy to read once you get the hang of it:
- The “e” at the beginning identifies the sequence as a control code or escape sequence.
- The “m” at the end of the sequence command indicates the end of the command. It also causes the control code to act.
- The numbers between “[»et«m»déterminentlescouleursàutiliserLescouleurssontidentifiéesparunnuméroCertainsnombresreprésententlescouleursd’arrière-planetd’autreslescouleursdepremierplan(texte)[”and“m”dictatewhichcolorswillbeusedThecolorsareidentifiedbynumberSomenumbersrepresentbackgroundcolorsandsomerepresentforeground(text)colors
Voici les codes que nous utiliserons pour démarrer une séquence de couleurs et comment les désactiver:
- ‘ e[01;31m[01;31m‘: Fond noir, texte rouge.
- ‘ e[01;32m[01;32m‘: Fond noir, texte vert.
- ‘ e[45;93m[45;93m‘: Fond magenta, texte jaune vif.
- » e[0min[0m‘: Désactivez tous les effets.
Nous allons envelopper tout cela dans une fonction shell que nous appellerons man. Il fixera ces valeurs pour nous, puis appellera le réel man programme.
Si vous avez déjà défini des fonctions shell dans un autre fichier, vous pouvez ajouter celle-ci à ce fichier. Sinon, copiez le texte suivant au bas de votre fichier «.bashrc»:
man() {
LESS_TERMCAP_md=
gedit .bashrc
Collez la fonction au bas de votre fichier «.bashrc».
Enregistrez vos modifications et fermez l’éditeur. Maintenant, nous devons lire le fichier «.bashrc» pour rendre la fonction shell active, donc nous tapons:
. .bashrc
Maintenant, quand nous commençons un man page, elle sera colorisée en less:
man chmod
La page de manuel s’ouvre avec une mise en évidence des couleurs.
Rétrospectivement, le jaune sur magenta n’était peut-être pas la meilleure idée. Heureusement, vous pouvez modifier les codes de couleur à votre guise.
Ce n’est pas juste joli
Il est facile de faire défiler une longue man et manquez une information importante, comme une option ou un paramètre, car elle est perdue dans une mer de texte.
Désormais, les noms des paramètres et des options seront mis en surbrillance et beaucoup plus faciles à repérer.
e[01;31m’ LESS_TERMCAP_me=
Collez la fonction au bas de votre fichier «.bashrc».
Enregistrez vos modifications et fermez l’éditeur. Maintenant, nous devons lire le fichier «.bashrc» pour rendre la fonction shell active, donc nous tapons:
Maintenant, quand nous commençons un man page, elle sera colorisée en less:
La page de manuel s’ouvre avec une mise en évidence des couleurs.
Rétrospectivement, le jaune sur magenta n’était peut-être pas la meilleure idée. Heureusement, vous pouvez modifier les codes de couleur à votre guise.
Bientôt: Comment créer des alias et des fonctions shell sous Linux
Ce n’est pas juste joli
Il est facile de faire défiler une longue man et manquez une information importante, comme une option ou un paramètre, car elle est perdue dans une mer de texte.
Désormais, les noms des paramètres et des options seront mis en surbrillance et beaucoup plus faciles à repérer.
e[0m’ LESS_TERMCAP_us=
Collez la fonction au bas de votre fichier «.bashrc».
Enregistrez vos modifications et fermez l’éditeur. Maintenant, nous devons lire le fichier «.bashrc» pour rendre la fonction shell active, donc nous tapons:
Maintenant, quand nous commençons un man page, elle sera colorisée en less:
La page de manuel s’ouvre avec une mise en évidence des couleurs.
In retrospect, yellow on magenta might not have been the best idea. Luckily, you can change the color codes to your liking.
It’s not just pretty
It’s easy to scroll through a long man and miss important information, like an option or setting, because it’s lost in a sea of text.
Now the names of settings and options will be highlighted and much easier to spot.
e[01;32m’ LESS_TERMCAP_ue=
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:
Now when we start a man page it will be colorized to less:
The man page opens with color highlighting.
In retrospect, yellow on magenta might not have been the best idea. Luckily, you can change the color codes to your liking.
Coming soon: How to create aliases and shell functions in Linux
It’s not just pretty
It’s easy to scroll through a long man and miss important information, like an option or setting, because it’s lost in a sea of text.
Now the names of settings and options will be highlighted and much easier to spot.
e[0m’ LESS_TERMCAP_so=
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:
Now when we start a man page it will be colorized to less:
The man page opens with color highlighting.
In retrospect, yellow on magenta might not have been the best idea. Luckily, you can change the color codes to your liking.
It’s not just pretty
It’s easy to scroll through a long man and miss important information, like an option or setting, because it’s lost in a sea of text.
Now the names of settings and options will be highlighted and much easier to spot.
e[45;93m’ LESS_TERMCAP_se=
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:
Now when we start a man page it will be colorized to less:
The man page opens with color highlighting.
In retrospect, yellow on magenta might not have been the best idea. Luckily, you can change the color codes to your liking.
It’s not just pretty
It’s easy to scroll through a long man and miss important information, like an option or setting, because it’s lost in a sea of text.
Now the names of settings and options will be highlighted and much easier to spot.
e[0m’ command man “[email protected]”}
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:
Now when we start a man page it will be colored in less:
The man page opens with color highlighting.
In retrospect, yellow over magenta might not have been the best idea. Fortunately, you can change the color codes as you like.
It’s not just pretty
It’s easy to scroll through a long man and miss an important piece of information, like an option or a setting, because it’s lost in a sea of text.
Now the names of settings and options will be highlighted and much easier to spot.