如何在VS Code中设置快捷键,新建指定语言模式或继承扩展名的无标题文件?
VS Code 快捷键设置方案
一、设置快捷键打开特定语言模式的无标题文件
- 打开快捷键配置文件:按下
Ctrl+Shift+P,输入Preferences: Open Keyboard Shortcuts (JSON)并回车。 - 在打开的
keybindings.json中添加自定义规则,示例如下(以打开Python无标题文件为例):
{ "key": "ctrl+shift+alt+n", // 自定义快捷键,可按需修改 "command": "workbench.action.files.newUntitledFile", "args": { "languageId": "python" // 指定目标语言ID,比如"javascript"、"markdown"等 }, "when": "editorTextFocus" // 仅在编辑器有焦点时触发,可调整条件 }
- 保存文件后,按下设置的快捷键即可直接打开对应语言模式的无标题文件。
二、让Ctrl+N新建文件继承当前文件的语言模式
需要借助 multi-command 扩展实现:
- 在VS Code扩展商店搜索并安装
multi-command。 - 打开设置配置文件:按下
Ctrl+Shift+P,输入Preferences: Open Settings (JSON)并回车。 - 添加宏配置:
"multiCommand.commands": [ { "command": "multiCommand.newUntitledWithCurrentLang", "sequence": [ "workbench.action.files.newUntitledFile", { "command": "editor.action.changeLanguageMode", "args": { "languageId": "${activeEditorLanguage}" } } ] } ]
- 再次打开
keybindings.json,覆盖默认的Ctrl+N快捷键:
{ "key": "ctrl+n", "command": "multiCommand.newUntitledWithCurrentLang", "when": "editorTextFocus" }, { "key": "ctrl+n", "command": "workbench.action.files.newUntitledFile", "when": "!editorTextFocus" }
这样设置后,当你在编辑某个文件时按下 Ctrl+N,新建的无标题文件会自动切换为当前文件的语言模式;若没有打开任何编辑器,则保持默认新建行为。
内容的提问来源于stack exchange,提问作者Bryan Menoscal
相关产品推荐
相关产品推荐

