How can I customize powershell when running inside of the VSCode integrated terminal?

后端 未结 4 1628
南笙
南笙 2020-12-11 08:50

I would like to include a few Powershell customizations that only occur when running inside of VSCode.

When inside of ISE or Package Management Console (inside Vis

相关标签:
4条回答
  • 2020-12-11 09:11

    VSCode sets the TERM_PROGRAM environment variable to vscode in its integrated terminal, so you can use the following test, which works whether or not the PowerShell extension is installed:

    $runningInVsCode = $env:TERM_PROGRAM -eq 'vscode'
    
    0 讨论(0)
  • 2020-12-11 09:20

    You could check if VSCode-specific environment variables exist:

    if((Test-Path env:\VSCODE_PID) -or ($env:TERM_PROGRAM -eq 'vscode')){
      # Running from Vistual Studio Code
    }
    
    0 讨论(0)
  • 2020-12-11 09:30

    See also Visual Studio Code $psise equivalent

    You may check for $psISE, and if it does not exist, try $context = [Microsoft.Powershell.EditorServices.Extensions.EditorContext]$psEditor.GetEditorContext() but be aware, this will throw in ISE.

    I use this snippet in a lot of my scripts, so I can always reference "$root".

    # Makes debugging from ISE easier.
    if ($PSScriptRoot -eq "")
    {
        if ($psISE)
        {
            $root = Split-Path -Parent $psISE.CurrentFile.FullPath
        }
        else
        {
            $context = $psEditor.GetEditorContext()
            $root = Split-Path -Parent $context.CurrentFile.Path
        }
    }
    else
    {
        $root = $PSScriptRoot
    }
    
    0 讨论(0)
  • You can try to check you have VSCode context. The simplest way is to test current VSCode process ID.

    if ($Env:VSCODE_PID) {  }
    

    And the ISE has own profile (https://blogs.technet.microsoft.com/heyscriptingguy/2012/05/21/understanding-the-six-powershell-profiles/).

    0 讨论(0)
提交回复
热议问题