Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How achieve a two line Prompt?

In the batch language of Microsoft's CMD.EXE console window, I never liked having my command start at the far right, after a long display of the directory path. So in my Control Panel → System → Advanced System Settings → Environment Variables I saved the following assignment, where $_ is like a Soft Return:

PROMPT=[$P\]$_$+$G$S

The displayed prompt was two lines like this:

[C:\Temp\]
>

(The $+ tracks pushd and popd, the fancier than chdir commands. $S is space. By the way, the ^ character a line wrap/continuation character in batch, just as backtick ` is in PowerShell.)

Now I want the same-ish two line prompt in PowerShell. There is good news and bad news.

The good news is I can achieve that in my open PowerShell window by typing at the > prompt:

 function prompt {'[' + $(get-location) + '\] SHIFTENTER > '

(By SHIFTENTER I mean press Shift+Enter, what I think might be called a "soft return"?)

....... BAD NEWS, PROBLEM ......

I want to put the above function prompt ... line into my profile PowerShell script, namely Microsoft.PowerShell_profile.ps1 (at path $Profile). But how?

Notepad.exe has no support for Shift+Enter.

MS Word understands Shift+Enter, but when I SaveAs .txt, and then examine with Notepad++, I see a plain CR-LF (meaning \r\n, 0x0d 0x0a).

Notepad++ menu Edit → Character Panel enables me to insert special ASCII characters into my .txt / .ps1 file, such as 0x0b called VT (for "vertical tab"). But despite some claims on websites, VT is not behaving like a Soft Return when I use it in my function prompt ... profile .ps1 file (I also run the profile .ps1 script to retest).

Can the prompt I want be established by a profile .ps1 script?

like image 300
MightyPen Avatar asked May 21 '16 20:05

MightyPen


1 Answers

The PowerShell equivalent of your batch-prompt is:

function prompt { "[$(Get-Location)\]`r`n$("+"*(Get-Location -Stack).Count)>" }

#`r`n is just a shorter way of writing [System.Environment]::NewLine

Add it to the profile to suits your needs:

AllUsersAllHosts:

C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

AllUsersPowerShell:

C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1

AllUsersISE:

C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1

CurrentUserAllHosts:

C:\Users\username\Documents\WindowsPowerShell\profile.ps1

CurrentUserPowerShell:

C:\Users\username\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

CurrentUserISE:

C:\Users\username\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

like image 153
Frode F. Avatar answered Oct 15 '22 11:10

Frode F.