💾 Archived View for bacaliu.de › 20220911-yasnippet.md captured on 2023-07-10 at 13:50:56.

View Raw

More Information

-=-=-=-=-=-=-


# Table of Contents

1.  [What is Yasnippet?](#what-yasnippet)
2.  [Where do the templates go?](#where-do-templates)
3.  [How are the templates structured?](#how-are-templates)
4.  [Examples](#examples)
    1.  [python method](#python-method)
    2.  [orgmode figure](#orgmode-figure)
5.  [Bibliography](#bibliography)
-   [Nav](#nav)
-   [Footer](#footer)



<a id="what-yasnippet"></a>

# What is Yasnippet?

Yasnippet (<a href="#citeproc_bib_item_2">joaotavora 2022</a>) is a template-system for Emacs. It allows you to set up snippets which get insertet when typing `<some_trigger>` + `tab`.  


<a id="where-do-templates"></a>

# Where do the templates go?

I use Doom Emacs (<a href="#citeproc_bib_item_1">doomemacs 2022</a>), 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.  


<a id="how-are-templates"></a>

# 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.  


<a id="examples"></a>

# Examples

<figure>
<figcaption><span class="figure-number">Screencast 1: </span>Example for Python-snippets</figcaption>
<script id="asciicast-RwVBBUuxmYsQ9k7sSSw7Y06Lg" src="https://asciinema.org/a/RwVBBUuxmYsQ9k7sSSw7Y06Lg.js" async></script>
</figure>


<a id="python-method"></a>

## python method

    # -*- 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`.  


<a id="orgmode-figure"></a>

## orgmode figure

Adapted from yyr (<a href="#citeproc_bib_item_3">yyr 2022</a>) 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 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.  


<a id="bibliography"></a>

# Bibliography

<style>.csl-entry{text-indent: -0; margin-left: 0;}</style><div class="csl-bib-body">
  <div class="csl-entry"><a id="citeproc_bib_item_1"></a>doomemacs. 2022. “doomemacs,” September 11, 2022, URL: <a href="https://github.com/doomemacs/doomemacs">https://github.com/doomemacs/doomemacs</a>, retrieved on September 11, 2022.</div>
  <div class="csl-entry"><a id="citeproc_bib_item_2"></a>joaotavora. 2022. “yasnippet,” <i>Github</i> September 11, 2022, URL: <a href="https://github.com/joaotavora/yasnippet">https://github.com/joaotavora/yasnippet</a>, retrieved on September 11, 2022.</div>
  <div class="csl-entry"><a id="citeproc_bib_item_3"></a>yyr. 2022. “yasnippets-orgmode,” <i>Github</i> September 11, 2022, URL: <a href="https://github.com/yyr/yasnippets-orgmode">https://github.com/yyr/yasnippets-orgmode</a>, retrieved on September 11, 2022.</div>
</div>


<a id="nav"></a>

# Nav

-   Tags: [Emacs](./tags/Emacs.md)

<!-- BEGIN insert Backlinks (but there are no) -->

-   Formats: [md](./20220911-yasnippet.md) - [txt](./20220911-yasnippet.txt) - [html](./20220911-yasnippet.html) - [gmi](./20220911-yasnippet.gmi)


<a id="footer"></a>

# Footer

License: CC BY-4.0  
[Impressum und Datenschutz](./impressum-datenschutz.gmi)