I just got into yasnippet and I wanted to automatically insert one of my fragments when opening a new file for certain modes. This brought me here, but I created a slightly different solution. Providing another alternative: ("new-shell" is the name of my personal fragment to provide a new script shell template)
(defun jsm/new-file-snippet (key)
"Call particular yasnippet template for newly created
files. Use by adding a lambda function to the particular mode
hook passing the correct yasnippet key"
(interactive)
(if (= (buffer-size) 0)
(progn
(insert key)
(call-interactively 'yas-expand))))
(add-hook 'sh-mode-hook '(lambda () (jsm/new-file-snippet "new-shell")))
IMO, my decision is a little less susceptible to hacking if the lynx changes dramatically.
source
share