πΎ Archived View for bacaliu.de βΊ 20220911-yasnippet.txt captured on 2023-07-10 at 13:51:02.
-=-=-=-=-=-=-
ββββββββββββββββββββββββββββββββββββββββββββββββββ 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 `<some_trigger>' + `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 `<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 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: <https://github.com/doomemacs/doomemacs>, retrieved on September 11, 2022. joaotavora. 2022. βyasnippet,β /Github/ September 11, 2022, URL: <https://github.com/joaotavora/yasnippet>, retrieved on September 11, 2022. yyr. 2022. βyasnippets-orgmode,β /Github/ September 11, 2022, URL: <https://github.com/yyr/yasnippets-orgmode>, 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>