Powershell + Windows Terminal:在标题栏中显示路径并自定义提示

Ang*_*elo 7 powershell command-line windows-terminal

我正在运行 Windows 终端,这很棒。但有两个设置我想更改:

\n
    \n
  1. 我想将提示更改为更短的内容。默认情况下,提示符显示当前工作目录。它往往会变得很长并且让我的终端变得混乱。

    \n
  2. \n
  3. 当然,一目了然地了解当前工作目录仍然很有价值,因此我想将其放在标题栏中。

    \n
  4. \n
\n

我发现我可以在我的powershell 配置文件中创建一个“提示”功能。我犹豫是否将其更改为. 希望有更聪明、更易于使用的东西吗?我想避免仅仅为了修复我的提示而陷入 MS 文档的深渊。PS>

\n

查看settings.json(这里是架构),恰好有一个 TabTitle 设置,但它似乎只接受一个字符串。是否可以输入提供 pwd 的表达式? \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf

\n
"tabTitle": {\n  "description": "If set, will replace the name as the title to pass to the shell on startup. Some shells (like bash) may choose to ignore this initial value, while others (cmd, powershell) may use this value over the lifetime of the application.",\n  "type": [\n    "string",\n    "null"\n  ]\n},\n
Run Code Online (Sandbox Code Playgroud)\n

Not*_*1ds 4

几种选择:

  • 您可以缩写提示以仅显示短目录名称,而不是像此答案中那样显示完整路径。

  • 我知道某些 Linux shell 和提示喜欢使用每个路径元素的第一个字符进行缩写。((Get-Location).toString().split("\") -replace '^(.).*','$1' -join '\') + '> '这可以通过使用提示函数之类的东西来完成。

  • 您可能会考虑使用像Starship这样的提示实用程序,它可以使用配置而不是代码来进行许多提示自定义。

  • 更新 PowerShell/Windows 终端中的选项卡标题是通过设置完成的$Host.UI.RawUI.WindowTitle

把它们放在一起,你最终可能会得到类似的结果:

function prompt {
  $Host.UI.RawUI.WindowTitle = "$pwd"
  ((Get-Location).toString().split("\") -replace '^(.).*','$1' -join '\') + '> '
}
Run Code Online (Sandbox Code Playgroud)