![如何在 Mac 上的 VSCode 中自定义 PowerShell 集成控制台提示?](https://linux22.com/image/1584917/%E5%A6%82%E4%BD%95%E5%9C%A8%20Mac%20%E4%B8%8A%E7%9A%84%20VSCode%20%E4%B8%AD%E8%87%AA%E5%AE%9A%E4%B9%89%20PowerShell%20%E9%9B%86%E6%88%90%E6%8E%A7%E5%88%B6%E5%8F%B0%E6%8F%90%E7%A4%BA%EF%BC%9F.png)
在 Microsoft 的 VSCode 中,我想像使用 .bash_profile 一样自定义集成的 PowerShell 提示。
看来你可以根据自己的喜好进行编辑这文档,但我不确定将我的 .ps1 文件保存在哪里function prompt { }
,也不确定如何让 VSCode 在打开新控制台时调用它。
似乎每个会话都会在 中设置一个临时配置文件Users/<username>/.config/powershell/Microsoft.VSCode_profile.ps1
,但在我的 ~/.config 目录上执行 时ls
,根本没有显示任何 powershell 目录。如果我创建目录,VSCode 会在我打开控制台时读取它吗?
有谁成功过吗?
输出自Get-Host
:
Name : Visual Studio Code Host
Version : 1.11.0
InstanceId : 8d0a98e7-12e1-41b1-b27e-02879107cf00
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-US
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.EditorServices.EditorServicesPSHost+ConsoleColorProxy
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
答案1
做了一些实验。是的,~/.config/powershell
如果不存在则创建,并将您的配置文件保存在名为确切地 Microsoft.VSCode_profile.ps1
例子:
function prompt {
"$(Get-Date) $(Resolve-Path -Relative -Path $(Get-Location))> "
}
<you>
...如果您的路径是 /Users/ /Documents/powershellscripts/,则会返回类似这样的内容
2019 年 3 月 11 日 20:11:32 ../powershellscripts>
答案2
VSCode 文档、本网站和整个网络均对此进行了完整记录。
使用“vscode 自定义提示”进行简单搜索就会提供此用例的链接。
这只是您添加到配置文件中的一项功能。我喜欢尽可能保持简单,所以,这是我在 Windows 上的配置。这是一个空白的提示行,路径显示在 GUI 标题栏中,我将其用于我的所有配置文件(PowerShell、ISE、VSCode)。
Function Prompt
{
# get the last command from history
$command = Get-History -Count 1
# set it to the window title
if ($command)
{
$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = new-object System.Security.principal.windowsprincipal($CurrentUser)
if ($principal.IsInRole("Administrators")) { $Role = 'Administrator: ' }
Else { $Role = 'User: ' }
$host.ui.rawui.WindowTitle = $Role + $PWD
}
# specify your custom prompt, e.g. the default PowerShell:
" "
}
因此,上述方法有效,但在 VSCode 中,它不会更改标题栏,因为它与您打开的文件相关联。但是,您只是在查看提示。所以,这种方法应该可以帮到你。
这已经在 stackexchange 上进行了介绍。
如何在 macOS 上的 VS Code 终端中自定义 shell 提示符
另外,请参阅 VSCode 文档以获取有关终端设置/定制的更多信息。
在 Visual Studio Code 中,您可以打开一个集成终端,最初从工作区的根目录开始。这很方便,因为您无需切换窗口或更改现有终端的状态即可执行快速命令行任务。