0

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?

  • with extension **Hypersnips** you can use JavaScript to create the content of the snippet – rioV8 Oct 15 '22 at 09:10
  • @rioV8 Thank you! I need this extension more. By the way, is it possible to run other language (like python or r) by this extension? – CCCCCC666666 Oct 16 '22 at 04:59

2 Answers2

1

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

Mark
  • 143,421
  • 24
  • 428
  • 436
0

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.

rioV8
  • 24,506
  • 3
  • 32
  • 49