Sphinx Substitution Extensions

2026.1.12 · active · verified Thu Apr 16

Sphinx Substitution Extensions enhances Sphinx documentation by enabling variable substitutions within standard directives like `code-block`, `literalinclude`, and `image`, and introduces an inline `substitution-code` role. It supports both reStructuredText (reST) and MyST Markdown syntax for defining and using substitutions. The project maintains a frequent release cadence, often with multiple updates per month, ensuring continuous improvements and compatibility.

Common errors

Warnings

Install

Imports

Quickstart

To quickly integrate `sphinx-substitution-extensions`, add it to your `extensions` list in `conf.py`. Define your substitutions using `rst_prolog` for reStructuredText files or `myst_substitutions` for MyST Markdown. Then, use the `|KEY|` syntax in reST or `{{KEY}}` in MyST within directives that support substitutions (like `code-block` with the `:substitutions:` flag) or with the inline `:substitution-code:` role. Setting `substitutions_default_enabled = True` in `conf.py` applies substitutions to code blocks and literal includes without explicit flags.

# conf.py
extensions = [
    'sphinx.ext.autodoc',
    'sphinx_substitution_extensions',
]

# Define substitutions for reStructuredText
rst_prolog = '''
.. |project_name| replace:: My Awesome Project
.. |version| replace:: 1.0.0
'''

# Optional: Enable substitutions by default for applicable directives
substitutions_default_enabled = True

# .rst file (e.g., index.rst)
# .. code-block:: python
#    print("Welcome to |project_name| version |version|!")

# .rst file (e.g., index.rst) - manual substitution flag
# .. code-block:: bash
#    :substitutions:
#
#    echo "Hello from |project_name|"

# .rst file (e.g., index.rst) - inline substitution
# :substitution-code:`echo "Installed version: |version|"`

# For MyST Markdown (if myst-parser is installed)
# myst_enable_extensions = ["substitution"]
# myst_substitutions = {
#     "project_name": "My Awesome Project",
#     "version": "1.0.0",
# }
#
# In a .md file:
# ```{code-block} bash
# :substitutions:
# echo "Hello from {{project_name}}"
# ```
#
# In a .md file - inline substitution:
# {substitution-code}`echo "Installed version: {{version}}"`

view raw JSON →