改变终端文本和背景的颜色?
我可以通过首选项更改颜色,但如何通过命令行更改终端的背景颜色和文本颜色?
在某些与XTerm / ANSI兼容的终端(如xterm
和gnome-terminal
)上,您可以使用大于默认8/16色调色板的调色板设置颜色(例如使用88色,256色或16777216色) (RGB)调色板; xterm
和gnome-terminal
支持256色和16777216色(RGB)调色板); 请注意,shell可能会覆盖它(例如对于zsh
就是这种情况)。
这是一个脚本,用于列出256色调色板中的颜色以及具有256色调色板支持的XTerm / ANSI兼容终端中的ANSI颜色代码:
#!/bin/bash for((i=16; i<256; i++)); do printf "\e[48;5;${i}m%03d" $i; printf '\e[0m'; [ ! $((($i - 15) % 6)) -eq 0 ] && printf ' ' || printf '\n' done
根据您是要将颜色应用于前景还是背景,请在以下命令中使用
值38
或48
(分别):
printf '\e[;5;m'
例如,要将前景色(
= 38
)设置为红色(
= 196
),将背景色(
= 48
)设置为黑色(
= 0
):
printf '\e[38;5;196m Foreground color: red\n' printf '\e[48;5;0m Background color: black\n'
有必要使用printf '\e[K'
重绘提示,以便将背景颜色应用于整行,并将前景色应用于光标:
在兼容终端中使用RGB值代替ANSI颜色代码可以完成同样的事情; 根据您是要将颜色应用于前景还是背景,请在以下命令中使用
值38
或48
(分别):
printf '\e[;2;;;m'
例如,将前景色(
= 38
)设置为红色(
= 255
,
= 0
, =
0
),将背景色(
= 48
)设置为黑色(
= 0
,
= 0
, =
0
):
printf '\e[38;2;255;0;0m Foreground color: red\n' printf '\e[48;2;0;0;0m Background color: black\n'
同样,有必要使用printf '\e[K'
重新绘制提示,以便将背景颜色应用于整行,并将前景色应用于光标:
使用任一方法,您可以使用printf '\e[0m'
重置所有属性:
偶然更改颜色
如果它是为了偶然改变颜色 :
您可以使用setterm命令:
setterm -term linux -back -fore -clear
从颜色,你可以选择(前面和后面):
black|blue|green|cyan|red|magenta|yellow|white|default
了解更多选择:
setterm -help
更改您的个人资料(颜色)设置
在14.04,我没有找到使用dconf
设置颜色或终端的选项。 但是你可以使用gconftool
-
您首先需要获取您的个人资料名称:
gconftool-2 --get /apps/gnome-terminal/global/profile_list
-
然后,设置个人资料的文字颜色:
gconftool-2 --set "/apps/gnome-terminal/profiles/
/foreground_color" --type string "#FFFFFF" 例如,将文本颜色设置为白色
背景颜色相同:
gconftool-2 --set "/apps/gnome-terminal/profiles/
/background_color" --type string "#000000" 例如,将背景颜色设置为黑色
或者,要设置颜色的名称,您可以使用与setterm命令相同的调色板中的white
或green
,例如:
gconftool-2 --set "/apps/gnome-terminal/profiles//background_color" --type string black
此页面上的信息,不包括预览列:
序列由Escape字符组成(通常用“ ^[
”或“
”表示),后跟一些其他字符:“
”(其中FC
是下面项目符号列表中的数字之一)。
在bash
, Esc代码可以是以下任一种:
-
\e
-
\033
(八进制) -
\x1B
(hex)
注1: “ \e[0m
”序列删除所有属性(格式和颜色)。 在每个彩色文本的末尾添加它是个好主意。
注意2:前景色和背景色可能会有所不同,具体取决于终端的配置, 并不支持所有颜色 。
设置/复位
-
0
:重置/删除所有修饰符,前景和背景属性:echo -e "\e[0mNormal Text"
-
1
:Bold / Bright:echo -e "Normal \e[1mBold"
-
2
:昏暗:echo -e "Normal \e[2mDim"
-
4
:带下划线:echo -e "Normal \e[4mUnderlined"
-
5
:闪烁(除了XTerm以外的大多数终端都不起作用):echo -e "Normal \e[5mBlink"
-
7
:反向/反转:echo -e "Normal \e[7minverted"
-
8
:隐藏(对敏感信息有用):echo -e "Normal \e[8mHidden Input"
-
21
:重置/删除粗体/亮:echo -e "Normal \e[1mBold \e[21mNormal"
-
22
:重置/删除暗淡:echo -e "Normal \e[2mDim \e[22mNormal"
-
24
:重置/删除下划线:echo -e "Normal \e[4mUnderlined \e[24mNormal"
-
25
:重置/删除闪烁:echo -e "Normal \e[5mBlink \e[25mNormal"
-
27
:复位/移除反向/反转:echo -e "Normal \e[7minverted \e[27mNormal"
-
28
:重置/删除隐藏:echo -e "Normal \e[8mHidden \e[28mNormal"
前景
-
39
:默认(通常为绿色,白色或浅灰色):echo -e "Default \e[39mDefault"
-
30
:黑色:echo -e "Default \e[30mBlack"
(最好结合背景颜色:echo -e "Default \e[30;107mBlack on white"
) -
31
:红色(不要使用绿色背景) -
32
:绿色 -
33
:黄色 -
34
:蓝色 -
35
:洋红色/紫色 -
36
:青色 -
37
:浅灰色 -
90
:深灰色 -
91
:浅红色 -
92
:浅绿色 -
93
:浅黄色 -
94
:浅蓝色 -
95
:浅洋红色/粉红色 -
96
:浅青色 -
97
:白色
背景
-
49
:默认背景颜色(通常为黑色或蓝色) -
40
:黑色 -
41
:红色 -
42
:绿色 -
43
:黄色 -
44
:蓝色 -
45
:洋红色/紫色 -
46
:青色 -
47
:浅灰色(不要与白色前景一起使用) -
100
:深灰色(不要使用黑色前景) -
101
:浅红色 -
102
:浅绿色(不要与白色前景一起使用) -
103
:浅黄色(不要与白色前景一起使用) -
104
:浅蓝色(不要使用浅黄色前景) -
105
:浅洋红色/粉红色(不要用于浅色前景) -
106
:浅青色(不要与白色前景一起使用) -
107
:白色(不要使用浅色前景)
要同时设置前景色和背景色,请使用其他forms的echo -e "\e[S;FG;BGm"
。 例如: echo -e "\e[1;97;41m"
(红色背景上的粗体白色前景)
有关256色选项,请参阅源页面。
用于获取彩色输出的各种颜色代码也可用于获取彩色背景 :
40 black 41 red 42 green 43 yellow 44 blue 45 magenta 46 cyan 47 white
因此,以下命令将我的背景变为红色:
$ echo -e '\e[0;41m'
根据shell,终端仿真器等,您可能不需要-e
。