<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Maxime Lefebvre</title>
    <description>The latest articles on DEV Community by Maxime Lefebvre (@maximelbv).</description>
    <link>https://dev.to/maximelbv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F411953%2F9f929140-0a3d-473c-959f-d5d32708e426.png</url>
      <title>DEV Community: Maxime Lefebvre</title>
      <link>https://dev.to/maximelbv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maximelbv"/>
    <language>en</language>
    <item>
      <title>How to create custom VS Code snippets ⌨️</title>
      <dc:creator>Maxime Lefebvre</dc:creator>
      <pubDate>Thu, 31 Oct 2024 04:11:54 +0000</pubDate>
      <link>https://dev.to/maximelbv/how-to-create-custom-vs-code-snippets-cd7</link>
      <guid>https://dev.to/maximelbv/how-to-create-custom-vs-code-snippets-cd7</guid>
      <description>&lt;p&gt;An enhanced version of this post is available &lt;a href="https://www.maximelbv.com/blog/vscode-custom-snippets" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;As developers, we often find ourselves writing similar code patterns across different projects. Visual Studio Code offers many productivity features to address this, including snippets.&lt;/p&gt;

&lt;p&gt;While built-in snippets are useful, creating custom snippets tailored to your needs can significantly enhance your coding efficiency.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll introduce you to the basics of VS Code snippets. We'll cover what snippets are, how to create your first custom snippet, and explore some fundamental features like tab stops and default values.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is a snippet ?
&lt;/h2&gt;

&lt;p&gt;A code snippet is a small block of reusable code that you can quickly insert into your programs. In VS Code, snippets help developers avoid repetitive tasks by providing a quick way to generate common code structures.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb0h6ksabm1067e5n02dl.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb0h6ksabm1067e5n02dl.gif" alt="snippet example" width="540" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;They can be accessed through VS Code extensions (if you use React, i highly recommend installing &lt;a href="https://marketplace.visualstudio.com/items?itemName=dsznajder.es7-react-js-snippets" rel="noopener noreferrer"&gt;this one&lt;/a&gt;) but you can also create your own personalized snippets, let's see how to do that !&lt;/p&gt;




&lt;h2&gt;
  
  
  Your first custom snippet
&lt;/h2&gt;

&lt;p&gt;Now that we understand what snippets are, let's dive into creating your own. Let's break down the process into simple steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Set up a new snippets file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In VS Code, open your command palette with &lt;code&gt;CMD / CTRL + SHIFT + P&lt;/code&gt; and search for "Snippets: Configure User Snippets"&lt;/p&gt;

&lt;p&gt;Select "New Global Snippets file" and name it. (You can also create a snippet file especially for your project or for a specific language, feel free to choose an option that suits your needs)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Understand the structure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By generating the file, you will se a bunch of informations and a default example at the end, you can uncomment it.&lt;/p&gt;

&lt;p&gt;Here, we have a snippet that prints a value to the console. Let's see how it is structured:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"Print to console"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scope"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"javascript,typescript"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"prefix"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"log"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"console.log('$1');"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"$2"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Log output to console"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;scope&lt;/code&gt; defines where this snippet is applicable. In this case, the snippet works in javascript ans typescript files&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;prefix&lt;/code&gt; is the acronym that you will type to invoke the snippet&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;body&lt;/code&gt; contains the code that will be displayed&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;description&lt;/code&gt; will show up in the autocomplete suggestions&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Try it yourself&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Save this file, and open a new one with the .js extension&lt;/li&gt;
&lt;li&gt;by typing log you should see the vscode autocomplete suggestion for the snippet we just described&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Tab stops
&lt;/h2&gt;

&lt;p&gt;We now have the keys to create our custom snippets...&lt;br&gt;
But did you notice the &lt;code&gt;$1&lt;/code&gt; and &lt;code&gt;$2&lt;/code&gt; ?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"Print to console"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scope"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"javascript,typescript"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"prefix"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"log"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"console.log('$1');"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"$2"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Log output to console"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are Tab Stops, they define where the cursor will be placed at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$1&lt;/code&gt; : initial position&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$2&lt;/code&gt; : second position (after pressing &lt;code&gt;TAB&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$0&lt;/code&gt; : last position&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is not mandatory, but very helpfull if you have multiple values to add to your snippet. By default, the cursor is placed at the end of the body&lt;/p&gt;




&lt;h2&gt;
  
  
  Default values
&lt;/h2&gt;

&lt;p&gt;We can also define default values for tab stops that will automatically be highlighted.&lt;br&gt;
Simply replace the &lt;code&gt;$1&lt;/code&gt; by &lt;code&gt;${1:Your default value}&lt;/code&gt;,&lt;/p&gt;

&lt;p&gt;If i take the previous example and replace the &lt;code&gt;$1&lt;/code&gt; by &lt;code&gt;${1:Hello world}&lt;/code&gt;, this will result to&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7urp3zq0va8euxfhtut.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7urp3zq0va8euxfhtut.gif" alt="snippet default value example" width="846" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the default value will be selected and so, easily replaceable 👍&lt;/p&gt;




&lt;h2&gt;
  
  
  Multiple values
&lt;/h2&gt;

&lt;p&gt;It also works with multiple values, simply enclose the values between &lt;code&gt;||&lt;/code&gt; chars :&lt;/p&gt;

&lt;p&gt;For example: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.${2|log,error,table|}(${1:Hello world})&lt;/code&gt; will result to&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa6juliuuzhok28y9w99b.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa6juliuuzhok28y9w99b.gif" alt="snippet multiple values example" width="530" height="148"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's all folks!&lt;/p&gt;

&lt;p&gt;You now have the keys to create basic custom snippets in vscode !&lt;/p&gt;

&lt;p&gt;If you're interested in going further, I highly recommend you take a look at the &lt;a href="https://code.visualstudio.com/docs/editor/userdefinedsnippets" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;, which goes into more detail about custom snippets.&lt;/p&gt;

&lt;p&gt;in the meantime, you can take a quizz to test your knowledge on the &lt;a href="https://www.maximelbv.com/blog/vscode-custom-snippets" rel="noopener noreferrer"&gt;original post&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vscode</category>
      <category>tooling</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
