Template Directives
File-specific template options can be set using template directives. Directives are special comments in templates that configure how the template is processed.Directive Syntax
Directives use the format:$KEY is the option name and $VALUE is its value. $VALUE must be quoted if it contains spaces or double quotes.
Multiple Directives
Multiple key/value pairs may be specified on a single line:Removing Directives
Lines containing template directives are automatically removed to avoid parse errors from any delimiters. This means directives don’t appear in the final output.Delimiters
By default, chezmoi uses the standardtext/template delimiters {{ and }}. These can be changed per-file using the delimiter directive.
Syntax
left-delimiter=$LEFT and right-delimiter=$RIGHT may be omitted. If either $LEFT or $RIGHT is empty, the default delimiter ({{ and }} respectively) is used instead.
Delimiters are specific to the file in which they appear and are not inherited by templates called from the file.
Examples
Shell Script with Custom Delimiters
script.sh.tmpl
Docker Compose with Alternative Delimiters
docker-compose.yml.tmpl
Only Change Left Delimiter
Encoding
Templates are always written in UTF-8 with no byte order mark. The output encoding can be transformed using the encoding directive.Syntax
Supported Encodings
| Encoding | Description |
|---|---|
utf-8 | UTF-8 with no byte order mark (default) |
utf-8-bom | UTF-8 with a byte order mark |
utf-16-be | Big-endian UTF-16 with no byte order mark |
utf-16-be-bom | Big-endian UTF-16 with a byte order mark |
utf-16-le | Little-endian UTF-16 with no byte order mark |
utf-16-le-bom | Little-endian UTF-16 with a byte order mark |
Examples
UTF-16 Little-Endian Output
config.ini.tmpl
UTF-8 with BOM
file.txt.tmpl
Format Indent
By default, chezmoi’stoJson, toToml, and toYaml template functions use an indent of two spaces. This can be overridden per-file.
Syntax
Literal String Indent
$STRING.
Width-Based Indent
$WIDTH spaces.
Examples
Tab Indentation
config.json.tmpl
Four-Space Indentation
config.yaml.tmpl
Line Endings
Many template functions use UNIX-style line endings (lf/\n), which may cause unexpected output on Windows or when running chezmoi diff on modify_ templates. Line endings can be overridden with a directive.
Syntax
Values
| Value | Effect |
|---|---|
lf | Use UNIX-style line endings (\n) |
crlf | Use Windows line endings (\r\n) |
native | Use platform-native line endings (crlf on Windows, lf elsewhere) |
| Custom string | Use the literal string as line ending |
Examples
Windows Line Endings
script.bat.tmpl
Platform-Native Line Endings
file.txt.tmpl
Force LF on Windows
script.sh.tmpl
Missing Keys
By default, chezmoi returns an error if a template indexes a map with a key that is not present. This behavior can be changed globally with thetemplate.options configuration variable or per-file with a directive.
Syntax
Values
| Value | Effect |
|---|---|
error | Return an error on any missing key (default) |
invalid | Ignore missing keys. If printed, the result is <no value> |
zero | Ignore missing keys. If printed, the result is the zero value |
Examples
Ignore Missing Keys with Zero Values
config.tmpl
.optional_field doesn’t exist, it outputs an empty string instead of erroring.
Show “no value” for Missing Keys
debug.tmpl
.optional is missing:
Combining Directives
Multiple directives can be used in the same file:script.ps1.tmpl
Directive Placement
Directives can appear anywhere in the file but should typically be placed:- At the top of the file for visibility
- Inside comments appropriate for the file type
- Before the content they affect
Examples
Shell Script
Go Template Comment
YAML Comment
HTML Comment
Practical Examples
PowerShell Script
dot_profile.ps1.tmpl
JSON Configuration with Tabs
dot_config_app_settings.json.tmpl
Windows Batch File
setup.bat.tmpl
Modify Template with Matching Line Endings
modify_dot_bashrc.tmpl
Global vs. File-Specific Options
Global Configuration
~/.config/chezmoi/chezmoi.toml
File-Specific Override
specific-file.tmpl
Debugging Directives
Test directive behavior:Related Pages
- Template Overview - Template system basics
- Template Functions - Available functions
- Template Variables - Built-in variables
- Configuration File - Global template options