in vscode how can I quickly generate a new file with datetime in the name?
Asked Answered
I

3

1

I'm trying to be able to have a keyboard shortcut that creates a new file with the datetime as the prefix and some additional text that I enter.

I know there is a shortcut for generating a new file and I've seen snippets and extensions used to insert datetime into the editor but those extensions don't seem to work in the new filename dialog box.

Thanks!

Interjacent answered 16/3, 2020 at 1:31 Comment(1)
you can use Marks date shell command if your shell has it, or you can use Command Variable and use the commandVariable.dateTime command to construct a date-time string using JavaScript Intl.DateTimeFormat that uses LocalsWakayama
L
6

Try this. I'm using the bash shell so you may have to modify the shell commands for your shell.

In tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "newFile",
      "command": "touch `date +%Y%m%d-%H%M`-${input:fileName}.txt",

          // to create and open this new file use the following instead
      // "command": "touch `date +%Y%m%d-%H%M`-${input:fileName}.txt; code . `date +%Y%m%d-%H%M`-${input:fileName}.txt",

      "type": "shell",
      "problemMatcher": [],
      "presentation": {
        "echo": false,
        "reveal": "silent",
        "focus": false,
        "panel": "shared",
        "showReuseMessage": false,
        "clear": true
      },
      "promptOnClose": false
    }
  ],

  "inputs": [
    {
      "type": "promptString",
      "id": "fileName",
      "description": "Complete my file name.",
      "default": "new file name"                  // make your default text here
    }
  ]
}

I used the bash commands touch and date, if you are using a non-unix type shell you'll have to modify that for your similar create a file and add timestamp commands. And the file extension too (you could make that another promptString if you wish) - here jus hard-coded as .txt.

The task will create a new file with the timestamp as formatted followed by a pause for you to add the extra text you wanted to add. See task inputs.

The task could be run from the command palette Run task command or set a keybinding to run the task like this (in keybindings.json):

{
  "key": "alt+r",            // whatever keybinding you want
  "command": "workbench.action.tasks.runTask",
  "args": "newFile"
}

create a file with timestamp

unix date examples and more unix date formatting examples

Ligroin answered 16/3, 2020 at 3:47 Comment(4)
Thanks very much Mark, one follow up question - I noticed that while the file is created, vscode doesn't switch to make this the active editing window. Do you have any suggestions on how I would add that functionality to this task?Interjacent
any version for windows platform?Curitiba
@KevinAuds The above works on Windows machines running a bash-like shell. If you are using cmd.exe or powershell you will have to translate it into your shell commands.Ligroin
@Ligroin how to translate it into shell commands?Curitiba
C
1

maybe espanso a text expander could give some help.

Chappelka answered 7/10, 2020 at 15:43 Comment(0)
S
0

For Mac users, I have this line in my alias which copies the date into your clipboard. You can also add this to your Alfred scripts.

alias today='date '+%m-%d-%Y' | pbcopy'
Scepter answered 14/4, 2022 at 23:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.