cmd.exe 和 PowerShell 中提示符(只是正确的提示符)的颜色?

发布于 2024-11-14 14:33:16 字数 648 浏览 4 评论 0原文

因此,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

地狱即天堂 2024-11-21 14:33:16

您可以在 Powershell 中将提示功能添加到您的个人资料中,以便通过提示执行您想做的任何事情。例如,像这样:

function prompt
{
    Write-Host "PS $(get-location)>"  -nonewline -foregroundcolor Magenta
    return ' '
}

要打开或创建您的 PowerShell 配置文件,请运行以下命令:

if(Test-Path $profile){notepad $profile}else{New-Item -path $profile -type file -force}

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:

function prompt
{
    Write-Host "PS $(get-location)>"  -nonewline -foregroundcolor Magenta
    return ' '
}

To open or create your PowerShell profile, run this:

if(Test-Path $profile){notepad $profile}else{New-Item -path $profile -type file -force}
蹲在坟头点根烟 2024-11-21 14:33:16

基于 @KriZ 的答案,ANSI 转义序列在 2019 年的 Windows 10 cmd.exe 中完美运行。不需要显式调用 ansi.sys 或复制任何内容文件。它在 Windows 10 中开箱即用。

例如,

set PROMPT=$E[1;37m[user@machine:$E[1;35m$P ]$ $E[1;37m

生成:

示例彩色提示

(注意最后的 $ 后面的空格)

驱动器之前的所有内容均以粗体白色显示,驱动器/文件夹是粗粉色的,最后的 $ 之后的所有内容都是粗体白色。

颜色的格式为:

$E[bold_or_not;colorm

m 始终跟在颜色编号后面。 bold_or_not = 0 或 1。以下是颜色指南:

0 关闭属性
1 高强度
2 正常强度
4 下划线(仅限单色)
5 眨眼
7 倒车影像
8 隐形
30黑色
31 红
32 绿色
33 黄色
34 蓝色
35 洋红色
36青色
37 白色
40黑色
41 红
42 绿色
43 黄色
44 蓝色
45 洋红色
46青色
47 白色

颜色来源: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 out ansi.sys or copy any files. It just worked out of the box in Windows 10.

For example,

set PROMPT=$E[1;37m[user@machine:$E[1;35m$P ]$ $E[1;37m

Produces:

example colored prompt

(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:

$E[bold_or_not;colorm

With m always following the color number. bold_or_not = 0 or 1. Here's a guide for the colors:

0     Turn Off Attributes
1     High Intensity
2     Normal Intensity
4     Underline (mono only)
5     Blink
7     Reverse Video
8     Invisible
30    Black
31    Red
32    Green
33    Yellow
34    Blue
35    Magenta
36    Cyan
37    White
40    Black
41    Red
42    Green
43    Yellow
44    Blue
45    Magenta
46    Cyan
47    White

Colors Source: https://kb.iu.edu/d/aamm

看春风乍起 2024-11-21 14:33:16

请点击此链接。有一个为 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

enter image description here

归途 2024-11-21 14:33:16

这些都是很好的信息,但我没有看到解决的一个重要问题是如何使自定义提示符在每次运行命令提示符时出现。在较旧的 Windows 中,例如 XP 及之前的版本,您可以将 PROMPT 环境变量放入 AUTOEXEC.BAT 文件中,但在 Windows 7 到 Windows 10 中,您可以将其永久化,如下所示:

  • 打开 使用 Windows 键 + R >“运行”提示符
  • 键入“systempropertiesadvanced”(不带引号)并按 ENTER
  • 这将打开“系统属性”对话框(您也可以右键单击我的电脑并选择属性来获取它)
  • 选择顶部的“高级”选项卡
  • 选择底部附近的“环境变量”
  • 在下部区域的“系统变量”区域中,查看当前是否有一个名为“提示”的变量(大小写无关紧要)
  • 如果是这样,请编辑提示变量,并且您的更改将是永久性的。
  • 如果不是,请单击底部附近的“新建”,然后对于变量名称,输入 PROMPT 并输入变量值,无论您想要什么。默认提示符的变量值为 $P$G
  • 单击“确定”
  • 运行命令提示符以测试
  • “完成”
  • 注意:我使用看起来像德克萨斯州旗帜的自定义命令提示符。其变量值为:
    $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:

  • Open the Run prompt by using the Windows key + R
  • Type "systempropertiesadvanced" (without the quotes) and hit ENTER
  • This will open the System Properties dialog box (You can also right click My Computer and choose Properties to get this)
  • Select the "Advanced" Tab at the top
  • Choose "Environment Variables" near the bottom
  • In the lower area, in the "System variables" area, look and see if you currently have a variable called "Prompt" (capitalization doesn't matter)
  • If so, edit the prompt variable and your changes with be permanent
  • If not, click "New" near the bottom and for Variable name, enter PROMPT and for the variable value, whatever you want it to be. The default prompt has a variable value of $P$G
  • Click OK
  • Run the command prompt to test
  • DONE
  • Note: I use a custom command prompt which looks like the Texas flag. The Variable value for this is:
    $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.)

娇俏 2024-11-21 14:33:16

您可以使用多种颜色(对于识别提示符的组成部分非常有用,典型的是 Unix):

function prompt {
    Write-Host ("@") -NoNewLine -ForegroundColor Magenta
    Write-Host ("$env:COMPUTERNAME") -NoNewLine -ForegroundColor Green
    Write-Host (":") -NoNewLine -ForegroundColor Magenta
    Write-Host ($(Get-Location)) -NoNewLine -ForegroundColor Green
    Write-Host (">") -NoNewLine -ForegroundColor Red
    return " "
}

在此处输入图像描述

(< code>COMPUTERNAME 已明确写入此处,但实际上它被环境变量的值替换)。

您可以添加随机颜色(取自 此处; 这个有一个类似的版本;两者都有其他非常有趣的调整):

function prompt
{
    $random = new-object random
    $color=[System.ConsoleColor]$random.next(1,16)
    Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor $color
    return " "
}

You can use multiple colors (very useful for identifying components of your prompt, typical in Unix):

function prompt {
    Write-Host ("@") -NoNewLine -ForegroundColor Magenta
    Write-Host ("$env:COMPUTERNAME") -NoNewLine -ForegroundColor Green
    Write-Host (":") -NoNewLine -ForegroundColor Magenta
    Write-Host ($(Get-Location)) -NoNewLine -ForegroundColor Green
    Write-Host (">") -NoNewLine -ForegroundColor Red
    return " "
}

enter image description here

(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):

function prompt
{
    $random = new-object random
    $color=[System.ConsoleColor]$random.next(1,16)
    Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor $color
    return " "
}
伏妖词 2024-11-21 14:33:16

感谢所有现有的答案,这是我的命令提示符(如果您喜欢它,您可以使用它)

$E[7;33m$P$_$E[0;37m[$E[0;32m%username%@%computername%$E[0;36m$S$D$S$T$H$H$H$E[0;37m]$$E[0;37m$S

只需添加一个具有上述值的用户变量 PROMPT

输出:

在此处输入图像描述

Thanks to all existing answers, this is my command prompt (if you like it, you can use it)

$E[7;33m$P$_$E[0;37m[$E[0;32m%username%@%computername%$E[0;36m$S$D$S$T$H$H$H$E[0;37m]$$E[0;37m$S

Just need to add a User Variable PROMPT with the above value

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文