0

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

I have the above line auto-inserted on new org files. Is there a way to make the cursor move to the next line? Now it stays at the beginning of the line, on the #.


(setq auto-insert-directory "~/template/")
(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...
                         ))
2
  • How did you create your template ? Do you use a skeleton ? Commented Sep 8, 2023 at 16:08
  • @TristanRiehs I don't know what skeleton is but I copied the code from my .emacs file. The inserted line is in /template/org.template file.
    – zeynel
    Commented Sep 8, 2023 at 21:21

1 Answer 1

1

The built-in feature auto-insert provides three methods for automatic insertion of file templates. You chose the first one which is easy to use but does not provide a fine-grained control on the point location after the insertion.

The second method, which consists in defining skeletons (EmacsWiki, Emacs Manual), does.

(define-skeleton org-skeleton
  "Skeleton for org files."
  ;; no prompt string
  nil
  ;; content
  "#+SETUPFILE: ~/.emacs.d/org-templates/level-2.org\n")

(setq auto-insert-alist '((org-mode . org-skeleton)))
(auto-insert-mode)

Since your template only consists of one line of content, the skeleton is fairly simple. Also, specifying the point location is not needed, I tested this skeleton and the point ends up after the "SETUPFILE" line. This first skeleton does what you want and is the simplest. However, you can improve it using embedded emacs-lisp.

(define-skeleton org-skeleton
  "Skeleton for org files."
  nil
  "#+SETUPFILE: " user-emacs-directory "org-templates/level-2.org\n")

This second example inserts the exact same content in the file. But the use of user-emacs-directory makes it more portable.

5
  • Thanks. I tried the first one but it did insert the template line.
    – zeynel
    Commented Sep 10, 2023 at 17:02
  • @zeynel Did you activate auto-insert-mode using either M-x auto-insert-mode or (auto-insert-mode 1) ? This is what makes the insertion automatic. You can also perform the insertion manually by invoking M-x auto-insert in a blank org-mode buffer. Commented Sep 10, 2023 at 17:59
  • I left in these statements and it worked: (setq auto-insert-query nil) (add-hook 'find-file-hook 'auto-insert) Thanks
    – zeynel
    Commented Sep 11, 2023 at 6:19
  • @zeynel I edited the code, try the new version. You are not supposed to have to add auto-insert to find-file-hook. Commented Sep 11, 2023 at 6:38
  • Thanks. Now it works fine.
    – zeynel
    Commented Sep 11, 2023 at 9:28

Your Answer

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

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