💾 Archived View for bacaliu.de › 20220911-yasnippet.html captured on 2023-07-10 at 13:51:04.
-=-=-=-=-=-=-
<!DOCTYPE html> <html lang="en"> <head> <!-- 2023-06-26 Mo 07:33 --> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Yasnippets</title> <meta name="description" content="overview of Yasnippets for emacs" /> <meta name="keywords" content="Emacs, Yasnippets, Doom Emacs" /> <meta name="generator" content="Org Mode" /> <meta property="og:type" content="article"> <meta property="og:site_name" content="Bacaliu.de"> <link rel="stylesheet" type="text/css" href="/css/modus.css"> <link rel="shortcut icon" href="/favicon.ico" sizes="65x65"> <script src=/static/js/htmx.min.js></script> <link rel="stylesheet" type="text/css" href="/css/colors/auto.css" id="ef-theme" hx-swap="outerHTML" hx-get="/data/colorscheme" hx-trigger="load"> </head> <body> <header id="preamble" class="status"> <a id='top'></a> <pre style='text-align: left; font-size: min(2vw, 1rem); display: inline-block;'> ___ _ _ _ | _ ) __ _ __ __ _| (_)_ _ __| |___ | _ \/ _` / _/ _` | | | || |_/ _` / -_) |___/\__,_\__\__,_|_|_|\_,_(_)__,_\___| </pre><br> <a href='#top'>↑</a> - <a href='/index.html'>Home</a> - <a href='/search.html'>Search</a> - <a href='#bottom'>↓</a> <br> <div hx-get="/data/colorlist" hx-trigger="load"></div> <hr> </header> <main id="content" class="content"> <header> <h1 class="title">Yasnippets</h1> <p class="subtitle" role="doc-subtitle">don't type def init(self) again and again and again</p> </header><nav id="table-of-contents" role="doc-toc"> <h2>Table of Contents</h2> <div id="text-table-of-contents" role="doc-toc"> <ul> <li><a href="#what-yasnippet">1. What is Yasnippet?</a></li> <li><a href="#where-do-templates">2. Where do the templates go?</a></li> <li><a href="#how-are-templates">3. How are the templates structured?</a></li> <li><a href="#examples">4. Examples</a> <ul> <li><a href="#python-method">4.1. python method</a></li> <li><a href="#orgmode-figure">4.2. orgmode figure</a></li> </ul> </li> <li><a href="#bibliography">5. Bibliography</a></li> <li><a href="#nav">Nav</a></li> </ul> </div> </nav> <div id="outline-container-what-yasnippet" class="outline-2"> <h2 id="what-yasnippet"><span class="section-number-2">1.</span> <a href="#what-yasnippet">What is Yasnippet?</a></h2> <div class="outline-text-2" id="text-1"> <p> 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 <code><some_trigger></code> + <kbd>tab</kbd>.<br> </p> </div> </div> <div id="outline-container-where-do-templates" class="outline-2"> <h2 id="where-do-templates"><span class="section-number-2">2.</span> <a href="#where-do-templates">Where do the templates go?</a></h2> <div class="outline-text-2" id="text-2"> <p> I use Doom Emacs (<a href="#citeproc_bib_item_1">doomemacs 2022</a>), therefore my configuration lays in <kbd>~/.doom.d/</kbd> instead of <kbd>~/.emacs.d/</kbd>. My snippets are located in <kbd>~/.doom.d/snippets/</kbd>. They are mode-dependent by putting them in the right subfolder.<br> </p> <p> Snippets for are located in <kbd>~/.doom.d/snippets/python-mode/</kbd>, for in <kbd>~/.doom.d/snippets/org-mode</kbd> 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 <kbd>M-x</kbd> <code>yas-new-snippet</code> which opens a new buffer with an example to make the template.<br> </p> </div> </div> <div id="outline-container-how-are-templates" class="outline-2"> <h2 id="how-are-templates"><span class="section-number-2">3.</span> <a href="#how-are-templates">How are the templates structured?</a></h2> <div class="outline-text-2" id="text-3"> <p> 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 <code>#</code>. The important property to set is were <code><something></code> should be replaced with the trigger-word.<br> </p> <p> The metadata-block ends with , after that <code>#</code> looses it's special meaning and you can using it even as fist character of the row without need for escaping.<br> </p> <div class="org-src-container"> <pre class="src src-conf"><span class="org-comment-delimiter"># </span><span class="org-comment">-*- mode: snippet -*-</span> <span class="org-comment-delimiter"># </span><span class="org-comment">name: <blabla></span> <span class="org-comment-delimiter"># </span><span class="org-comment">key: <something></span> <span class="org-comment-delimiter"># </span><span class="org-comment">[... more ...]</span> <span class="org-comment-delimiter"># </span><span class="org-comment">--</span> THE SNIPPET TO INCLUDE </pre> </div> <p> In the snippet you can use <code>$1</code> to let the curser going there first, <code>$2</code> after pressing <kbd>tab</kbd> and so on. If included, after finishing the last field and pressing <kbd>tab</kbd> again, the cursor moves to position <code>$0</code>. You can even include default-values by writing which makes the cursor moves to the position as usual but marking <code>default-value</code>. By pressing <kbd>tab</kbd>, the default-value stays there. By typing text it is overwritten by it.<br> </p> </div> </div> <div id="outline-container-examples" class="outline-2"> <h2 id="examples"><span class="section-number-2">4.</span> <a href="#examples">Examples</a></h2> <div class="outline-text-2" id="text-4"> <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> </div> <div id="outline-container-python-method" class="outline-3"> <h3 id="python-method"><span class="section-number-3">4.1.</span> <a href="#python-method">python method</a></h3> <div class="outline-text-3" id="text-4-1"> <div class="org-src-container"> <label class="org-src-name"><span class="listing-number">Listing 1: </span><kbd>~/.doom.d/snippets/python-mode/method</kbd></label><pre class="src src-conf"><span class="org-comment-delimiter"># </span><span class="org-comment">-*- mode: snippet -*-</span> <span class="org-comment-delimiter"># </span><span class="org-comment">name: method</span> <span class="org-comment-delimiter"># </span><span class="org-comment">key: met</span> <span class="org-comment-delimiter"># </span><span class="org-comment">--</span> def ${1:method}(self$2): $0 pass </pre> </div> <p> Instead of typing every method by hand (and forget the <code class="src src-python"><span class="org-tree-sitter-hl-faceXkeyword">self</span></code> every second time) I just type in <code>met</code> and then <kbd>tab</kbd> replaces it with a minimal example. The cursor is at position <code>$1</code> ready to overwrite the default name <i>method</i>. Type <kbd>tab</kbd> again and the cursor moves to position <code>$2</code> to type in arguments if nessecary. After the last <kbd>tab</kbd> yasnippet turns off and the cursor sits at position <code>$0</code>.<br> </p> </div> </div> <div id="outline-container-orgmode-figure" class="outline-3"> <h3 id="orgmode-figure"><span class="section-number-3">4.2.</span> <a href="#orgmode-figure">orgmode figure</a></h3> <div class="outline-text-3" id="text-4-2"> <p> Adapted from yyr (<a href="#citeproc_bib_item_3">yyr 2022</a>) I made the following snippet to easily insert images in orgmode.<br> </p> <div class="org-src-container"> <label class="org-src-name"><span class="listing-number">Listing 2: </span><kbd>~/.doom.d/snippets/org-mode/figure_invertable</kbd></label><pre class="src src-conf"><span class="org-comment-delimiter"># </span><span class="org-comment">-*- mode: snippet -*-</span> <span class="org-comment-delimiter"># </span><span class="org-comment">name: figure invertable</span> <span class="org-comment-delimiter"># </span><span class="org-comment">key: figi</span> <span class="org-comment-delimiter"># </span><span class="org-comment">--</span> <span class="org-comment-delimiter">#</span><span class="org-comment">+ATTR_HTML: :width ${1:50%} :class invertable</span> <span class="org-comment-delimiter">#</span><span class="org-comment">+caption: ${3:caption}</span> `(progn (org-insert-link <span class="org-string">'(4)) nil)`</span> <span class="org-string">$0</span> </pre> </div> <p> 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.<br> </p> <p> This snippet gives the images the class in html (Therefore <code>figi</code> + <kbd>tab</kbd>). A similar snippet with key <code>fig</code> makes the same without giving this class.<br> </p> </div> </div> </div> <div id="outline-container-bibliography" class="outline-2"> <h2 id="bibliography"><span class="section-number-2">5.</span> <a href="#bibliography">Bibliography</a></h2> <div class="outline-text-2" id="text-5"> <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> </div> </div> <div id="outline-container-nav" class="outline-2"> <h2 id="nav"><a href="#nav">Nav</a></h2> <div class="outline-text-2" id="text-nav"> <ul class="org-ul"> <li>Tags: <a href="./tags/Emacs.html">Emacs</a><br></li> </ul> <!-- BEGIN insert Backlinks (but there are no) --> <ul class="org-ul"> <li>Formats: <a href="./20220911-yasnippet.md">md</a> - <a href="./20220911-yasnippet.txt">txt</a> - <a href="./20220911-yasnippet.html">html</a> - <a href="./20220911-yasnippet.gmi">gmi</a></li> </ul> </div> </div> </main> <footer id="postamble" class="status"> <hr> 2022-09-11 <br> <img alt="CC BY-4.0" style="border-width:0; vertical-align: middle; height: 1em;" src="/icons/cc-by-4.0-80x15.svg"> <a rel='license' href='http://creativecommons.org/licenses/by/4.0/'> CC BY-4.0. </a> <br> running at <a href='https://www.hosting.de/'>hosting.de</a> <br> Creator: <a href="https://www.gnu.org/software/emacs/">Emacs</a> 28.2 (<a href="https://orgmode.org">Org</a> mode 9.5.5); <br> <a href='/impressum-datenschutz.html' id='impressum-dings'> Impressum und Datenschutzerklärung </a> <a id='bottom'></a> </footer> </body> </html>