在vscode中,如何快速生成名称为datetime的新文件? [英] in vscode how can I quickly generate a new file with datetime in the name?

查看:441
本文介绍了在vscode中,如何快速生成名称为datetime的新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拥有一个键盘快捷键,该快捷键可以创建一个以日期时间为前缀的新文件,并输入一些其他文本.

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.

谢谢!

推荐答案

尝试一下.我正在使用bash shell,因此您可能必须修改shell的shell命令.

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

在tasks.json中:

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
    }
  ]
}

我使用了bash命令touchdate,如果您使用的是非Unix类型的Shell,则必须对其进行修改,以类似的方式创建文件并添加时间戳命令.以及文件扩展名(如果需要,您可以将其设置为另一个提示符字符串)-在这里强制将其编码为.txt.

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.

可以从命令面板Run task命令运行该任务,也可以设置键绑定以运行这样的任务(在keybindings.json中):

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日期示例 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆