oh-my-posh/docs/docs/segment-environment.md
2020-10-16 11:18:19 -07:00

1.4 KiB

id title sidebar_label
environment Environment Variable Environment Variable

What

Show the content of an environment variable. Can be used to visualize a local settings/context unavailable to Go my Posh otherwise.

For example, in PowerShell, adding the below configuration to a block and extending the prompt function to set an environment variable before the prompt, you can work a bit of magic.

[ScriptBlock]$Prompt = {
  $realLASTEXITCODE = $global:LASTEXITCODE
  $env:POSH = "hello from Powershell"
  & "C:\tools\oh-my-posh.exe" -config "~/downloadedtheme.json" -error $realLASTEXITCODE -pwd $PWD
  $global:LASTEXITCODE = $realLASTEXITCODE
  Remove-Variable realLASTEXITCODE -Confirm:$false
}

If you're using the PowerShell module, you can override a function to achieve the same effect. make sure to do this after importing go-my-posh and you're good to go.

function Set-EnvVar {
    $env:POSH=$(Get-Date)
}
New-Alias -Name 'Set-PoshContext' -Value 'Set-EnvVar' -Scope Global

The segment will show when the value of the environment variable isn't empty.

Sample Configuration

{
  "type": "envvar",
  "style": "powerline",
  "powerline_symbol": "\uE0B0",
  "foreground": "#ffffff",
  "background": "#0077c2",
  "properties": {
    "var_name": "POSH"
  }
}
  • var_name: string - the name of the environment variable