怎么做:Gnome终端的下划线,粗体,斜体,删除线,颜色,背景和大小?

怎么做:Gnome终端的下划线,粗体,斜体,删除线和颜色?

胆大

斜体

强调

s̶̶̶̶̶̶̶̶̶̶̶̶̶

颜色

background

font <(如果你不知道,它的单声道)

尺寸

ANSI / VT100终端和终端仿真器不仅能够显示黑白文本; 由于转义序列,它们可以显示颜色和格式化文本。 这些序列由Escape字符组成(通常用“^ [”或“Esc”表示),后跟一些其他字符:“Esc [FormatCodem”。

在Bash中,可以使用以下语法获取字符:

 \e \033 \x1B 

在此处输入图像描述

命令(便于复制粘贴):

 echo -e "\e[1mbold\e[0m" echo -e "\e[3mitalic\e[0m" echo -e "\e[4munderline\e[0m" echo -e "\e[9mstrikethrough\e[0m" echo -e "\e[31mHello World\e[0m" echo -e "\x1B[31mHello World\e[0m" 

来源(包括所有类型的前景/背景颜色代码): http : //misc.flogisoft.com/bash/tip_colors_and_formatting

为了扩展Sylvain的答案,一些辅助函数:

 ansi() { echo -e "\e[${1}m${*:2}\e[0m"; } bold() { ansi 1 "$@"; } italic() { ansi 3 "$@"; } underline() { ansi 4 "$@"; } strikethrough() { ansi 9 "$@"; } red() { ansi 31 "$@"; } 

然后

在此处输入图像描述

尚未涵盖的东西是预定义颜色的两个或三个参数的组合 ,例如粗体下划线 。 这是通过3种语法实现的,例如:

 ~$ printf "\e[3;4;33mthis is a test\n\e[0m" 

将导致“这是一个测试”打印成黄色( 33m 3m ),斜体( 3m )和下划线( 4m )。
请注意, 没有必要重复\e[每次。
另请注意(与Sylvain相似)我每次都添加了一个\e[0m来重置设置,因为否则黄色和字体样式将在终端中保持活动状态! 毋庸置疑,您必须注意这些在脚本中重置,因为如果您的脚本永久修改终端中的颜色+样式设置,使用您的脚本的用户可能会不喜欢它!

GNOME终端3.28(VTE 0.52),在Ubuntu 18.04 LTS上首次亮相,增加了对更多样式的支持,包括在Kitty中看到的curl和彩色下划线,在Konsole中看到的上线,最后每个人都非常喜欢或非常讨厌blink属性。

考虑到VTE至少为版本0.52,这些也可以自动在任何其他基于VTE的终端仿真器(例如Tilix,终结器,Xfce4终端,Guake等)中工作。

这是一个列表,展示标准转义序列,以及GNOME终端(VTE)的新增function。 请注意,对于每个开始序列,我也只显示该属性的结束序列,而不是通用的\e[m\e[0m ,它禁用所有特殊模式。

 echo -e '\e[1mbold\e[22m' echo -e '\e[2mdim\e[22m' echo -e '\e[3mitalic\e[23m' echo -e '\e[4munderline\e[24m' echo -e '\e[4:1mthis is also underline (new in 0.52)\e[4:0m' echo -e '\e[21mdouble underline (new in 0.52)\e[24m' echo -e '\e[4:2mthis is also double underline (new in 0.52)\e[4:0m' echo -e '\e[4:3mcurly underline (new in 0.52)\e[4:0m' echo -e '\e[5mblink (new in 0.52)\e[25m' echo -e '\e[7mreverse\e[27m' echo -e '\e[8minvisible\e[28m <- invisible (but copy-pasteable)' echo -e '\e[9mstrikethrough\e[29m' echo -e '\e[53moverline (new in 0.52)\e[55m' echo -e '\e[31mred\e[39m' echo -e '\e[91mbright red\e[39m' echo -e '\e[38:5:42m256-color, de jure standard (ITU-T T.416)\e[39m' echo -e '\e[38;5;42m256-color, de facto standard (commonly used)\e[39m' echo -e '\e[38:2::240:143:104mtruecolor, de jure standard (ITU-T T.416) (new in 0.52)\e[39m' echo -e '\e[38:2:240:143:104mtruecolor, rarely used incorrect format (might be removed at some point)\e[39m' echo -e '\e[38;2;240;143;104mtruecolor, de facto standard (commonly used)\e[39m' echo -e '\e[46mcyan background\e[49m' echo -e '\e[106mbright cyan background\e[49m' echo -e '\e[48:5:42m256-color background, de jure standard (ITU-T T.416)\e[49m' echo -e '\e[48;5;42m256-color background, de facto standard (commonly used)\e[49m' echo -e '\e[48:2::240:143:104mtruecolor background, de jure standard (ITU-T T.416) (new in 0.52)\e[49m' echo -e '\e[48:2:240:143:104mtruecolor background, rarely used incorrect format (might be removed at some point)\e[49m' echo -e '\e[48;2;240;143;104mtruecolor background, de facto standard (commonly used)\e[49m' echo -e '\e[21m\e[58:5:42m256-color underline (new in 0.52)\e[59m\e[24m' echo -e '\e[21m\e[58;5;42m256-color underline (new in 0.52)\e[59m\e[24m' echo -e '\e[4:3m\e[58:2::240:143:104mtruecolor underline (new in 0.52) (*)\e[59m\e[4:0m' echo -e '\e[4:3m\e[58:2:240:143:104mtruecolor underline (new in 0.52) (might be removed at some point) (*)\e[59m\e[4:0m' echo -e '\e[4:3m\e[58;2;240;143;104mtruecolor underline (new in 0.52) (*)\e[59m\e[4:0m' 

(*)下划线的真彩色值略微近似。

有点奇怪的不太适合这张图片,因为它更像是一种function而非风格,但在这里可能值得一提,是与iTerm2共同设计的超链接支持,自GNOME终端3.26(VTE 0.50)开始提供:

 echo -e '\e]8;;http://askubuntu.com\ahyperlink\e]8;;\a' 

这是显示结果的屏幕截图: 在gnome-terminal 3.28中渲染