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

VSCode中同一快捷键Ctrl+L在blade.json与javascript.json中实现不同功能

实现VSCode按Ctrl+L在对应文件插入调试代码

1. 配置全局快捷键(keybindings.json)

打开快捷键配置文件(Ctrl+Shift+P → 选择"Preferences: Open Keyboard Shortcuts (JSON)"),添加以下配置:

[
    {
        "key": "ctrl+l",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus && editorLangId == blade",
        "args": {
            "name": "Blade Debug dd"
        }
    },
    {
        "key": "ctrl+l",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus && editorLangId == javascript",
        "args": {
            "name": "JS Debug console.log"
        }
    }
]

2. 配置Blade代码片段(blade.json)

打开Blade代码片段文件(Ctrl+Shift+P → 选择"Preferences: Configure User Snippets" → 选择blade.json),添加:

{
    "Blade Debug dd": {
        "prefix": "",
        "body": [
            "",
            "dd(${TM_CURRENT_LINE/^\\s*(\\$\\w+).*/$1/});"
        ],
        "description": "Insert dd() with current line variable below"
    }
}

该片段会自动提取当前行的变量(比如$fruit = mango;里的$fruit),在下方新行插入dd($fruit);。

3. 配置JavaScript代码片段(javascript.json)

打开JS代码片段文件(Ctrl+Shift+P → 选择"Preferences: Configure User Snippets" → 选择javascript.json),添加:

{
    "JS Debug console.log": {
        "prefix": "",
        "body": [
            "",
            "console.log(${TM_CURRENT_LINE/^\\s*(?:const|let|var)\\s*(\\w+).*/$1/});"
        ],
        "description": "Insert console.log() with current line variable below"
    }
}

该片段会匹配const/let/var声明的变量(比如var fruit = 'mango';里的fruit),在下方新行插入console.log(fruit);。

说明

  • 快捷键的when条件通过editorLangId区分文件类型,确保Ctrl+L在Blade和JS文件触发不同的片段。
  • 代码片段里的正则表达式${TM_CURRENT_LINE/正则/替换/}用来从当前行提取变量名,适配常见的变量声明格式。

内容的提问来源于stack exchange,提问作者Imtiaze

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 23:09:22