skip to Main Content

In RStudio, there is a snippet as follows:

snippet ts
    `r paste("#", date(), "------------------------------n")`

It will pastes some texts by running date(),
for example,
# Sat Oct 15 11:04:22 2022 ------------------------------.


How to define this in VScode?

2

Answers


  1. With extension File Templates you can create a key binding that insert a template with date variables.

    Create a date format you want and the template that uses that date format.

    Login or Signup to reply.
  2. Here is a way to do this in any type of file. You will need this extension (that I wrote), Find and Transform. Make this keybinding (in your keybindings.json):

    {
      "key": "alt+d",
      "command": "findInCurrentFile",
      "args": {
        
        "replace": [
          "$${",
            "const date = new Date();",
            "const options = { weekday: 'short', month: 'short', year: 'numeric', day: 'numeric',  hour: 'numeric',  minute: 'numeric',  second: 'numeric'};",
            "let str = `${LINE_COMMENT} ` +  new Intl.DateTimeFormat('en-US', options).format(date) + ' ';",
            
            "return str.padEnd(70, '-');",    // padEnd with ---, whatever final length you want
            
          "}$$"
        ],
        "restrictFind": "line",
        "postCommands": "cancelSelection"
      },
      // "when": "editorLangId == r"
    }
    

    You can use any of the Intl.DateTimeFormat options.

    Note the use of ${LINE_COMMENT} at the beginning of the string. That will resolve to the comment style of whichever language you are in at the time. The demo is in an r file.

    demo of keybinding to add a time stamp with comment and padding

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search