1

I am trying to add a new line before each Powershell prompt to improve readability as shown below.

Instead of:

(base) PS C:\Users\myname --> "Hello"
Hello
(base) PS C:\Users\myname --> "Wow"
Wow

I would like an output like this:

(base) PS C:\Users\myname --> "Hello"
Hello

(base) PS C:\Users\myname --> "Wow"
Wow

(base) PS C:\Users\myname -->

My powershell profile, which I have modified for my prompt and to add the conda environment hook as per other SO posts is as follows:

function Prompt
{
    $promptString = "PS " + $(Get-Location) + " -->"
    Write-Host "$promptString" -NoNewline -ForegroundColor Yellow
    return " "
}

%{& "C:\Users\myname\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook"} | Out-String | Invoke-Expression

I have tried adding the new line parameter ("`n") to the function prompt as follows:

$promptString = "`nPS " + $(Get-Location) + " -->"

This gives me the output as:

(base)
PS C:\Users\myname -->

which is not what I want.

I also tried adding a Write-Host before the conda hook but that doesn't work either:

Write-Host `n | %{& "C:\Users\myname\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook"} | Out-String | Invoke-Expression

It gives me the error: "Invoke-Expression : Cannot bind argument to parameter 'Command' because it is an empty string." So this doesn't work either.

I assume this should be a simple fix but I'm unable to find the right commands/place to put the command that will allow me to add that new line to improve readability of the code. Been googling for a while and though I've learned quite a bit doing so, just haven't been able to find a fix for quite some time, so looking to the Stack Overflow community for some help. Thank you in advance!

Ray
  • 11
  • 3
  • See following : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.3 – jdweng May 14 '23 at 20:35

2 Answers2

1

You need to modify the prompt function after it has been modified by conda.exe, which you can do by placing the following after your & "C:\Users\myname\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook" | Out-String | Invoke-Expression call in your $PROFILE file (note that there's no need for % { ... }):

$function:prompt = @"
  Write-Host "`n" 
  $function:prompt
"@ 

This redefines the prompt function by emitting a newline ("`n") before calling the previous prompt function's body, using namespace variable notation.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I really appreciate the tip, thank you. I added your $function:prompt code after the conda.exe call (also, thank you for the note on removing the `% {...}`. While I get line breaks and the conda environment now, it appears I have lost the custom PS decoration i.e., `(base) PS C:\Users\myname --->`. It has gone back to the vanilla `(base) PS>` style, which appears to be the default conda style. I went through the link you provided but just couldn't get the custom PS command to work. Any ideas? Thank you again! – Ray May 14 '23 at 22:31
  • That is curious, @Ray. Did you retain your custom `prompt` function definition before calling the `conda` command? – mklement0 May 14 '23 at 22:53
  • I did! Code below: `function Prompt {Prior formatted PS} THEN DID & "C:\Users\myname\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook" | Out-String | Invoke-Expression THEN DID $function:prompt = @"{Your code}"@ ` and the output was `(base) PS>` – Ray May 14 '23 at 23:02
  • @Ray, honestly, I have no explanation. – mklement0 May 15 '23 at 01:26
0

Not the most elegant answer, but I found a way to do this by adding a Write-Host "" command within the Conda.psm1 profile as follows. Marking the inserted text within ****. Thank you for all the help!

    function global:prompt() {
    if ($Env:CONDA_PROMPT_MODIFIER) {
        *****Write-Host ""****
        $Env:CONDA_PROMPT_MODIFIER | Write-Host -NoNewline
    }
    CondaPromptBackup;
}
Ray
  • 11
  • 3