在每个 PowerShell 提示符(包括 conda 环境名称)之前添加一个新行。

huangapple go评论56阅读模式
英文:

Add a new line before each Powershell prompt (including the conda environment name)

问题

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!

英文:

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!

答案1

得分: 1

你需要在被 conda.exe 修改后修改 prompt 函数,可以通过在你的 $PROFILE 文件中添加以下内容来实现(注意,不需要 % { ... }):

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

这重新定义了 prompt 函数,在调用前一个 prompt 函数的主体之前发出一个换行符(`n),使用命名空间变量表示法

英文:

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.

答案2

得分: 0

function global:prompt() {
    if ($Env:CONDA_PROMPT_MODIFIER) {
        *****Write-Host ""****
        $Env:CONDA_PROMPT_MODIFIER | Write-Host -NoNewline
    }
    CondaPromptBackup;
}
英文:

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;
}

huangapple
  • 本文由 发表于 2023年5月15日 02:04:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定