template-dumper
is an Emacs package for creating new files from yasnippet templates.
- Easily specify file names by providing a function that returns a string.
- Use case: Create a file according to a specified naming convention in a particular directory.
- Maximum flexibility:
- Call
(save-buffer)
after(template-dumper-yas-new-file ...)
to create the file - Wrap your function in
save-window-excursion
if you don’t want to modify your current window configuration
- Call
Here is an example of how to use this package to write a self-contained function to create a file from a Yasnippet template.
(use-package template-dumper
:ensure nil)
;; Creates '/home/nate/myfile' from yas-snippet template 'elisp-default-file'
(template-dumper-yas-new-file "/home/nate/" "elisp-default-file" "myfile")
(defun create-dynamically-named-file ()
"Demo of dynamically named file creation"
(let* ((home (getenv "HOME"))
(matching (directory-files home t "template-dumper-test\-[0-9]+\.el"))
(basename (format "template-dumper-test-%s.el" (length matching)))
(fullpath (file-name-concat home basename)))
(save-window-excursion
(template-dumper-yas-new-file home "elisp-default-file" basename)
(save-buffer)
(message (format "Created %s" fullpath))
basename)))
;; Creates "$HOME/template-dumper-test-0.el" from yasnippet template "elisp-default-file"
(create-dynamically-named-file)
;; Creates "$HOME/template-dumper-test-1.el" from yasnippet template "elisp-default-file"
(create-dynamically-named-file)
;; Creates "$HOME/template-dumper-test-2.el" from yasnippet template "elisp-default-file"
(create-dynamically-named-file)
;; Creates "$HOME/template-dumper-test-3.el" from yasnippet template "elisp-default-file"
(create-dynamically-named-file)
template-dumper
enables specifying a directory of files that are each initialized from a named Yasnippet template.
This feature is subject to the following caveats:
- Missing parent directories are created as needed
- Files are not overwritten if they already exist
- May break if the yas template assigned to a file is interactive (i.e., the template uses
$1, $2, ... $n
) or other advanced Yasnippet features.
Example usage:
(setq nate/tpl-python '(dir "./"
((file "main.py" "python-default-file")
(file "README.org" "org-default-readme")
(file "LICENSE" "license-expat")
(file ".gitignore" "gitignore-default-file")
(cmd "chmod +x \"$DIR/main.py\"")
(cmd "git init \"$DIR\"")
(cmd "git add \"$DIR\"")
(cmd "git commit -m \"initial\""))))
(setq nate/tpl-html '(dir "./"
((file "index.html" "html-default-file-1")
(file "README.org" "org-default-readme")
(file "LICENSE" "license-expat")
(file ".gitignore" "gitignore-default-file"))))
(defun nate/tpl-callback-1 () (interactive) (template-dumper-mk-proj-tree-rel nate/tpl-python))
(defun nate/tpl-callback-2 () (interactive) (template-dumper-mk-proj-tree-rel nate/tpl-html))
(transient-define-prefix nate/project-template ()
"Create org-notepad file or open the most recent"
[["This is a transient:"
("1" "Python default project" nate/tpl-callback-1)
("2" "HTML default project" nate/tpl-callback-2)
("q" "exit" transient-noop)
]])
(define-key (current-global-map) (kbd "C-c <f2>") #'nate/project-template)