问题描述
怎么做:Gnome终端的下划线,粗体,斜体,删除线和颜色?
胆大
斜体
强调
s̶̶̶̶̶̶̶̶̶̶̶̶̶
background
font
< (如果你不知道它的单声道)
size
最佳解决方法
The ANSI/VT100 terminals and terminal emulators are not just able to display black and white text; they can display colors and formatted texts thanks to escape sequences. Those sequences are composed of the Escape character (often represented by “^[” or “Esc”) followed by some other characters: “Esc[FormatCodem”.
在Bash中,可以使用以下语法获取字符:
\e
\033
\x1B
命令(方便copy-paste):
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 "$@"; }
然后
第三种解决方法
尚未涵盖的东西是两个或三个参数的组合,例如: G。以预定义颜色加粗和下划线。这是通过3种语法实现的,例如:
~$ printf "\e[3;4;33mthis is a test\n\e[0m"
将导致“这是一个测试”以黄色(33m
),斜体(3m
)和下划线(4m
)打印。请注意,不必每次都重复\e[
。另请注意(与Sylvain相似)我每次都添加了一个\e[0m
来重置设置,否则黄色和字体样式将在终端中保持活动状态!毋庸置疑,您必须注意这些在脚本中重置,因为如果您的脚本永久修改终端中的颜色+样式设置,使用您的脚本的用户可能会不喜欢它!
第四种方法
GNOME终端3.28(VTE 0.52),在Ubuntu 18.04 LTS上首次亮相,增加了对更多样式的支持,包括在Kitty中看到的 curl 和彩色下划线,在Konsole中看到的上线,最后每个人都非常喜欢或非常讨厌blink属性。
考虑到VTE至少为版本0.52,这些也可以自动在任何其他VTE-based终端仿真器(例如Tilix,终结器,Xfce4终端,Guake等)中工作。
这是一个列表,演示了标准的转义序列,以及GNOME Terminal’s (VTE’s)的补充。请注意,对于每个打开序列,我还仅显示该属性的关闭序列,而不是禁用所有特殊模式的通用\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'
(*)下划线的真彩色值略微近似。
并且有点奇怪的不适合这张图片,因为它更像是一种功能而不是风格,但在这里可能值得一提,hyperlink支持co-designed和iTerm2,自GNOME终端3.26(VTE 0.50)起可用:
echo -e '\e]8;;http://askubuntu.com\e\\hyperlink\e]8;;\e\\'
这是显示结果的屏幕截图: