3

I use org-mode to maintain my personal site. Whenever I create a new .org file in my website, I find myself retyping the following

#+TITLE: 
#+OPTIONS: html-postamble:nil whn:nil toc:nil nav:nil
#+HTML_HEAD:
#+HTML_HEAD_EXTRA: 

Is it possible to bind this template to a keystroke?

  • 1
    Are you willing to use Yasnippet? – Dan Aug 3 '17 at 22:07
  • I'm willing to learn anything that works! – Brian Fitzpatrick Aug 3 '17 at 22:30
  • I use autoinsert mode for this purpose. No keystrokes to learn. – Juancho Aug 3 '17 at 23:52
  • See this discussion for latest org-mode template usage. – 147pm Dec 12 '19 at 5:53
3

You can use the templating system of org-mode. If you insert the following lines at the end of your init.el file:

(add-to-list 'org-structure-template-alist
             '("P" "#+TITLE:\n#+OPTIONS: html-postamble:nil whn:nil toc:nil nav:nil\n#+HTML_HEAD:\n#+HTML_HEAD_EXTRA:\n\n? "))

After you restart emacs or source your init.el file, you only have to type <P at the beginning of your org-mode file, then the <TAB> key and this will automatically integrate the desired code.

See http://orgmode.org/manual/Easy-templates.html for the documentation of the template mechanism.

  • AFAIU, this is obsolete. Starting with 9.2, tempo-define-template is the way to go. – 147pm Dec 12 '19 at 5:50
0

You may want to check out this example wherein the author suggests this define-skeleton:

(define-skeleton org-skeleton
  "In-buffer settings info for a emacs-org file."
  "Title: "
  "#+TITLE:" str " \n"
  "#+AUTHOR: Your Name\n"
  "#+email: your-email@server.com\n"
  "#+INFOJS_OPT: \n"
  "#+BABEL: :session *R* :cache yes :results output graphics :exports both :tangle yes \n"
  "-----"
 )
(global-set-key [C-S-f4] 'org-skeleton)

Customize to please. But like so many things in the Emacs world, I don't know if this is "best practice."

There is also autoinsert which will insert lines whenever you create a new file. Here's what I have in my init:

(use-package autoinsert
  :ensure t
  :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 "\\.org?$" "default-org.org"))

Note the last line (define-auto-insert "\\.org?$" "default-org.org") So the file default-org.org (in my .emacs.d directory) contains my customized in-buffer settings, which will be automatically inserted at the top whenever I create a file with an org ending.

Unfortunately, I could not figure out how to plant variables and expanding things in this template, as is supposedly possible. (See this cry for help.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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