2021-11-12 10:14:16 -08:00
---
2022-04-20 09:43:59 -07:00
id: templates
2022-01-28 04:42:34 -08:00
title: Templates
sidebar_label: Templates
2021-11-12 10:14:16 -08:00
---
2022-09-23 11:44:53 -07:00
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";
2024-11-02 13:46:16 -07:00
import Config from "@site/src/components/Config.js";
2022-09-23 11:44:53 -07:00
2022-01-22 10:46:56 -08:00
Every segment has a `template` property to tweak the text that is displayed.
Under the hood, this uses go's [text/template][go-text-template] feature extended with [sprig][sprig] and
offers a few standard properties to work with.
2022-01-28 04:42:34 -08:00
## Global properties
2023-04-22 03:10:12 -07:00
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.
2023-01-22 11:06:31 -08:00
| Name | Type | Description |
| --------------- | --------- | ----------------------------------------------------------------- |
| `.Root` | `boolean` | is the current user root/admin or not |
2024-06-13 05:58:13 -07:00
| `.PWD` | `string` | the current working directory (`~` for `$HOME`) |
| `.AbsolutePWD` | `string` | the current working directory (unaltered) |
2024-08-06 02:54:56 -07:00
| `.PSWD ` | `string` | the current non-filesystem working directory in PowerShell |
2023-01-22 11:06:31 -08:00
| `.Folder` | `string` | the current working folder |
| `.Shell` | `string` | the current shell name |
| `.ShellVersion` | `string` | the current shell version |
2023-04-02 10:10:34 -07:00
| `.SHLVL` | `int` | the current shell level |
2023-01-22 11:06:31 -08:00
| `.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][templates] result |
| `.PromptCount` | `int` | the prompt counter, increments with 1 for every prompt invocation |
2022-04-15 11:49:27 -07:00
## Environment variables
2022-09-16 07:32:48 -07:00
| Name | Type | Description |
| -------------- | -------- | ------------------------------------------------------------------------- |
| `.Env.VarName` | `string` | Any environment variable where `VarName` is the environment variable name |
2022-01-22 10:46:56 -08:00
2022-04-15 11:49:27 -07:00
:::tip
2022-09-23 11:44:53 -07:00
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.
<Tabs
defaultValue="powershell"
groupId="shell"
values={[
{ label: 'powershell', value: 'powershell', },
{ label: 'zsh', value: 'zsh', },
{ label: 'bash', value: 'bash', },
{ label: 'fish', value: 'fish', },
2024-06-18 03:58:15 -07:00
{ label: 'nu', value: 'nu', },
2022-09-23 11:44:53 -07:00
]
}>
<TabItem value="powershell">
2022-04-15 11:49:27 -07:00
```powershell
2024-07-27 07:13:49 -07:00
function Set-EnvVar([bool]$originalStatus) {
2022-04-15 11:49:27 -07:00
$env:POSH=$(Get-Date)
}
New-Alias -Name 'Set-PoshContext' -Value 'Set-EnvVar' -Scope Global -Force
```
2022-09-23 11:44:53 -07:00
</TabItem>
<TabItem value="zsh">
```bash
function set_poshcontext() {
export POSH=$(date)
}
```
</TabItem>
<TabItem value="bash">
```bash
function set_poshcontext() {
export POSH=$(date)
}
```
</TabItem>
<TabItem value="fish">
```shell
function set_poshcontext
set --export POSH $(date)
end
```
2024-06-16 01:25:18 -07:00
</TabItem>
<TabItem value="nu">
```bash
$env.SET_POSHCONTEXT = {
$env.POSH = ( date now );
}
```
2022-09-23 11:44:53 -07:00
</TabItem>
</Tabs>
2022-04-15 11:49:27 -07:00
:::
2023-02-19 11:21:33 -08:00
## Config variables
| Name | Type | Description |
| -------------- | ----- | -------------------------------------------------------- |
2023-08-25 02:04:14 -07:00
| `.Var.VarName` | `any` | Any config variable where `VarName` is the variable name |
2023-02-19 11:21:33 -08:00
### Example
2024-11-02 13:46:16 -07:00
<Config
data={{
2024-11-08 07:24:28 -08:00
version: 3,
2024-11-02 13:46:16 -07:00
// highlight-start
var: {
Hello: "hello",
World: "world",
},
// highlight-end
blocks: [
{
type: "prompt",
alignment: "left",
segments: [
{
type: "text",
style: "plain",
foreground: "p:white",
// highlight-next-line
template: "{{ .Var.Hello }} {{ .Var.World }} ",
},
],
},
],
}}
/>
2023-02-19 11:21:33 -08:00
2022-05-18 22:58:23 -07:00
## Template logic
2024-06-12 00:21:14 -07:00
<!-- markdownlint-disable MD013 -->
2022-07-08 02:47:08 -07:00
2022-05-18 22:58:23 -07:00
| 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 $. |
2024-06-13 05:58:13 -07:00
| `Hi {{if .Name}} {{.Name}} {{else}} visitor {{end}}` | If-else statement. If will evaluate whether or not the argument is empty. Using the elseif conditional is also an option. The not negation is available too. |
2022-05-18 22:58:23 -07:00
| `{{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). |
2022-07-08 02:47:08 -07:00
2022-05-18 22:58:23 -07:00
<!-- markdownlint-enable MD013 -->
2022-01-28 04:42:34 -08:00
## Helper functions
2022-05-18 22:58:23 -07:00
### Sprig
Oh My Posh has all [sprig][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][sprig] for available options and how to
use them.
### Custom
<!-- markdownlint-disable MD013 -->
2022-07-08 02:47:08 -07:00
2024-08-04 20:59:41 -07:00
| Template | Description |
| ------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------- |
| `{{ url .UpstreamIcon .UpstreamURL }}` | Create an `OSC8` hyperlink to a website to open your default browser (needs terminal [support][terminal-list-hyperlinks]). |
| `{{ path .Path .Location }}` | Create an `OSC8` file link to a folder to open your file explorer (needs terminal [support][terminal-list-hyperlinks]). |
| `{{ 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][glob] as a boolean template function. |
| `{{ if matchP ".*\\.Repo$" .Path }}Repo{{ else }}No Repo{{ end }}` | Exposes [regexp.MatchString][regexpms] as a boolean template function. |
| `{{ replaceP "c.t" "cut code cat" "dog" }}` | Exposes [regexp.ReplaceAllString][regexpra] as a string template function. |
| <code>\{\{ .Code | hresult \}\}</code> | Transform a status code to its HRESULT value for easy troubleshooting. For example `-1978335212` becomes `0x8A150014`. |
| `{{ readFile ".version.json" }}` | Read a file in the current directory. Returns a string. |
2024-09-19 23:46:37 -07:00
| `{{ random (list \"a\" 2 .MyThirdItem) }}` | Selects a random element from a list. The list can be an array or slice containing any types (use sprig's `list`). |
2022-07-08 02:47:08 -07:00
2022-05-18 22:58:23 -07:00
<!-- markdownlint-enable MD013 -->
2022-01-28 04:42:34 -08:00
2022-05-18 22:30:20 -07:00
## 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.
2023-08-26 22:26:08 -07:00
If you want to for example use the [git][git] segment's `.UpstreamGone` property in the [status][status] segment, you can
2022-05-18 22:30:20 -07:00
do so like this:
2024-11-02 13:46:16 -07:00
<Config
data={{
template:
" {{ if .Segments.Git.UpstreamGone }}\ueb05{{ else if gt .Code 0 }}\uf00d{{ else }}\uf00c{{ end }} ",
}}
/>
2022-05-18 22:30:20 -07:00
2022-08-09 23:42:18 -07:00
:::caution
2022-05-18 22:30:20 -07:00
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.
:::
2022-09-20 11:09:38 -07:00
:::tip
If you have two identical segments for a different purpose, you can make use of the `alias` property on the segment
2024-11-02 13:46:16 -07:00
to distinct between both.
2022-09-20 11:09:38 -07:00
:::
2024-11-02 13:46:16 -07:00
<Config
data={{
segments: [
{
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 }}",
},
],
}}
/>
2022-11-20 04:59:32 -08:00
If you want to know if a specific segment is active, you can use the `.Segments.Contains` function, for example:
2024-11-02 13:46:16 -07:00
<Config
data={{
template:
'{{ if .Segments.Contains "Git" }}\ueb05{{ else if gt .Code 0 }}\uf00d{{ else }}\uf00c{{ end }} ',
}}
/>
2022-11-20 04:59:32 -08:00
2022-01-22 10:46:56 -08:00
## Text decoration
2021-11-12 10:14:16 -08:00
You can make use of the following syntax to decorate text:
2022-09-16 07:32:48 -07:00
| Syntax | Description |
| ---------------------- | ------------------------------------- |
2022-07-08 02:47:08 -07:00
| `<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 |
2021-11-12 10:14:16 -08:00
This can be used in templates and icons/text inside your config.
[terminal-list-hyperlinks]: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
[path-segment]: /docs/path
[git-segment]: /docs/git
2023-03-14 03:40:43 -07:00
[go-text-template]: https://pkg.go.dev/text/template
2022-01-22 10:46:56 -08:00
[sprig]: https://masterminds.github.io/sprig/
2022-02-28 23:10:23 -08:00
[glob]: https://pkg.go.dev/path/filepath#Glob
2024-07-05 03:21:58 -07:00
[git]: /docs/segments/scm/git
[status]: /docs/segments/system/status
2024-06-12 00:21:14 -07:00
[templates]: /docs/configuration/segment
2023-01-26 06:19:37 -08:00
[regexpms]: https://pkg.go.dev/regexp#Regexp.MatchString
[regexpra]: https://pkg.go.dev/regexp#Regexp.ReplaceAllString