{"id":145,"date":"2018-05-30T15:51:23","date_gmt":"2018-05-30T15:51:23","guid":{"rendered":"http:\/\/michaelschultz.net\/tech\/?p=145"},"modified":"2018-05-30T15:51:23","modified_gmt":"2018-05-30T15:51:23","slug":"using-vscode-snippets-to-apply-your-script-template","status":"publish","type":"post","link":"https:\/\/michaelschultz.net\/tech\/using-vscode-snippets-to-apply-your-script-template\/","title":{"rendered":"Using VSCode snippets to apply your script template"},"content":{"rendered":"<p>Previously I wrote about the need for standardizing scripts and showed the template that I made. The question is how to easily start your scripts from the template.\u00a0 You can open the template and save it as a new file but I think that is extra work I do not want to do.\u00a0 If your script editor allows for the use of code snippets, things become easier.\u00a0 My editor of choice is Visual Studio Code (VSCode) and I will show you how to create a code snippet for your template.\u00a0 You can use this to easily add repeatable code to your scripts.\u00a0 Again, this is being done with VSCode which is what I recommend as I love the script analyzer that it has as well as the ability to expand its functionality with community created extensions.<!--more--><\/p>\n<p>We need to access the User Snippets in VSCode by going to <strong>Preferences<\/strong> \u2013 <strong>User Snippets<\/strong> under <strong>File<\/strong>.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-149\" src=\"https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/snippets-menu.jpg?resize=622%2C428\" alt=\"\" width=\"622\" height=\"428\" srcset=\"https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/snippets-menu.jpg?w=622&amp;ssl=1 622w, https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/snippets-menu.jpg?resize=300%2C206&amp;ssl=1 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>There are two options.\u00a0 You can use a language specific snippets file that is already there or create a new global snippet file.\u00a0 I prefer to create me own global snippet file because it is easier for me to copy to another location for backing it up.<\/p>\n<p>Select New Global Snippet file and give your file a name and location to save it.\u00a0 I leave it in the default location.<\/p>\n<p>Once you click ok.\u00a0 You are presented with the file in VSCode which includes a basic explanation on how to format your snippet as well as an example.\u00a0 Add your snippet or as many as you need after the last commented out line and above the closing <strong>}<\/strong>.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/new_snippet_file.jpg\"><img data-recalc-dims=\"1\" decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-146\" src=\"https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/new_snippet_file.jpg?resize=508%2C195\" alt=\"\" width=\"508\" height=\"195\" srcset=\"https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/new_snippet_file.jpg?w=1015&amp;ssl=1 1015w, https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/new_snippet_file.jpg?resize=300%2C115&amp;ssl=1 300w, https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/new_snippet_file.jpg?resize=768%2C294&amp;ssl=1 768w\" sizes=\"auto, (max-width: 508px) 85vw, 508px\" \/><\/a><\/p>\n<p>The basic information you need is what\u00a0you want to name the snippet.\u00a0 I am calling mine Template.\u00a0 You need to set the scope which is what languages you want to snippet to show up for.\u00a0 More on that in a bit. You set the prefix which is like a short name.\u00a0 I went with temp.\u00a0 Then you add the code that the snippet will insert into your script.\u00a0 Each line of the code needs to sit between \u201c\u201d.<\/p>\n<p>Before I show what my snippet looks like, we need to discuss Scope.\u00a0 As I said, the scope is what languages you want the snippet to work with.\u00a0 Before it will show a snippet for the language you are using, you first need to save the script.\u00a0 Also, what if you have a snippet that you want available regardless of language?\u00a0 The easiest way to do this is to just omit the scope line as it mentions in the explanation created when you made the global snippet file.\u00a0 This makes the snippets without a scope available as soon as you start a new script or regardless of script language used.<\/p>\n<p>Here is what my snippet looks like.<\/p>\n<div class=\"codecolorer-container powershell default\" style=\"overflow:auto;white-space:nowrap;width:500px;\"><div class=\"powershell codecolorer\"><span class=\"st0\">&quot;Template&quot;<\/span>: <span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"st0\">&quot;prefix&quot;<\/span>: <span class=\"st0\">&quot;temp&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;body&quot;<\/span>: <span class=\"br0\">&#91;<\/span><br \/>\n<span class=\"st0\">&quot;&amp;lt;#&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;.SYNOPSIS&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Script synopsis&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;.SYNTAX&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Scrpt syntax&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;.DESCRIPTION&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Script description&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;.PARAMETER &quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Parameter input descriptions and repeat this as needed for each parameter&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;.INPUTS&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Input description if used&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;.OUTPUTS&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Output description if used&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;.NOTES&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;FileName:&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Author:&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Contact:&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Created:&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Modified:&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Version:&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;.EXAMPLE&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Example description and example&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;#&amp;gt;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;#-----Parameters-----&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Param (&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;)&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<br \/>\n<span class=\"st0\">&quot;#-----Initializations and Module Imports-----&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;#-----Variables-----&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;#-----Functions-----&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;#-----Execution-----&quot;<\/span><br \/>\n<span class=\"br0\">&#93;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;description&quot;<\/span>: <span class=\"st0\">&quot;PowerShell Template&quot;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"sy0\">,<\/span><\/div><\/div>\n<p>You then just save the file and reload VSCode.<\/p>\n<p>Start a new file and type in the prefix you set and hit tab.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-148\" src=\"https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/selectsnippet.jpg?resize=536%2C99\" alt=\"\" width=\"536\" height=\"99\" srcset=\"https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/selectsnippet.jpg?w=536&amp;ssl=1 536w, https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/selectsnippet.jpg?resize=300%2C55&amp;ssl=1 300w\" sizes=\"auto, (max-width: 536px) 85vw, 536px\" \/><\/p>\n<p>The prefix is replaced with my code snippet.\u00a0 As you will notice, the text is all white.\u00a0 That is because the file has not been saved yet.\u00a0 Once it is save, you code will become color coded.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/savedcode.jpg\"><img data-recalc-dims=\"1\" decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-147\" src=\"https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/savedcode.jpg?resize=764%2C150\" alt=\"\" width=\"764\" height=\"150\" srcset=\"https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/savedcode.jpg?w=1522&amp;ssl=1 1522w, https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/savedcode.jpg?resize=300%2C59&amp;ssl=1 300w, https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/savedcode.jpg?resize=768%2C151&amp;ssl=1 768w, https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/savedcode.jpg?resize=1024%2C201&amp;ssl=1 1024w, https:\/\/i0.wp.com\/michaelschultz.net\/tech\/wp-content\/uploads\/2018\/05\/savedcode.jpg?resize=1200%2C236&amp;ssl=1 1200w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<p>Now VSCode does have some variables that you can use as well and you can grab environmental variables.\u00a0 One thing I did was add variables from year, month, and date to auto populate the created field.\u00a0 The format is YYYYMMDD.<\/p>\n<div class=\"codecolorer-container powershell default\" style=\"overflow:auto;white-space:nowrap;width:500px;\"><div class=\"powershell codecolorer\"><span class=\"st0\">&quot;.NOTES&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;FileName: &quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Author: &quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Contact: &quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Created: $CURRENT_YEAR$CURRENT_MONTH$CURRENT_DATE&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Modified: &quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"st0\">&quot;Version: &quot;<\/span><span class=\"sy0\">,<\/span><\/div><\/div>\n<p>You can find more information at <a href=\"https:\/\/code.visualstudio.com\/docs\/editor\/userdefinedsnippets\" target=\"_blank\" rel=\"noopener\">https:\/\/code.visualstudio.com\/docs\/editor\/userdefinedsnippets<\/a>.<\/p>\n<p>While this is all done with VSCode, there are other editors that allow you to add code snippets.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously I wrote about the need for standardizing scripts and showed the template that I made. The question is how to easily start your scripts from the template.\u00a0 You can open the template and save it as a new file but I think that is extra work I do not want to do.\u00a0 If your &hellip; <a href=\"https:\/\/michaelschultz.net\/tech\/using-vscode-snippets-to-apply-your-script-template\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using VSCode snippets to apply your script template&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13,22],"tags":[11,24,23],"class_list":["post-145","post","type-post","status-publish","format-standard","hentry","category-scripting","category-vscode","tag-powershell","tag-scripting","tag-vscode"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p98a2r-2l","post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/comments?post=145"}],"version-history":[{"count":2,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":151,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts\/145\/revisions\/151"}],"wp:attachment":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/media?parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}