💾 Archived View for bacaliu.de › 20220911-yasnippet.gmi captured on 2023-11-14 at 08:06:56. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-09-08)
-=-=-=-=-=-=-
Yasnippet (joaotavora 2022) is a template-system for Emacs. It allows you to set up snippets which get insertet when typing `<some_trigger> ` + `tab'.
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 `python-mode' are located in `~/.doom.d/snippets/python-mode/', for `org-mode' 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 `python-mode'). Then run `M-x' `yas-new-snippet ` which opens a new buffer with an example to make the template.
The first line enables `snippet-mode'. 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 `# key: <something>' were `<something> ` 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: <blabla> # key: <something> # [... 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 `${1:default-value}' 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.
# -*- mode: snippet -*- # name: method # key: met # -- def ${1:method}(self$2): $0 pass
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 `.
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
Elisp-code inside backquotes is evaluated, the result is inserted. The original version would include a `t' before the `[​[image/path]]', because `org-insert-link' returns `t' when succesfull. `progn' just makes sure to return `nil' therefore yasnippet has nothing to include.
This snippet gives the images the `.invertable' class in html (Therefore `figi ` + `tab'). A similar snippet with key `fig ` makes the same without giving this class.
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.
License: CC BY-4.0 [Impressum und Datenschutz]