17
8
I always make my prompt to be this color in my linux
Then I can find my input in the black command window. But can we make the same color in my Powershell?
17
8
I always make my prompt to be this color in my linux
Then I can find my input in the black command window. But can we make the same color in my Powershell?
32
What you want to do is customise the prompt.
The default prompt in PowerShell 3.0 and newer is:
function prompt
{
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
}
Now, Windows 10 supports the ANSI escape codes in its conhost, and 24-bit colour is supported since 1703.
To use one of the ANSI escape codes, we need a literal ESC character. This is octal 033, or decimal 27. In Bash, you would use "\033"
or "\e"
; There's no direct equivalent sequence in PowerShell but you can instead embed an expression: "$([char]27)"
In PowerShell Core you can use the "`e"
escape sequence instead. Thanks to the comment by asherber.
This means you can, for example, use the following escape sequence to set the background colour to cyan with the code 46
:
"$([char]27)[46m"
Which can be used like so:
echo "$([char]27)[46mColoured text"
To make things more readable, let's define a variable:
$ESC = [char]27
So we can use:
"$ESC[46m"
Where $ESC
is the literal ESC character, [
defines the start of the param list, and m
the end.
Using it in a command:
echo "$ESC[46mColoured text"
And to restore the original colour settings, we use the code 0
instead:
echo "$ESC[46mColoured text$ESC[0mUncoloured text"
If you aren't happy with the basic 16-colour palette, you can use full 24-bit colours with the form:
"$ESC[48;2;<r>;<g>;<b>m"
Where <r>
is the decimal value for red from 0-255, <g>
for green and <b>
for blue, e.g. "$ESC[48;2;255;0;123m"
Please refer to the documentation for more information.
In fact, this is all directly equivalent to what you would do in bash, except you need to use $ESC
(after you've defined it) instead of \e
or \033
.
Putting these together, we can use the ANSI colour escape codes in a modified prompt to change the background colour:
function prompt
{
$ESC = [char]27
"$ESC[46mPS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) $ESC[0m"
}
If you want this to persist across sessions, add it to your profile (like .bashrc
). You can view the path to your profile in $profile
.
Hi~,it is a great answer..But further more, could we make the text be bold style?Then we can find it more easy..
– yode – 2017-10-17T14:51:16.820@yode You can add the code 1
to enable the bold/bright option, e.g. $ESC[46;1m
. However, it looks like conhost currently does not support an actual bold typeface — it just uses a bright foreground colour. You can add 4
for an underline if you prefer. – Bob – 2017-10-17T15:02:38.863
Crazy man. Could you tell me how do you find this information? – yode – 2017-10-17T15:12:02.147
1
@yode It's more or less all documented here, which I linked in the answer ;)
– Bob – 2017-10-17T15:14:15.627As your lilnk, it seem the 91 is what I after. But is seem it doesn't work.. – yode – 2017-10-17T19:38:14.403
Instead of defining $ESC
, why not define things like $RED
, $BLUE
and $OFF
. I think that would really enhance readability. "This ${BLUE}phrase is blue${OFF} and ${RED}this one is red${OFF}."
– Paused until further notice. – 2017-10-17T22:37:28.717
@DennisWilliamson There's enough different cases that I think it gets unwieldy - how do you distinguish between blue background and foreground? And you can change multiple formatting settings in a single command, e.g. colour + underline. It makes more sense if you need it across many commands, e.g. $COLOUR_ERROR
. But that's not really useful for a prompt line. $ESC
was done so it can be more or less a drop-in replacement for the same command in bash (\e
). – Bob – 2017-10-17T23:17:15.387
@yode In what way does it not work? What command did you use exactly? That won't give you bold but it should give you a bright red foreground colour. – Bob – 2017-10-17T23:17:54.437
1
@yode Ah. If you compare to 37 non-bright white, it looks like PowerShell windows already use 97 bright white by default. (btw, there's going to be a new colour palette for clean installs of the next Win10 version. There's some screenshots there of the current palette; note that 1;37m
is the same as 97m
, both meaning "bright white")
1
@yode Basically, conhost does not yet support bold. Bright is a substitute for bold, and you're already using the bright colour by default. If you really want bold, you can use an alternative terminal, such as ConEmu — but it looks like their support for ANSI bold isn't complete either. IMO, it isn't worth the trouble.
– Bob – 2017-10-18T03:47:24.1771I can't get the Markdown to work correctly, but in PowerShell Core you can use backtick+e for [char]27
. I'm not sure if this was added in earlier versions. – asherber – 2019-12-18T23:59:55.673
@asherber Thanks. I've added a mention but I'll keep the rest of the answer valid for older PowerShell versions. I've just confirmed that the version bundled with Win10 does not support that escape sequence. – Bob – 2019-12-19T10:01:51.030
What is the console font? – Dmitry Kudriavtsev – 2017-10-17T19:16:52.267
@DmitryKudriavtsev Look this, maybe it is Sim song in English.
– yode – 2017-10-17T19:36:37.330This is SimSun maybe? – Dmitry Kudriavtsev – 2017-10-17T19:37:15.173
@DmitryKudriavtsev Yes, sorry I make a mistake. – yode – 2017-10-17T19:38:55.430
not your fault, transliteration is hard – Dmitry Kudriavtsev – 2017-10-17T19:39:20.827