0

I'd like to insert

#+SETUPFILE: ~/.emacs.d/org-templates/level-0.org

to each .org file that I intend to publish.

I opened the variable auto-insert-alist but I cannot figure out where to paste my insert.

Can you help?

Can I add auto-insert-alist to my init file instead of customizing it in Quick Customization window?

Thanks. EDİT enter image description here

EDIT:

I found this code here

   (use-package autoinsert
      :init
      ;; Don't want to be prompted before insertion:
      (setq auto-insert-query nil)

      (setq auto-insert-directory (locate-user-emacs-file "templates"))
      (add-hook 'find-file-hook 'auto-insert)
      (auto-insert-mode 1)

      :config
      (define-auto-insert "\\.html?$" "default-html.html"))

This would work for me. But I didn't understand this line

      (setq auto-insert-directory (locate-user-emacs-file "templates"))

Do I need to create a templates file?

2
  • 1
    Yes, but... any particular reason you prefer not to use Customize? It seems to me that going by your problem description, Customize is exactly what you need, since it takes care of the enclosing syntax for you. Aug 27, 2022 at 10:23
  • ok. Where do I paste the #+SETUPFILE: ~/.emacs.d/org-templates/level-0.org in the function. It looks very complicated (my elisp is not that good)
    – zeynel
    Aug 27, 2022 at 10:34

2 Answers 2

1

You can bypass both customize and use-package[1]. Add something like this to your init file, changing the definition of auto-insert-directory to whatever you want and adding dotted pairs of (mode . "mode.template") for each mode that you are interested in; e.g.:


(setq auto-insert-directory "/path/to/templates/")
(setq auto-insert-alist '(
                          ;; templates for python and html mode
                          ;; are examples
                          (python-mode   . "python.template")
                          (html-mode     . "html.template")
                          (org-mode      . "org.template")

                          ;; more such pairs for other modes
                          ;; can be added here...
              ))

(add-hook 'find-file-hook 'auto-insert)

Then create the corresponding files in your templates directory. For Org mode, create org.template containing this:

#+SETUPFILE: ~/.emacs.d/org-templates/level-0.org

and whatever you want for the other modes. Restart emacs and open a new Org mode file.


[1] I would normally not bypass customize but it seems that the interface is too complicated in this case. As for use-package, IMO a prerequisite to its usage would be to understand it very well at a high level (and in some detail at the lower levels, at least to the extent of what its constructs would translate to), and be able to debug problems with it. I have a vague understanding of it at a high level, but I do not meet most of these prerequisites, so I don't use it.

2
  • 1
    Thanks. This worked for me. I added also (setq auto-insert-query nil) to eliminate the query.
    – zeynel
    Aug 28, 2022 at 7:38
  • What are the existing rules in the Customize? Are they examples? Some of them look like C and C++ templates. But others may be used by emacs. Is this true? I first deleted them and then I thought they are needed and reverted. For anyone trying to do this in Customize, I believe you go to the end and click the "Insert" button there to create a new rule.
    – zeynel
    Aug 28, 2022 at 7:44
2

It is unusually complicated for a customize interface, that's true. I'd go so far as to say the code that defines auto-insert-alist needs to be re-written to present a clearer interface making better use of Customize features.

Anyway... you need to enter org-mode into the first field (as "Major mode") and (nil "#+SETUPFILE: ~/.emacs.d/...")-- that is, a Lisp list of two elements, with nil as the first element -- into the second field (as "Skeleton or vector").

It's that second field interface that really needs to be re-written IMO. We should be able to just enter the text we want, with no enclosing quotes, much less quotes and an enclosing list.

1
  • Thanks. I filled the form as per your answer but when I create a new .org file I don't see the snippet. I added the screenshot in my question. What am I doing wrong. And do I need to delete the other stuff under my entry?
    – zeynel
    Aug 27, 2022 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.