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

旧巷老猫 提交于 2020-04-07 09:18:14

问题


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!


回答1:


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",
      "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"
}

unix date examples and more unix date formatting examples



来源:https://stackoverflow.com/questions/60699348/in-vscode-how-can-i-quickly-generate-a-new-file-with-datetime-in-the-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!