cmd.exe 和 PowerShell 中提示符(只是正确的提示符)的颜色?
因此,在 Bash 中,您只需配置 PS1
即可为提示添加颜色。我说的是正确的提示,而不是前景(文本)或背景的颜色。在 Bash 中这真的很容易,如果您需要在一片混乱的文本输出中找到您的命令,它会很有帮助。
您能否为 cmd.exe
实现相同的效果,或者作为 PowerShell 的后备方案?彩色提示?
我不知道在Win32之前是否可以通过加载ANSI.SYS
来完成。我认为这只是为了让前景和背景变得丰富多彩。但我可能错了。不管怎样,那些日子已经一去不复返了,在我们的现代(我知道),我们正在使用 cmd.exe
或 PowerShell。
我知道 cmd.exe 和 PowerShell 都能够进行彩色输出。对于 cmd.exe,只需运行 color /?
即可找到答案。但我的问题不是关于前景和背景,这是人类都知道的 - 它只是关于更改 cmd.exe 的提示颜色,可能通过 PROMPT
环境变量(如通过 PS1) Bash 的
变量?是否可以?
不,Cygwin 并不是这个的替代方案。我是 Cygwin 用户,使用 MinTTY 等,我喜欢它。但我仍然希望我的 cmd.exe
提示符也有颜色。
So in Bash you just configure PS1
to add colors to your prompt. I'm talking about the prompt proper, not the color of the foreground (text) or the background. And it's really easy in Bash and it helps a lot if you need to find your commands in a sea of messy text output.
Can you achieve the same for cmd.exe
, or as a fallback, for PowerShell? A colored prompt?
I don't know if it could be done in the old days before Win32 by loading ANSI.SYS
. I think that was just to make the foreground and the background colorful. But I might be wrong. And anyway, those days are gone, and in our modern times (I know), we're using cmd.exe
, or PowerShell.
I know both cmd.exe and PowerShell are capable of colored output. For cmd.exe, just run color /?
to find out. But my question is not about the foreground and the background, that's all known to humankind - it's about just changing the prompt color for cmd.exe, probably via the PROMPT
environment variable as via the PS1
variable for Bash? Is it possible?
And no, Cygwin is not an alternative for this. I'm a Cygwin user with MinTTY and all, and I love it. But I still want my cmd.exe
prompt colored, too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以在 Powershell 中将提示功能添加到您的个人资料中,以便通过提示执行您想做的任何事情。例如,像这样:
要打开或创建您的 PowerShell 配置文件,请运行以下命令:
You can add a Prompt function to your profile in Powershell to pretty much do whatever you want with the prompt. So for instance something like this:
To open or create your PowerShell profile, run this:
基于 @KriZ 的答案,ANSI 转义序列在 2019 年的 Windows 10
cmd.exe
中完美运行。不需要显式调用ansi.sys
或复制任何内容文件。它在 Windows 10 中开箱即用。例如,
生成:
(注意最后的
$
后面的空格)驱动器之前的所有内容均以粗体白色显示,驱动器/文件夹是粗粉色的,最后的
$
之后的所有内容都是粗体白色。颜色的格式为:
m 始终跟在颜色编号后面。 bold_or_not = 0 或 1。以下是颜色指南:
颜色来源:https://kb.iu.edu/d/aamm
Building on @KriZ's answer, the ANSI escape sequences work perfectly in Windows 10
cmd.exe
as of 2019. Didn't need to explicitly call outansi.sys
or copy any files. It just worked out of the box in Windows 10.For example,
Produces:
(Notice the space after the final
$
)Everything before the drive is colored in bold white and the drive/folder is bold pink, and everything after the final
$
is bold white.The format for the colors is:
With m always following the color number. bold_or_not = 0 or 1. Here's a guide for the colors:
Colors Source: https://kb.iu.edu/d/aamm
请点击此链接。有一个为 CMD.exe shell 开发的 ANSI hack
ansi hack 链接
I已经在我的 win 7 professional SP1 上尝试过了,效果非常好
follow this link. There's an ANSI hack developped for the CMD.exe shell
link to ansi hack
I've tried it on my win 7 professional SP1 and works like a charm
这些都是很好的信息,但我没有看到解决的一个重要问题是如何使自定义提示符在每次运行命令提示符时出现。在较旧的 Windows 中,例如 XP 及之前的版本,您可以将 PROMPT 环境变量放入
AUTOEXEC.BAT
文件中,但在 Windows 7 到 Windows 10 中,您可以将其永久化,如下所示:使用 Windows 键 + R >“运行”提示符
ENTER
$e[1;44m*$e[41m▀▀$e[0;1m $P$G]
(白色条是通过按住 ALT 并在右侧键盘上输入 223 来制作的。此提示中有两个这样的字符。)
This is all good information but an important thing that I didn't see addressed is how to make the custom prompt appear each time you run a command prompt. In older Windows, such as XP and before, you would put the PROMPT environment variable in the
AUTOEXEC.BAT
file but in Windows 7 through Windows 10, you would make it permanent as follows:Run
prompt by using the Windowskey + R
ENTER
$e[1;44m*$e[41m▀▀$e[0;1m $P$G
(The white bar is made by holding down ALT and typing 223 on the keypad on the right. There are two of these characters in this prompt.)
您可以使用多种颜色(对于识别提示符的组成部分非常有用,典型的是 Unix):
(< code>COMPUTERNAME 已明确写入此处,但实际上它被环境变量的值替换)。
您可以添加随机颜色(取自 此处; 这个有一个类似的版本;两者都有其他非常有趣的调整):
You can use multiple colors (very useful for identifying components of your prompt, typical in Unix):
(
COMPUTERNAME
was explicitly written here, but it actually gets replaced by the value of the environment variable).And you can add random colors (taken from here; this has a similar version; both have other very interesting tweaks):
感谢所有现有的答案,这是我的命令提示符(如果您喜欢它,您可以使用它)
只需添加一个具有上述值的用户变量
PROMPT
输出:
Thanks to all existing answers, this is my command prompt (if you like it, you can use it)
Just need to add a User Variable
PROMPT
with the above valueOutput: