VS代码:在编辑器当前目录内创建文件

问题描述 投票:0回答:1

是否可以在按下explorer.newFile的键盘快捷键ctrl + n时,在与编辑器中处于活动状态的文件相同的文件夹中创建一个新文件?

我可以通过查看面包屑来判断它是否是正确的文件夹,并且大多数情况下已关闭文件浏览器。但是,该命令似乎在文件浏览器打开或聚焦的最后一个文件夹内创建一个文件。

visual-studio-code vscode-settings vscode-tasks
1个回答
0
投票

您可以通过重载explorer.newFile命令的默认键绑定来运行任务来实现。这是键盘绑定:

{
  "key": "ctrl+n",
  "command": "workbench.action.tasks.runTask",
  "args": "newFile",
  "when": "editorFocus"  // must have an editor focused
},

和任务(在tasks.json中:)>

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",

  "tasks": [
    {
      "label": "newFile",

      "command": "touch '${fileDirname}/${input:fileName}' && code '${fileDirname}/${input:fileName}'",

      "type": "shell",
      "problemMatcher": [],
    },
   ],

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

将在与活动编辑器目录相同的目录中创建一个文件-它将询问您文件名-然后打开它。

我使用bash命令touch创建文件(如果尚不存在),如果您使用的是其他Shell,则需要使用touch等效文件。

© www.soinside.com 2019 - 2024. All rights reserved.