ATL*_*DEV 4 macros visual-studio-code vscode-tasks
我处理大型 html 文件,我想将它们分成单独的文件。这样做的过程非常繁琐,因为它需要复制代码,创建一个新文件,将其粘贴到新文件中,然后选择一个文件夹和一个新名称来保存它。
VS Code 是否有内置的快捷方式、宏或扩展来简化此操作?
请注意,由于这是一个 html 文件,因此新的重构Move to a new file不可用。它可以满足您的需求,并且可以在其他一些语言(如 js/ts)中工作,但不能在 html 中工作。您可以通过选择要移动的文本并在上下文菜单中选择来访问它Refactor..- 如果支持,它甚至可以将导入语句添加到旧文件中。
runCommands这使用可以运行多个命令的内置命令。以下键绑定将获取选定的文本,将其粘贴到新的无标题文件中,该saveAs命令将提示您输入文件名。
在 keybindings.json 中:
{
"key": "alt+q",
"command": "runCommands",
"args": {
"commands": [
// choose which one you want
"editor.action.clipboardCutAction",
// "editor.action.clipboardCopyAction",
"workbench.action.files.newUntitledFile",
"editor.action.clipboardPasteAction",
// prompt for save immediately
"workbench.action.files.saveAs",
]
},
"when": "editorFocus"
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何自动执行问题的“选择文件夹和新名称以将其保存在”部分。我认为您仍然需要手动执行此操作,但在“另存为”对话框中有一些方便的“智能感知”。
2020年更新
当我想出这个答案后,在 vscode 中查看如何快速生成名称中包含日期时间的新文件?
我认为可能有一种更好的方法来处理通过任务创建文件并一次性提示输入文件夹和文件名。您会失去saveAs文件夹结构的智能感知,但无论如何,这是一种非常好的技术。并且不需要宏。在外壳中bash:
{
"version": "2.0.0",
"tasks": [
{
"label": "newFile",
// assuming your folder name isn't static
"command": "echo '${selectedText}' > ${input:folderName}/${input:fileName}.html",
"type": "shell",
"problemMatcher": [],
"presentation": { // terminal handling which you may not care about and could delete
"echo": false,
"reveal": "silent",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": true
},
"promptOnClose": false
}
],
"inputs": [
{
"type": "promptString",
"id": "folderName",
"description": "Complete my folder name.",
"default": "folder"
},
{
"type": "promptString",
"id": "fileName",
"description": "Complete my file name.",
"default": "new file name"
}
]
}
Run Code Online (Sandbox Code Playgroud)
运行该任务的一些按键绑定(或者只是从命令选项板Run Task命令运行它):
{
"key": "alt+r", // whatever you choose
"command": "workbench.action.tasks.runTask",
"args": "newFile"
},
Run Code Online (Sandbox Code Playgroud)
就这样,选择文本,然后运行任务Alt+ R。
| 归档时间: |
|
| 查看次数: |
3528 次 |
| 最近记录: |