━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ YASNIPPETS don't type def init(self) again and again and again ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2022-09-11 Table of Contents ───────────────── 1. What is Yasnippet? 2. Where do the templates go? 3. How are the templates structured? 4. Examples .. 1. python method .. 2. orgmode figure 5. Bibliography Nav Footer 1 What is Yasnippet? ════════════════════ Yasnippet (joaotavora 2022) is a template-system for Emacs. It allows you to set up snippets which get insertet when typing `' + `tab'. 2 Where do the templates go? ════════════════════════════ I use Doom Emacs (doomemacs 2022), therefore my configuration lays in `~/.doom.d/' instead of `~/.emacs.d/'. My snippets are located in `~/.doom.d/snippets/'. They are mode-dependent by putting them in the right subfolder. Snippets for are located in `~/.doom.d/snippets/python-mode/', for in `~/.doom.d/snippets/org-mode' and so on. If you're unsure where the snippets should be placed, open a file (e.g. a python-file to enable ). Then run `M-x' `yas-new-snippet' which opens a new buffer with an example to make the template. 3 How are the templates structured? ═══════════════════════════════════ The first line enables . Some next lines can store information about the creator, the name of the snippet, etc. All this lines start wit a `#'. The important property to set is were `' should be replaced with the trigger-word. The metadata-block ends with , after that `#' looses it's special meaning and you can using it even as fist character of the row without need for escaping. ┌──── │ # -*- mode: snippet -*- │ # name: │ # key: │ # [... more ...] │ # -- │ THE SNIPPET TO INCLUDE └──── In the snippet you can use `$1' to let the curser going there first, `$2' after pressing `tab' and so on. If included, after finishing the last field and pressing `tab' again, the cursor moves to position `$0'. You can even include default-values by writing which makes the cursor moves to the position as usual but marking `default-value'. By pressing `tab', the default-value stays there. By typing text it is overwritten by it. 4 Examples ══════════ 4.1 python method ───────────────── ┌──── │ # -*- mode: snippet -*- │ # name: method │ # key: met │ # -- │ def ${1:method}(self$2): │ $0 │ pass └──── Listing 1: `~/.doom.d/snippets/python-mode/method' Instead of typing every method by hand (and forget the `self' every second time) I just type in `met' and then `tab' replaces it with a minimal example. The cursor is at position `$1' ready to overwrite the default name /method/. Type `tab' again and the cursor moves to position `$2' to type in arguments if nessecary. After the last `tab' yasnippet turns off and the cursor sits at position `$0'. 4.2 orgmode figure ────────────────── Adapted from yyr (yyr 2022) I made the following snippet to easily insert images in orgmode. ┌──── │ # -*- mode: snippet -*- │ # name: figure invertable │ # key: figi │ # -- │ #+ATTR_HTML: :width ${1:50%} :class invertable │ #+caption: ${3:caption} │ `(progn (org-insert-link '(4)) nil)` │ $0 └──── Listing 2: `~/.doom.d/snippets/org-mode/figure_invertable' Elisp-code inside backquotes is evaluated, the result is inserted. The original version would include a before the , because returns when succesfull. just makes sure to return therefore yasnippet has nothing to include. This snippet gives the images the class in html (Therefore `figi' + `tab'). A similar snippet with key `fig' makes the same without giving this class. 5 Bibliography ══════════════ doomemacs. 2022. “doomemacs,” September 11, 2022, URL: , retrieved on September 11, 2022. joaotavora. 2022. “yasnippet,” /Github/ September 11, 2022, URL: , retrieved on September 11, 2022. yyr. 2022. “yasnippets-orgmode,” /Github/ September 11, 2022, URL: , retrieved on September 11, 2022. Nav ═══ ⁃ Tags: [Emacs] ⁃ Formats: [md] - [txt] - [html] - [gmi] [Emacs] <./tags/Emacs.org> [md] <./20220911-yasnippet.md> [txt] <./20220911-yasnippet.txt> [html] <./20220911-yasnippet.html> [gmi] <./20220911-yasnippet.gmi> Footer ══════ License: CC BY-4.0 [Impressum und Datenschutz] [Impressum und Datenschutz] <./impressum-datenschutz.gmi>