首页 > 解决方案 > 当光标在括号中时按 Enter 时,如何禁用插入新的空行?

问题描述

重现步骤:

if () {
  
}
if () {
}

我希望不要插入一个空行。

可能是它默认工作(添加一个空行),当Alt+Enter它不添加一个空行时。

我没有在 vscode 中找到设置。我在谷歌上没有找到任何东西。


我试过这个:

{
  "key": "alt+enter",
  "command": "type",
  "args": { 
    "text": "\n" 
  },
  "when": "editorTextFocus"
}

因为Alt+Enter默认情况下什么都不做。但是,与该选项onEnterRules一起使用的函数会检测到字符的添加并添加一个额外的空行。:(editor.autoIndent\n

我想用editor.autoIndent. 但我想使用快捷方式关闭(不要打开)Alt+Enter


最糟糕的选择:寻找一个与 完全一样的扩展editor.autoIndent,但能够创建一个快捷方式Alt+Enter来按照我想要的方式工作。

标签: visual-studio-codevscode-settingsvscode-extensions

解决方案


您可以使用扩展多命令并构建一个执行您想要的命令。

将此添加到您的settings.json(全局或工作区)

  "multiCommand.commands": {
    "multiCommand.lineBreakNoEmptyline": {
      "sequence": [
        "lineBreakInsert",
        "deleteWordRight",
        "cursorRight",
        "cursorHome"
      ]
    }
  }

将此添加到您的keybindings.json

  {
    "key": "alt+enter",
    "command": "multiCommand.lineBreakNoEmptyline",
    "when": "editorTextFocus"
  }

或使用仅键绑定方法

  {
    "key": "alt+enter",
    "command": "extension.multiCommand.execute",
    "args": { 
      "sequence": [
        "lineBreakInsert",
        "deleteWordRight",
        "cursorRight",
        "cursorHome"
      ]
    },
    "when": "editorTextFocus"
  }

推荐阅读