WebStorm能否通过快捷键插入自定义实时模板?
在WebStorm中自定义快捷键插入实时模板(不破坏原代码)
我目前使用WebStorm开发,希望实现类似VS Code的功能:选中变量后按下Ctrl + L,就能在下一行插入指定的模板代码,且不破坏原有代码。试过自带的.log实时模板,但会修改原代码;也用过logit插件,但它仅支持单一模板,灵活性不够。
我在VS Code中是这样实现的:
// keybindings.json { "key": "ctrl+l", "command": "runCommands", "when": "editorHasSelection && editorTextFocus && !editorReadonly", "args": { "commands": [ "editor.action.clipboardCopyAction", "editor.action.insertLineAfter", { "command": "editor.action.insertSnippet", "args": { "snippet": "console.log('${1:$CLIPBOARD} :>>', ${1:$CLIPBOARD});" } } ] } }
WebStorm实现步骤
1. 创建自定义实时模板
- 打开设置:Ctrl + Alt + S(Windows/Linux)或Cmd + ,(Mac)
- 进入Editor > Live Templates
- 点击右上角「+」,选择Live Template
- 配置模板:
- 缩写(Abbreviation):随便填一个标识,比如
log-next - 模板文本(Template text):
console.log('$SELECTION$ :>>', $SELECTION$); - 点击「Edit variables」,确认
SELECTION变量已关联(默认会自动捕获选中内容) - 在「Applicable in」中勾选你需要的语言(比如JavaScript、TypeScript)
- 缩写(Abbreviation):随便填一个标识,比如
2. 录制组合动作宏
- 进入设置里的Keymap
- 右键空白处,选择「Add Macro」,命名为
Insert Log After Line - 点击「Record Macro」,依次执行:
- 按Shift + Enter在下一行插入新行并定位光标
- 按Ctrl + J调出实时模板菜单,输入刚才的缩写
log-next并回车
- 点击「Stop Recording」保存宏
3. 绑定快捷键
- 在Keymap中找到刚才创建的
Insert Log After Line宏 - 右键选择「Add Keyboard Shortcut」
- 按下Ctrl + L,确认无快捷键冲突后点击「OK」
这样设置后,选中变量按Ctrl + L,就能在下一行插入指定的日志模板,完全不影响原代码,且支持自定义模板内容。
内容的提问来源于stack exchange,提问作者Moriarty47
相关产品推荐
相关产品推荐

