Appendix for Yasnippets

This commit is contained in:
Howard Abrams 2024-08-04 22:11:00 -07:00
parent accf51fcc1
commit 36ea4b904e

View file

@ -201,3 +201,50 @@ Can we do both Bash, Fish and Powershell?
** Graphviz
** PlantUML
** Pikchr
* Tips and Tricks
This final chapter contains optional information you might find useful.
Oh, and please let me know if you have a tip or trick that you think should be included.
** Yasnippet Templates
If you use [[https://www.emacswiki.org/emacs/Yasnippet][Yasnippet]], and include the [[https://github.com/AndreaCrotti/yasnippet-snippets][yasnippet-snippets project]], you can type =<s= in an Org buffer to expand into a src block, and type =def= in an Emacs Lisp buffer to get a function definition, but why not have both?
In an Org file, type ~M-x yas-new-snippet~ and replace the default text with:
#+begin_example
# key: #slf
# name: emacs-lisp-defun
# --
,#+BEGIN_SRC emacs-lisp
(defun ${1:fun} (${2:args})
"${3:docstring}"
${4:(interactive${5: "${6:P}"})}
$0)
,#+END_SRC
#+end_example
Type ~C-c C-c~ to save and install the snippet with Org.
Lets make one for variables:
#+begin_example
# key: slv
# name: emacs-lisp-defvar
# --
,#+BEGIN_SRC emacs-lisp
(defvar ${1:symbol} ${2:initvalue} "${3:docstring}")
,#+END_SRC
#+end_example
And maybe another one for defining unit tests?
#+begin_example
# -*- mode: snippet -*-
# name: ert-deftest
# key: edt
# --
,#+BEGIN_SRC emacs-lisp :tangle no
(ert-deftest $1-test ()
(should (= $0)))
,#+END_SRC
#+end_example