Skip to main content

Template Functions

All standard text/template and text template functions from Sprig are included. chezmoi provides many additional functions.

General Functions

abortEmpty

Returns empty, causing the target file to be removed. Useful for conditionally creating files.
{{ if .condition }}
file content
{{ else }}
{{ abortEmpty }}
{{ end }}

comment prefix string

Adds prefix to the beginning of each line in string.
{{ "Line 1\nLine 2" | comment "# " }}
# Line 1
# Line 2

completion shell

Returns shell completion script for the specified shell (bash, fish, powershell, zsh).
{{ completion "bash" }}

decrypt ciphertext

Decrypts ciphertext using the configured encryption method.
{{ decrypt "encrypted-data" }}

encrypt plaintext

Encrypts plaintext using the configured encryption method.
{{ "secret" | encrypt }}

ensureLinePrefix prefix [addPrefix] string

Ensures each line in string has prefix. If addPrefix is specified, it’s added to lines missing prefix.
{{ "line1\n# line2" | ensureLinePrefix "# " }}
# line1
# line2

eqFold first second [more…]

Case-insensitive equality comparison.
{{ if eqFold .chezmoi.os "LINUX" }}Linux detected{{ end }}

exec command [args…]

Executes command with args and returns true if successful, false otherwise.
{{ if exec "which" "docker" }}
Docker is installed
{{ end }}

File Functions

include filename

Returns the literal contents of filename. Relative paths are interpreted relative to the source directory.
{{ include "scripts/setup.sh" }}

includeTemplate filename [data]

Executes the template in filename with optional data and returns the result.
{{ includeTemplate "config.tmpl" . }}
{{ includeTemplate "header.tmpl" }}

stat name

Returns file information for name (name, size, mode, perm, modTime, isDir, type). Returns false if file doesn’t exist.
{{ if stat "/path/to/file" }}
File exists
{{ end }}

{{ $info := stat "/path/to/file" }}
{{ $info.size }}

lstat name

Like stat, but doesn’t follow symbolic links.
{{ $info := lstat "/path/to/symlink" }}
{{ $info.type }}

glob pattern

Returns list of files matching pattern in the destination directory.
{{ range glob "*.conf" }}
Found: {{ . }}
{{ end }}

Execution Functions

output command [args…]

Returns the output of executing command with args. Exits with error if command fails.
current-context: {{ output "kubectl" "config" "current-context" | trim }}

outputList command argsList

Like output, but takes arguments as a list.
{{ $args := list "config" "current-context" }}
{{ outputList "kubectl" $args }}

lookPath file

Returns the full path to file if found in PATH, empty string otherwise.
{{ if lookPath "docker" }}
Docker path: {{ lookPath "docker" }}
{{ end }}

findExecutable file pathList

Finds file in pathList and returns the full path.
{{ findExecutable "python" (list "/usr/bin" "/usr/local/bin") }}

findOneExecutable fileList pathList

Finds the first executable from fileList in pathList.
{{ findOneExecutable (list "python3" "python") (splitList ":" (env "PATH")) }}

isExecutable file

Returns true if file has executable permissions.
{{ if isExecutable "/usr/bin/bash" }}Executable{{ end }}

Path Functions

joinPath elements

Joins path elements using the OS-specific path separator.
{{ joinPath .chezmoi.homeDir ".config" "app" }}

Data Conversion Functions

fromJson string

Parses string as JSON and returns the result.
{{ $data := fromJson .jsonString }}
{{ $data.key }}

fromJsonc string

Parses string as JSONC (JSON with comments).
{{ $data := fromJsonc .jsoncString }}

fromToml string

Parses string as TOML.
{{ $data := fromToml .tomlString }}

fromYaml string

Parses string as YAML.
{{ $data := fromYaml .yamlString }}

fromIni string

Parses string as INI format.
{{ $data := fromIni .iniString }}

toJson data

Converts data to JSON format.
{{ dict "key" "value" | toJson }}

toPrettyJson [indent] data

Converts data to pretty-printed JSON.
{{ dict "key" "value" | toPrettyJson }}
{{ dict "key" "value" | toPrettyJson "    " }}

toToml data

Converts data to TOML format.
{{ dict "key" "value" | toToml }}

toYaml data

Converts data to YAML format.
{{ dict "key" "value" | toYaml }}

toIni data

Converts data to INI format.
{{ dict "section" (dict "key" "value") | toIni }}

String Functions

hexDecode string

Decodes hexadecimal string to bytes.
{{ hexDecode "48656c6c6f" }}

hexEncode string

Encodes string to hexadecimal.
{{ "Hello" | hexEncode }}

quote values

Double-quotes each value and joins with spaces.
{{ quote "arg1" "arg2" }}
# "arg1" "arg2"

squote values

Single-quotes each value and joins with spaces.
{{ squote "arg1" "arg2" }}
# 'arg1' 'arg2'

quoteList list

Double-quotes each element in list.
{{ list "a" "b" | quoteList }}

replaceAllRegex pattern replacement string

Replaces all matches of regex pattern with replacement in string.
{{ replaceAllRegex "[0-9]+" "X" "abc123def456" }}
# abcXdefX

splitList separator string

Splits string by separator and returns a list.
{{ splitList "," "a,b,c" }}

toString value

Converts value to a string.
{{ 123 | toString }}

toStrings values

Converts multiple values to strings.
{{ toStrings 1 2 3 }}

Data Manipulation Functions

deleteValueAtPath path dict

Deletes the value at path in dict.
{{ $data := dict "a" (dict "b" "c") }}
{{ deleteValueAtPath "a.b" $data }}

setValueAtPath path value dict

Sets value at path in dict.
{{ $data := dict }}
{{ setValueAtPath "a.b.c" "value" $data }}

pruneEmptyDicts dict

Removes all empty dictionaries from dict.
{{ dict "empty" (dict) "full" (dict "key" "value") | pruneEmptyDicts }}

jq query input

Executes jq query on input and returns results.
{{ $data := dict "items" (list 1 2 3) }}
{{ jq ".items[] | . * 2" $data }}

Special Functions

mozillaInstallHash path

Returns Mozilla installation hash for path.
{{ mozillaInstallHash "/usr/lib/firefox" }}

ioreg

Returns macOS IORegistry information (macOS only).
{{ if eq .chezmoi.os "darwin" }}
{{ $ioreg := ioreg }}
{{ end }}

warnf format args

Prints a warning message. Returns empty string.
{{ if not (stat "/path/to/file") }}
{{ warnf "File %s does not exist" "/path/to/file" }}
{{ end }}

getRedirectedURL url

Follows redirects and returns the final URL.
{{ getRedirectedURL "https://short.url" }}

GitHub Functions

These functions integrate with GitHub’s API:
  • gitHubKeys user - Returns user’s public SSH keys
  • gitHubLatestRelease repo - Returns latest release info
  • gitHubLatestReleaseAssetURL repo pattern - Returns asset download URL
  • gitHubLatestTag repo - Returns latest tag name
  • gitHubRelease repo tag - Returns release info for tag
  • gitHubReleaseAssetURL repo tag pattern - Returns asset URL for release
  • gitHubReleases repo - Returns all releases
  • gitHubTags repo - Returns all tags

Example

{{ $release := gitHubLatestRelease "twpayne/chezmoi" }}
Version: {{ $release.tag_name }}

{{ range gitHubKeys "username" }}
ssh-key: {{ . }}
{{ end }}

Password Manager Functions

See Password Manager Functions for:
  • 1Password (onepassword, onepasswordDocument, etc.)
  • Bitwarden (bitwarden, bitwardenFields, etc.)
  • LastPass (lastpass, lastpassRaw)
  • Pass (pass, passFields, passRaw)
  • Keeper (keeper, keeperDataFields)
  • KeePassXC (keepassxc, keepassxcAttribute)
  • And many more

Build docs developers (and LLMs) love