6

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?

Brian Fitzpatrick
  • 2,265
  • 1
  • 17
  • 40

2 Answers2

6

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.

Eric Leung
  • 103
  • 4
Lgen
  • 1,380
  • 10
  • 19
  • 1
    AFAIU, this is obsolete. Starting with 9.2, `tempo-define-template` is the way to go. – 147pm Dec 12 '19 at 05:50
2

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.)

147pm
  • 2,907
  • 1
  • 18
  • 39