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

如何自定义VS Code中Ctrl + Delete的删除行为?

实现VS Code中Ctrl+Delete的自定义删除行为

可以通过自定义快捷键绑定实现需求,具体步骤如下:

  1. 打开VS Code的快捷键配置文件

    • 按下Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入Preferences: Open Keyboard Shortcuts (JSON)并回车,打开keybindings.json文件。
  2. 添加自定义快捷键规则
    将以下代码粘贴到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$'))"
  }
]
  1. 保存配置并测试
    保存keybindings.json后回到编辑器验证:
    • 当光标后只有一个空格(如[光标] Hello),按下Ctrl+Delete会仅删除空格,变为Hello;
    • 当光标后有两个及以上空格(如[光标] Hello),按下Ctrl+Delete会删除所有空格及后续单词,保持原有行为。

内容的提问来源于stack exchange,提问作者아이엠초코

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 02:48:18