如何自定义VS Code中Ctrl + Delete的删除行为?
实现VS Code中Ctrl+Delete的自定义删除行为
可以通过自定义快捷键绑定实现需求,具体步骤如下:
打开VS Code的快捷键配置文件
- 按下
Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入Preferences: Open Keyboard Shortcuts (JSON)并回车,打开keybindings.json文件。
- 按下
添加自定义快捷键规则
将以下代码粘贴到keybindings.json中,替换原有的ctrl+delete绑定(如果存在):
[ // 当光标后仅有一个空格时,仅删除该空格 { "key": "ctrl+delete", "command": "editor.action.deleteRight", "when": "editorTextFocus && !editorReadonly && regexTest(editor.document.getText(range(editor.selection.active, editor.selection.active.translate(0,1))), '^\\s$') && !regexTest(editor.document.getText(range(editor.selection.active.translate(0,1), editor.selection.active.translate(0,2))), '^\\s$')" }, // 其他情况保持原有删除行为 { "key": "ctrl+delete", "command": "editor.action.deleteWordRight", "when": "editorTextFocus && !editorReadonly && !(regexTest(editor.document.getText(range(editor.selection.active, editor.selection.active.translate(0,1))), '^\\s$') && !regexTest(editor.document.getText(range(editor.selection.active.translate(0,1), editor.selection.active.translate(0,2))), '^\\s$'))" } ]
- 保存配置并测试
保存keybindings.json后回到编辑器验证:- 当光标后只有一个空格(如
[光标] Hello),按下Ctrl+Delete会仅删除空格,变为Hello; - 当光标后有两个及以上空格(如
[光标] Hello),按下Ctrl+Delete会删除所有空格及后续单词,保持原有行为。
- 当光标后只有一个空格(如
内容的提问来源于stack exchange,提问作者아이엠초코
相关产品推荐
相关产品推荐

