Skip to main content

Introduction

Templates allow you to change file contents based on the environment. For example, use different email addresses or API keys on different machines. chezmoi uses Go’s text/template syntax extended with Sprig functions. A file is interpreted as a template if:
  • The filename has a .tmpl suffix
  • The file is in .chezmoitemplates directory or its subdirectories

Template data

chezmoi data
Data sources (later sources override earlier ones):
  1. Built-in variables in .chezmoi (e.g., .chezmoi.os, .chezmoi.hostname)
  2. .chezmoidata.$FORMAT files (read in alphabetical order)
  3. data section in your config file
chezmoi also provides functions to retrieve runtime data from password managers, environment variables, and the file system.

Creating a template file

chezmoi add --template ~/.zshrc
Creates ~/.local/share/chezmoi/dot_zshrc.tmpl.

Editing a template file

chezmoi edit ~/.zshrc
The edit command opens the source file and checks template syntax when you quit the editor.

Testing templates

chezmoi execute-template '{{ .chezmoi.hostname }}'
Output: myhost

Template syntax

Template actions are written inside {{ }}. Text outside is copied literally.
{{ .chezmoi.hostname }}
{{ .email }}

Simple logic

# common config
export EDITOR=vi

# machine-specific configuration
{{- if eq .chezmoi.hostname "work-laptop" }}
# this will only be included in ~/.bashrc on work-laptop
export WORK_VAR=value
{{- end }}

Boolean functions

FunctionDescription
eqReturns true if first argument equals any other argument
notReturns boolean negation
andReturns boolean AND (returns first empty arg or last arg)
orReturns boolean OR (returns first non-empty arg or last arg)

Integer comparison functions

FunctionDescription
lenReturns integer length
eqarg1 == arg2
nearg1 != arg2
ltarg1 < arg2
learg1 <= arg2
gtarg1 > arg2
gearg1 >= arg2

More complicated logic

{{ if eq "foo" "foo" "bar" }}hello{{end}}
Checks if first arg equals any of the other args.

Helper functions

chezmoi includes:
chezmoi data

Using .chezmoitemplates

Files in .chezmoitemplates can be included in other templates using the template action:
1

Create shared template

.chezmoitemplates/part.tmpl
{{ if eq .chezmoi.os "linux" }}
# linux config
{{ else }}
# non-linux config
{{ end }}
2

Use in another template

dot_file.tmpl
{{ template "part.tmpl" . }}
Pass . to make template variables available. Without it, the template receives nil data.

Creating similar files with shared templates

1

Create shared template

.chezmoitemplates/alacritty
some: config
fontsize: {{ . }}
more: config
Files in .chezmoitemplates don’t need .tmpl extension.
2

Create files using the template

{{- template "alacritty" 12 -}}
3

Test the output

chezmoi cat ~/small-font.yml
Output:
some: config
fontsize: 12
more: config

Passing multiple arguments

~/.config/chezmoi/chezmoi.toml
[data.alacritty.big]
    fontsize = 18
    font = "DejaVu Serif"
[data.alacritty.small]
    fontsize = 12
    font = "DejaVu Sans Mono"
Template:
.chezmoitemplates/alacritty
some: config
fontsize: {{ .fontsize }}
font: {{ .font }}
more: config
Usage:
small-font.yml.tmpl
{{- template "alacritty" .alacritty.small -}}

Build docs developers (and LLMs) love