VS Code Snippets

Writing custom snippets in VS Code

I’ve worked with computers for a couple of decades now, and I love Notepad… #sorrynotsorry, vim fans. It’s fantastic for writing - there’s no fancy GUI needing customization to fit your workflow, and offers just enough structure that it’s usable for a variety of tasks. It’s like the pre-markdown version of markdown. My favourite feature of Notepad is the ability to insert the date & time via. F5. This is a brilliant and elegant (and often overlooked) feature. Word’s insert date/time function pales in comparison to the elegant simplicty of F5. Even trying to use keyboard shortcuts isn’t fast, when it should be.

Notepad Word GUI Work shortcuts
F5 click Insert ALT+I
click Date/Time D
read the menu read the menu
double-click choice UP/DOWN ARROW to choice
ENTER

When I write Powershell scripts in VS Code, I had been manually typing in the date until I decided to write a custom snippet that would insert a header with all the things I normally add: script name, date/time, comments, my name. It’s automatically populated whenever I type in header <ENTER>

  1. Open the Command Palette (View, Command Palette…. or CTRL+SHIFT+P)
  2. Select Preferences: Configure User Snippets
  3. Select Powershell from the list
  4. Add the snippet:
     "File Header": {
        "prefix": "header",
        "description": "Output a file header with the file name, comment field, author and date",
        "body": [
            "#$TM_FILENAME",
            "",
            "<#",
            "",
            "",
            "Greg Beifuss",
            "$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE",
            "#>",
            "",
            "",
        ]
    }
    

Since I’m using VS Code to write markdown now, I decided to do a similar thing to reproduce Notepad’s F5 feature:

  1. Open the Command Palette (View, Command Palette…. or CTRL+SHIFT+P)
  2. Select Preferences: Configure User Snippets
  3. Select Markdown from the list
  4. Add the snippet:
    "Current Date and Time": {
        "prefix": "datetime",
        "description": "Add Current date & time",
        "body": [
            "$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE"
        ]
    }
    

This snippet seems to be more tempermental - when I type in datetime <ENTER>, usually it’s not replaced with the date/timestamp. I resort to:

1. CTRL+I
2. datetime <ENTER>
Built with Hugo
Theme Stack designed by Jimmy