linux命令行彩色显示_如何在Linux上以彩色显示手册页

linux命令行彩色显示_如何在Linux上以彩色显示手册页_第1张图片

linux命令行彩色显示

linux命令行彩色显示_如何在Linux上以彩色显示手册页_第2张图片 Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

If you want color highlighting in your man pages similar to the syntax highlighting in an editor, there are two simple ways you can achieve it. We’ll show you both!

如果要使man页中的颜色突出显示类似于编辑器中的语法突出显示,可以通过两种简单的方法来实现。 我们将向您展示!

颜色突出显示 (Color Highlighting )

Color highlighting makes things easier to read. It can make details pop, so you don’t skim past 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 colorized to make it easier to visually parse a page or function of code.

颜色突出显示使内容更易于阅读。 它可以使细节变得流行,因此您不会掠过而错过它们。 大多数现代编辑器都支持语法突出显示,该语法突出显示使用颜色来识别和区分编程语言的不同元素。 保留字,变量,字符串和数字都用彩色着色,以便于直观地解析页面或代码功能。

Having this feature in the Linux man pages would be extremely helpful. Despite favoring brevity, some man pages are big, dense, and difficult to get through. Anything that makes it easier to visually navigate them is a good thing.

在Linux man页中具有此功能将非常有帮助。 尽管简明扼要,但某些man页很大,很密集,很难通过。 使视觉上更容易浏览的任何事物都是一件好事。

We’re going to describe two ways you can get a colorized effect in man pages. One involves using a different pager to display them, while the other requires passing a bunch of parameters to less at run time. The neatest way to do that is to create a shell function.

我们将描述两种在man页中获得彩色效果的方法。 一种涉及使用不同的寻呼机来显示它们,而另一种则需要在运行时将一堆参数传递给less的参数。 最简单的方法是创建一个shell函数。

最传呼机 (The most Pager )

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

最多的传呼器是文件查看器,它more像处理more宽的文件一样, more less使用。 它还会自动为man页上色。

To install most on Ubuntu, use this command:

要在Ubuntu上most安装,请使用以下命令:

sudo apt-get install most
sudo apt-get install most in a terminal window.

To install most on Fedora, type:

要在Fedora上most安装,请键入:

sudo dnf install most
sudo dnf install most in a terminal window.

To install most on Manjaro, you type:

要在Manjaro上most安装,请键入:

sudo pacman -Syu most
sudo pacman -Syu most in a terminal window.

设置为默认寻呼机 (Set most as the Default Pager )

To tell Linux to use most as the default pager, we have to export the value of the PAGER environment variable.

为了告诉Linux most使用默认的寻呼机,我们必须导出PAGER环境变量的值。

We type the following:

我们输入以下内容:

export PAGER=“most”
export PAGER=“most” in a terminal window.

This only works until you close the terminal window, though. To make this change permanent, we have to add it to the “.bashrc” file (we’ll make it the last line in the file):

但是,这仅在关闭终端窗口之前有效。 为了使此更改永久生效,我们必须将其添加到“ .bashrc”文件中(将其设置为文件的最后一行):

gedit .bashrc
gedit .bashrc in a terminal window.

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

我们添加该行,保存更改,然后关闭编辑器。

linux命令行彩色显示_如何在Linux上以彩色显示手册页_第3张图片

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

为了激活修改后的“ .bashrc”文件的内容,我们关闭并重新打开终端窗口。

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

为了使终端窗口保持打开状态,我们将使用source命令,该命令可以缩短为一个句点( . )。 这将使外壳读取修改后的“ .bashrc”文件的内容。

We type the following:

我们输入以下内容:

. .bashrc
. .bashrc in a terminal window.

彩色手册页 (Color man Pages)

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

让我们打开一个man页,看看它是什么样的:

man grep
man grep in a terminal window.

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

man页照常打开,但是现在它以不同的颜色突出显示了文本。

linux命令行彩色显示_如何在Linux上以彩色显示手册页_第4张图片

Scroll down, and you’ll see how the different elements of the page are colorized.

向下滚动,您将看到页面不同元素的颜色。

linux命令行彩色显示_如何在Linux上以彩色显示手册页_第5张图片

Using most is very similar to using less, but there are some differences. Press H in  most to see a list of keybindings and their functions.

使用most与使用less非常相似,但是有一些区别。 most按H键可查看按键绑定及其功能的列表。

linux命令行彩色显示_如何在Linux上以彩色显示手册页_第6张图片

更少使用色彩 (Using Color with less )

If you don’t want to install another pager or have to learn new keystrokes, there’s a trick you can use to force less to use color. There are different ways you can do this, but we’ll cover the quickest and easiest method.

如果您不想安装其他寻呼机或必须学习新的按键,可以使用一种技巧来less使用颜色。 您可以通过多种方法来执行此操作,但是我们将介绍最快,最简单的方法。

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

此方法使用美国国家标准协会 (ANSI)的颜色代码来控制与旧的且大多已失效的 termcap设置相关的屏幕效果。

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

这些曾经用来指定不同品牌和型号的计算机终端应如何解释显示命令。 软件包还具有自己的termcap设置,而且设置也less

Here are the definitions of the less termcap settings:

以下是termcap设置less的定义:

  • LESS_TERMCAP_md: Start bold effect (double-bright).

    LESS_TERMCAP_md :开始加粗效果(双亮)。

  • LESS_TERMCAP_me: Stop bold effect.

    LESS_TERMCAP_me :停止粗体效果。

  • LESS_TERMCAP_us: Start underline effect.

    LESS_TERMCAP_us :开始下划线效果。

  • LESS_TERMCAP_ue: Stop underline effect.

    LESS_TERMCAP_ue :停止下划线效果。

  • LESS_TERMCAP_so: Start stand-out effect (similar to reverse text).

    LESS_TERMCAP_so :开始突出效果(类似于反向文本)。

  • LESS_TERMCAP_se: Stop stand-out effect (similar to reverse text).

    LESS_TERMCAP_se :停止突出效果(类似于反向文本)。

Again, we’ll set these to control color combinations using the American National Standard Institute (ANSI) color codes.

再次,我们将使用美国国家标准协会 (ANSI) 颜色代码将其设置为控制颜色组合。

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

一旦理解,颜色代码的格式就很容易阅读:

  • The “\e” at the beginning identifies the sequence as a control code or escape sequence.

    开头的“ \ e”将序列标识为控制代码或转义序列。
  • The “m” at the end of the sequence command indicates the end of the command. It also causes the control code to be actioned.

    序列命令末尾的“ m”表示命令末尾。 这也使控制代码起作用。
  • The numbers 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.

    “ [”和“ m”之间的数字表示将使用哪种颜色。 颜色由数字标识。 一些数字表示背景色,而一些数字表示前景色(文本)。

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; 31m ' 黑色背景,红色文本。

  • ‘\e[01;32m: Black background, green text.

    '\ e [01; 32m ' 黑色背景,绿色文字。

  • ‘\e[45;93m: Magenta background, bright yellow text.

    '\ e [45; 93m ' 洋红色背景,亮黄色文本。

  • ’‘\e[0m’: Turn off all effects.

    ''\ e [0m ':关闭所有效果。

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.

我们将所有这些都包装在一个称为man的shell函数中。 它将设置这些值的我们,然后再调用真正的man程序。

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:

如果已经在另一个文件中定义了一些Shell函数,则可以将此函数添加到该文件中。 否则,将以下文本复制到“ .bashrc”文件的底部:

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.

将该函数粘贴到“ .bashrc”文件的底部。

linux命令行彩色显示_如何在Linux上以彩色显示手册页_第7张图片

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”文件以使外壳程序功能处于活动状态,因此我们键入:

. .bashrc
. .bashrc in a terminal window.

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

现在,当我们开始一个man页时,它将以less颜色显示:

man chmod
Running

The man page opens with color highlighting.

手册页随即突出显示颜色。

linux命令行彩色显示_如何在Linux上以彩色显示手册页_第8张图片

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

回想起来,洋红色上的黄色可能不是最好的主意。 值得庆幸的是,您可以根据自己的喜好调整颜色代码。

不只是漂亮 (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.

滚动浏览较长的man页很容易,并且会错过重要的信息,例如选项或参数,因为它丢失了很多文字。

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

现在,参数和选项名称将突出显示,使您更容易发现。

翻译自: https://www.howtogeek.com/683134/how-to-display-man-pages-in-color-on-linux/

linux命令行彩色显示

你可能感兴趣的:(python,linux,java,ubuntu,shell)