You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code中设置快捷键,新建指定语言模式或继承扩展名的无标题文件?

VS Code 快捷键设置方案

一、设置快捷键打开特定语言模式的无标题文件

  1. 打开快捷键配置文件:按下 Ctrl+Shift+P,输入 Preferences: Open Keyboard Shortcuts (JSON) 并回车。
  2. 在打开的 keybindings.json 中添加自定义规则,示例如下(以打开Python无标题文件为例):
{
    "key": "ctrl+shift+alt+n", // 自定义快捷键,可按需修改
    "command": "workbench.action.files.newUntitledFile",
    "args": {
        "languageId": "python" // 指定目标语言ID,比如"javascript"、"markdown"等
    },
    "when": "editorTextFocus" // 仅在编辑器有焦点时触发,可调整条件
}
  1. 保存文件后,按下设置的快捷键即可直接打开对应语言模式的无标题文件。

二、让Ctrl+N新建文件继承当前文件的语言模式

需要借助 multi-command 扩展实现:

  1. 在VS Code扩展商店搜索并安装 multi-command。
  2. 打开设置配置文件:按下 Ctrl+Shift+P,输入 Preferences: Open Settings (JSON) 并回车。
  3. 添加宏配置:
"multiCommand.commands": [
    {
        "command": "multiCommand.newUntitledWithCurrentLang",
        "sequence": [
            "workbench.action.files.newUntitledFile",
            {
                "command": "editor.action.changeLanguageMode",
                "args": {
                    "languageId": "${activeEditorLanguage}"
                }
            }
        ]
    }
]
  1. 再次打开 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 14:07:12