Generate template files and directories from a single org document.
org-generate.el provides a simple and powerful way to define and generate file/folder templates using Org mode. Define your templates once in an org file, then generate them anywhere with a single command.
Key features:
- Define templates using familiar Org mode syntax
- Support for single files, multiple files, or entire project structures
- Variable substitution using Mustache templates
- Interactive template selection with completion
- Emacs 26.1 or later
- Org mode 9.3 or later
- mustache.el 0.23 or later
(use-package org-generate
:ensure t)Or with leaf:
(leaf org-generate :ensure t)- Create a template file at
~/.emacs.d/org-generate.org(or customizeorg-generate-file) - Define your templates using org headings
- Run
M-x org-generateand select a template
Templates are organized in a two-level hierarchy:
- First level: Category (e.g.,
hugo,project) - Second level: Template name (e.g.,
page,single) - Third level and beyond: File and directory definitions
* hugo
** page
:PROPERTIES:
:org-generate-root: ~/dev/repos/hugo-src/content/blog/
:END:
*** page.md
#+begin_src markdown
---
title: "New Post"
date: 2024-01-01
draft: true
---
Content goes here.
#+end_src
Run M-x org-generate and select hugo/page to create the file.
Use Mustache syntax for dynamic content. Declare variables in the org-generate-variable property:
* hugo
** single
:PROPERTIES:
:org-generate-root: ~/dev/repos/hugo-src/content/blog/
:org-generate-variable: title date
:END:
*** {{title}}.md
#+begin_src markdown
---
title: {{title}}
date: {{date}}
draft: true
---
Content goes here.
#+end_src
When you generate this template, you’ll be prompted for title and date values.
Create entire project structures with a single template:
* project
** elisp
:PROPERTIES:
:org-generate-root: ~/dev/repos/
:org-generate-variable: pkg-name description
:END:
*** {{pkg-name}}.el/
**** .github/
***** workflows/
****** test.yml
#+begin_src yaml
name: Main workflow
on: [push, pull_request]
#+end_src
**** .gitignore
#+begin_src gitignore
*-autoloads.el
*.elc
/.keg
#+end_src
**** README.org
#+begin_src org
,,* Description
{{description}}
#+end_src
**** {{pkg-name}}.el
#+begin_src emacs-lisp
;;; {{pkg-name}}.el --- {{description}} -*- lexical-binding: t; -*-
;;; Code:
(provide '{{pkg-name}})
;;; {{pkg-name}}.el ends here
#+end_src
Note: Directory names must end with / to be recognized as directories.
org-generate- Generate files from a selected templateorg-generate-edit- Open the template file for editing
| Variable | Description | Default |
|---|---|---|
org-generate-file | Path to the template definition file | ~/.emacs.d/org-generate.org |
org-generate-with-export-as-org | Export as org before generating | t |
When org-generate-with-export-as-org is enabled, you can use additional Org features like file includes, macros, and noweb references in your templates.
Contributions are welcome. Please feel free to submit issues and pull requests.
Install keg for testing:
cd ~/
git clone https://github.com/conao3/keg .keg
export PATH="$HOME/.keg/bin:$PATH"make testGPL-3.0. See LICENSE for details.
Naoya Yamashita (conao3)
