<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Diveck]]></title><description><![CDATA[Software engineer]]></description><link>https://diveck.hashnode.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1641107523771/Z9-4D81_-.png</url><title>Diveck</title><link>https://diveck.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 21:55:07 GMT</lastBuildDate><atom:link href="https://diveck.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Creating Code Snippets in Visual Studio Code]]></title><description><![CDATA[Code snippets
Code snippets are small, reusable source code or text templates that make it easier for a programmer to write repeating code patterns.
Let's say you write a function to calculate the time it takes for a program to finish executing.
impo...]]></description><link>https://diveck.hashnode.dev/creating-code-snippets-in-visual-studio-code</link><guid isPermaLink="true">https://diveck.hashnode.dev/creating-code-snippets-in-visual-studio-code</guid><category><![CDATA[Visual Studio Code]]></category><category><![CDATA[snippets]]></category><dc:creator><![CDATA[Divine Chukwu]]></dc:creator><pubDate>Sat, 01 Jan 2022 17:03:17 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1641056404138/CBX-wn8M8s.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-code-snippets">Code snippets</h1>
<p>Code snippets are small, reusable source code or text templates that make it easier for a programmer to write repeating code patterns.
Let's say you write a function to calculate the time it takes for a program to finish executing.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-title">time</span>

<span class="hljs-title">def</span> <span class="hljs-title">total_time</span>():
    <span class="hljs-title">start_time</span> <span class="hljs-operator">=</span> <span class="hljs-title">time</span>.<span class="hljs-title">time</span>()
    <span class="hljs-title">a</span> <span class="hljs-operator">=</span> 1
    <span class="hljs-title">b</span> <span class="hljs-operator">=</span> 2
    <span class="hljs-title">c</span> <span class="hljs-operator">=</span> <span class="hljs-title">a</span> <span class="hljs-operator">+</span> <span class="hljs-title">b</span>
    <span class="hljs-title">print</span>(<span class="hljs-title">c</span>) #3

    <span class="hljs-title">end_time</span> <span class="hljs-operator">=</span> <span class="hljs-title">time</span>.<span class="hljs-title">time</span>()
    <span class="hljs-title">total_time</span> <span class="hljs-operator">=</span> <span class="hljs-title">end_time</span> <span class="hljs-operator">-</span> <span class="hljs-title">start_time</span>
    <span class="hljs-title">print</span>(<span class="hljs-string">"Time: "</span>, <span class="hljs-title">total_time</span>)

<span class="hljs-title">total_time</span>()
</code></pre><p>And then along the line, you need to use this function repeatedly, can you imagine how tiring that would be? </p>
<p>Instead of writing this function, we can just make it a snippet and call it whenever we need to rewrite the function.</p>
<h2 id="heading-built-in-snippets">Built-in snippets</h2>
<p>There are a lot of built-in snippets in VS code for so many languages such as Python, Ruby, JavaScript, Markdown, PHP etc.</p>
<p>To see the available snippets for a language in VS code, go to the command palette by pressing Ctrl+Shift+P and click on the Insert Snippet command</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1641050519243/9rJG0770f.png" alt="Screenshot at 2022-01-01 16-23-28.png" /></p>
<p>This also includes user snippets that you have defined and any snippets provided by extensions you have installed.</p>
<h2 id="heading-create-your-own-snippets">Create your own snippets</h2>
<p>Now that we know what snippets are let us see how we can create our own snippets.
You can easily create your own code snippets without the use of any extension. To do this,
go to <strong>File &gt; Preferences &gt; User Snippets</strong>, and then choose the language you want your snippet to be in, or you can make it a global snippet for all languages.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1641052064034/DOfx8Ig2f.png" alt="Screenshot at 2022-01-01 16-53-10.png" /></p>
<p>After selecting a snippet language you should see a screen with this text being commented.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1641052416035/MJlSH10wy.png" alt="Screenshot at 2022-01-01 16-56-08.png" /></p>
<p>Select this part of the code and uncomment it</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1641052605816/YHDKcgEhF.png" alt="Screenshot at 2022-01-01 17-00-33.png" /></p>
<p>To give you something like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1641052657647/2FlIPT7f_.png" alt="Screenshot at 2022-01-01 17-00-59.png" /></p>
<p>Now on the line <em>Print to console</em> rename it to any name of your choice that you wish to name your snippet, Rename log to whatever name you want to use and call up our snippet when coding, Edit the content of the body to the code you want your snippet to show.</p>
<pre><code><span class="hljs-string">"Print to console"</span>: {
        <span class="hljs-string">"prefix"</span>: <span class="hljs-string">"log"</span>,
        <span class="hljs-string">"body"</span>: [
            <span class="hljs-string">"console.log('<span class="hljs-subst">$1</span>');"</span>,
            <span class="hljs-string">"<span class="hljs-subst">$2</span>"</span>
        ],
        <span class="hljs-string">"description"</span>: <span class="hljs-string">"Log output to console"</span>
    }
</code></pre><p>How about we do this</p>
<ul>
<li>change <code>print to console</code> to <code>my snippet</code> </li>
<li>change <code>log</code> to <code>console</code></li>
<li>change body content to <code>console.log('hurray we just created our first snippet on Javascript')</code></li>
<li>save and exit</li>
</ul>
<pre><code><span class="hljs-string">"my snippet"</span>: {
        <span class="hljs-string">"prefix"</span>: <span class="hljs-string">"console"</span>,
        <span class="hljs-string">"body"</span>: [
            <span class="hljs-string">"console.log('hurray we just created our first snippet on Javascript')"</span>
        ],
        <span class="hljs-string">"description"</span>: <span class="hljs-string">"Log output to console"</span>
    }
</code></pre><p>Now go to your Javascript file and type console, you will see that your snippet would appear and ready to be reused.</p>
<p>I hope you found this article helpful, if any questions please feel free to ask. Thanks for reading. Please like/share so that it reaches others as well.</p>
]]></content:encoded></item></channel></rss>