For a long time now, I've wanted Emacs to auto-insert text into a buffer for me. I have not been able to get it to work, but thanks to the awesome emacs reddit community, I was able to get it to work. They also pointed out this rather extensive yasnippet Congration file: https://github.com/alexott/emacs-configs/blob/master/rc/emacs-rc-auto-insert.el
Inserting a predefined snippet via Emacs lisp is defined here. In case you don't like reading, then just do the following:
(yas-expand-snippet (yas-lookup-snippet "SnippetName"))
I'm defining some functions that will automatically expand when I open a file. Cool eh? It will also turn my state into evil-state. That way I can just start typing.
;; -*- lexical-binding: t -*-
(defun my/yas-org-snippet ()
(interactive)
(yas-expand-snippet (yas-lookup-snippet "<org-mode"))
(evil-insert-state))
(defun my/yas-elisp-init-files-snippet ()
(interactive)
(yas-expand-snippet (yas-lookup-snippet "<elisp-init-files"))
(evil-insert-state))
(defun my/yas-c-mode-snippet ()
(interactive)
(yas-expand-snippet (yas-lookup-snippet "<c-mode"))
(evil-insert-state))
(defun my/cover-letter-file-snippet ()
(interactive)
(yas-expand-snippet (yas-lookup-snippet "<cover-letter"))
(evil-insert-state))
(defun my/blog-entry-file-snippet ()
(interactive)
(yas-expand-snippet (yas-lookup-snippet "<blog-entry"))
(evil-insert-state))
(defun my/yas-web-mode-snippet ()
(interactive)
(yas-expand-snippet (yas-lookup-snippet "<web-mode"))
(evil-insert-state))
This section configures emacs to autoinsert text into an empty buffer via yasnippets.
Require the autoinsert library.
;;(use-package autoinsert)
When you open up an empty file, perform auto-insertion, if applicable.
(add-hook 'find-file-hook 'auto-insert)
Tell Emacs where to look for default text files to insert into empty files. If you don't want to use yasnippet then you could use this method instead. But let's be honest, we all like yasnippets, so you really probably don't need this next section of code.
(setq auto-insert-directory "~/.config/emacs/auto-insert-directory/")
Enable autoinsert mode, and do not ask me if I want to perform automatic insertion. Just do it.
(setq auto-insert-mode t
auto-insert-query t)
Auto-insertion of a buffer is completed via the variable auto-insert-alist
. If CONDITION is met, then ACTION is done.
=(add-to-list auto-insert-alist '((CONDITION . DESCRIPTION) . ACTION))=
CONDITION may be a regexp that matches the file name or a symbol that matches the major mode.
ACTION may be a function or a filename found in auto-insert-directory.
(setq auto-insert-alist '((web-mode . [my/yas-web-mode-snippet])
(c-mode . [my/yas-c-mode-snippet])
(org-mode . [my/yas-org-snippet])))
I use org-mode a lot. And different directories have different formats. This next bit of code ensures that when I open a file in a specific directory, Emacs inserts a template specific to that directory.
Much of my Emacs init files, use org-mode. So if I am in my init-file directory, go ahead and open an org file with that predefined snippet.
(add-to-list 'auto-insert-alist '((".*/lisp/.*\\.org$") . [my/yas-elisp-init-files-snippet]))
I have a directory where I write cover letters. I want a default snippet to expand whenever I write a cover letter.
(add-to-list 'auto-insert-alist '((".*/cover-letters.*\\.org$") . [my/cover-letter-file-snippet]))
I use org-mode for blogging. It is quite nice to have it auto-insert what needs to be inserted.
(add-to-list 'auto-insert-alist '((".*/my-wordpress-blog.*\\.org$") . [my/blog-entry-file-snippet]))
(provide 'init-autoinsert)