Templates
Every segment has a template
property to tweak the text that is displayed.
Under the hood, this uses go's text/template feature extended with sprig and
offers a few standard properties to work with.
Global propertiesโ
These properties can be used anywhere, in any segment. If a segment contains a property with the same name,
the segment property value will be used instead. In case you want to use the global property, you can prefix
it with .$
to reference it directly.
Name | Type | Description |
---|---|---|
.Root | boolean | is the current user root/admin or not |
.PWD | string | the current working directory |
.Folder | string | the current working folder |
.Shell | string | the current shell name |
.ShellVersion | string | the current shell version |
.SHLVL | int | the current shell level |
.UserName | string | the current user name |
.HostName | string | the host name |
.Code | int | the last exit code |
.OS | string | the operating system |
.WSL | boolean | in WSL yes/no |
.Templates | string | the templates result |
.PromptCount | int | the prompt counter, increments with 1 for every prompt invocation |
Environment variablesโ
Name | Type | Description |
---|---|---|
.Env.VarName | string | Any environment variable where VarName is the environment variable name |
For the shells below, you can override a function to execute some logic before the prompt is rendered. This can be used to for example populate an environment variable with a specific context.
- powershell
- zsh
- bash
- fish
function Set-EnvVar {
$env:POSH=$(Get-Date)
}
New-Alias -Name 'Set-PoshContext' -Value 'Set-EnvVar' -Scope Global -Force
function set_poshcontext() {
export POSH=$(date)
}
function set_poshcontext() {
export POSH=$(date)
}
function set_poshcontext
set --export POSH $(date)
end
Config variablesโ
Name | Type | Description |
---|---|---|
.Var.VarName | any | Any config variable where VarName is the variable name |
Exampleโ
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"version": 2,
"var": {
"Hello": "hello",
"World": "world"
},
"blocks": [
{
"type": "prompt",
"alignment": "left",
"segments": [
{
"type": "text",
"style": "plain",
"foreground": "p:white",
"template": "{{ .Var.Hello }} {{ .Var.World }} "
}
]
}
]
}
Template logicโ
Template | Description |
---|---|
{{.}} | Root element. |
{{.Var}} | Variable in a struct, where Var is a variable. |
{{- .Var -}} | Remove extra white space surrounding the .Var variable and remove the newline. Either side is fine too. |
{{ $planet := "Earth"}} | {{ $planet }} Store a value in a custom variable to reference later. Note that .$ is used to reference the global/parent context, like in the full example below with $. |
Hi {{if .Name}} {{.Name}} {{else}} visitor {{end}} | If-else statement. If will will evaluate whether or not the argument is empty. Using the elseif conditional is also an option. The not negation is available too. |
{{if and .Arg1 .Arg2}} both complete. {{else}} incomplete. {{end}} | The and function compares multiple arguments to return the boolean AND (if arg1 then arg2 else arg1). Both arguments are evaluated. The or function compares multiple arguments to return the boolean OR. Similar to if arg1 then arg1 else arg2, so arg2 will never be evaluated if arg1 is false (not empty). |
{{with .Var}} {{end}} | With statement, where Var is a variable. It skips the block if the variable is absent. |
{{range .Array}} {{end}} | Range statement, where Array is an array, slice, map, or channel. |
{{ lt 3 4 }} | This lt comparison function evaluates to true since 3 is less than 4 (other boolean operators: eq, ne, lt, le, gt, ge). |
Helper functionsโ
Sprigโ
Oh My Posh has all sprig functions included, meaning you can do operations on strings, paths and a lot of other manipulations straight from your template. Have a look at their documentation for available options and how to use them.
Customโ
Template | Description |
---|---|
{{ url .UpstreamIcon .UpstreamURL }} | Create a hyperlink to a website to open your default browser (needs terminal support). |
{{ path .Path .Location }} | Create a link to a folder to open your file explorer (needs terminal support). |
{{ secondsRound 3600 }} | Round seconds to a time indication. In this case the output is 1h . |
{{ if glob "*.go" }}OK{{ else }}NOK{{ end }} | Exposes filepath.Glob as a boolean template function. |
{{ if matchP "*.Repo" .Path }}Repo{{ else }}No Repo{{ end }} | Exposes regexp.MatchString as a boolean template function. |
{{ replaceP "c.t" "cut code cat" "dog" }} | Exposes regexp.ReplaceAllString as a string template function. |
{{ .Code | hresult }} | Transform a status code to its HRESULT value for easy troubleshooting. For example -1978335212 becomes 0x8A150014 . |
Cross segment template propertiesโ
To use another segment's template properties in a template, you can make use of {{ .Segments.Segment }}
in your template where .Segment
is the name of the segment you want to use with the first letter uppercased.
If you want to for example use the git segment's .UpstreamGone
property in the status segment, you can
do so like this:
"template": " {{ if .Segments.Git.UpstreamGone }}\ueb05{{ else if gt .Code 0 }}\uf00d{{ else }}\uf00c{{ end }} "
For this to work, the segment you refer to needs to be in your config. The above example won't work if your config does not contain a git segment as Oh My Posh only populates the properties when it needs to.
If you have two identical segments for a different purpose, you can make use of the alias
property on the segment
to distinct between both. For example:
{
"type": "command",
"alias": "Hello",
"style": "plain",
"foreground": "#ffffff",
"properties": {
"command": "echo Hello"
}
},
{
"type": "command",
"alias": "World",
"style": "plain",
"foreground": "#ffffff",
"properties": {
"command": "echo World"
}
},
{
"type": "text",
"style": "plain",
"foreground": "#ffffff",
"template": "{{ .Segments.Hello.Output }} {{ .Segments.World.Output }}"
}
If you want to know if a specific segment is active, you can use the .Segments.Contains
function, for example:
"template": "{{ if .Segments.Contains \"Git\" }}\ueb05{{ else if gt .Code 0 }}\uf00d{{ else }}\uf00c{{ end }} "
Text decorationโ
You can make use of the following syntax to decorate text:
Syntax | Description |
---|---|
<b>bold</b> | bold as bold text |
<u>underline</u> | underline as underlined text |
<o>overline</o> | overline as overlined text |
<i>italic</i> | italic as italic text |
<s>strikethrough</s> | strikethrough as strikethrough text |
<d>dimmed</d> | dimmed as dimmed text |
<f>blink</f> | blink as blinking (flashing) text |
<r>reversed</r> | reversed as reversed text |
This can be used in templates and icons/text inside your config.