Customizing the Powershell Prompt
Initially I was scanning through Google as usual and found some solutions. The key idea was taken from this SOF thread. One needs to use Virtual Terminal Sequences to do this.
Using Virtual Terminal Sequences:
From the Microsoft documentation on this:
Virtual terminal sequences are control character sequences that can control cursor movement, color/font mode, and other operations when written to the output stream.
We’re interested in color/font mode to change the appearance of our prompt. We need a sequence of the format
ESC[<n>m
There are two parts to this:
- We need to the start with the ASCII ESC character (hex 0x1B). This can be achieved by writing the expression
$([char]27)
in Powershell. - For
<n>
we insert an appropriate integer code that corresponds to a particular formatting style. The entire list can be found in the same doc page.
When we combine the two parts, we write something like:
$([char]27)[36m
which applies non-bold/bright cyan to foreground. The text that follows will acquire this style. To negate all styles we use ESC[0m
.
Custom colours:
With ESC[38;2;<r>;<g>;<b>m
(ESC[48;...
) one can use any (r, g, b) value for the text foreground (background). For instance, (250, 128, 114) is the salmon colour.
Changing the prompt permanently
We modify the Powershell profile file to make changes permanent. (Creating profiles) We add the following lines:
$ESC = [char]27
function prompt {
$(if (Test-Path variable:/PSDebugContext) { '[DBG]: ' }
else { '' }) + "$ESC[38;2;250;128;114mPS@$ESC[38;2;127;255;255m$ESC[4m" + $(Get-Location) + "$ESC[24m" +
$(if ($NestedPromptLevel -ge 1) { '>>' }) + "> $ESC[0m"
}
where
- We have stored the ESC character in a variable to ease our life.
- The
prompt()
function is used to modify the prompt. The code within theprompt()
function has been obtained from the doc on Powershell prompt. - The built-in prompt has been modified1 by inserting sequences at appropriate places. Currently, it should look like this:
For changes to take effect immediately, source the profile using . $profile
.
-
The original SOF solution (and many other places) give this solution:
function prompt { "$ESC[93mPS $ESC[36m$($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) $ESC[0m" }
but
$('>' * ($nestedPromptLevel + 1))
causes issues. More specifically, the character m appeared automatically after the prompt when I was typing a period.
which is undesirable behaviour. TO BE INVESTIGATED. ↩