【问题标题】:How to prevent Sublime Text 2 from swallowing closing brackets, quotes and parentheses?如何防止 Sublime Text 2 吞下右括号、引号和圆括号?
【发布时间】:2012-12-11 12:10:17
【问题描述】:

Sublime 有这种行为,有时当你必须输入带有大量括号的结构时,这真的很烦人。当您输入( 时,它会添加() 并将光标放在中间,一切都很好,但是如果您输入),它会默默地吞下右括号。

在输入长正则表达式时这真的很烦人,因为括号很快就会变得不平衡,这让我发疯。所以你最终会得到像(([a-z]) 这样的结构。

所以问题是 - 有没有办法禁用它?如果我输入一个右括号,我希望它留下来,而不是被吞掉。

我已经检查过 Sublime 配置,谷歌搜索,但似乎没有人介意这种行为。我用错了吗?

更新

您可能还想查看Sublime: Jump out of matching brackets 快捷方式。

完整版允许您使用() 输入,但如果您输入了任何文本,则不会吞下结束符号:

  { "keys": ["\""], "command": "insert", "args": {"characters": "\""}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^\"", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "[^\"]$", "match_all": true }
      ]
  },
  { "keys": [")"], "command": "insert", "args": {"characters": ")"}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "[^(]$", "match_all": true }
      ]
  },
  { "keys": [")"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "\\($", "match_all": true }
      ]
  },
  { "keys": ["'"], "command": "insert", "args": {"characters": "'"}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^'", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "'$", "match_all": true }
      ]
  },
  { "keys": ["]"],"command": "insert", "args": {"characters": "]"}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "[$", "match_all": true }
      ]
  },
  { "keys": ["}"], "command": "insert", "args": {"characters": "}"}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "{$", "match_all": true }

      ]
  }

【问题讨论】:

  • 虽然我不熟悉它,但这篇 SO 帖子提供了一些信息。在 BracketHighLighter 插件上,这可能会提供一些缓解:stackoverflow.com/questions/10372004/how-to-change-style-of-matched-brackets-in-sublime-text-2。
  • 谢谢,但这是为了突出括号。我试图找到的是当光标位于右括号并且您正在输入时防止括号丢失的方法。
  • 解决该问题的另一种方法是暂时关闭括号匹配 - 例如:在使用 RegEx 时。这个答案就是这样做的:superuser.com/questions/392200/…
  • 就是这么简单! :O

标签: autocomplete sublimetext2 editing brackets


【解决方案1】:

将此添加到您的用户键绑定文件

{ "keys": [")"], "command": "insert", "args": {"characters": ")"}, "context":
    [
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
    ]
}

它将覆盖一个键绑定,而不是插入右括号,只是将光标向前移动一个位置。所以本质上它应该做你想做的事。

如果你想完全禁用这种行为,对于各种括号和引号,这里是完整的用户键绑定部分:

{ "keys": ["\""], "command": "insert", "args": {"characters": "\""}, "context":
    [
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\"", "match_all": true }
    ]
},
{ "keys": [")"], "command": "insert", "args": {"characters": ")"}, "context":
    [
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
    ]
},
{ "keys": ["'"], "command": "insert", "args": {"characters": "'"}, "context":
    [
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^'", "match_all": true }
    ]
},
{ "keys": ["]"],"command": "insert", "args": {"characters": "]"}, "context":
    [
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
    ]
},
{ "keys": ["}"], "command": "insert", "args": {"characters": "}"}, "context":
    [
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
    ]
}

编辑:

如果您想跳过右括号(如果光标正好位于左括号之后)并在所有其他情况下打印它,您可以拆分您的键绑定以区分这两种可能性:

{ "keys": [")"], "command": "insert", "args": {"characters": ")"}, "context":
    [
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
        { "key": "preceding_text", "operator": "regex_contains", "operand": "[^(]$", "match_all": true }
    ]
},
{ "keys": [")"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
    [
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
        { "key": "preceding_text", "operator": "regex_contains", "operand": "\\($", "match_all": true }
    ]
},

如果前面的文本不以左括号结尾,则第一个插入字符。如果第二个以左括号结束,则将光标向前移动一个位置。如果您对正则表达式有点熟悉,您可以对所有其他类型的括号和引号执行相同的操作。

【讨论】:

  • 好的,我可以重复方括号和花括号。但是双引号和单引号应该怎么做呢?
  • 我刚刚编辑了我的答案。顺便说一句,我做了什么:我只是查看了默认的键绑定文件并复制了每个部分,例如“)”的击键导致“移动”命令。您可以通过搜索“自动配对”找到这些部分。然后我只是将“移动”命令更改为插入适当字符的“插入”命令。
  • 这行得通,但是有一个警告,如果它就在开始括号之后,你认为有可能让它吞下右括号吗?所以当我输入 function() 时,它不会以 function()) 结尾?有点像如果您在某些文本之前放置一个左括号,它不会添加右括号。
  • 哇,这太棒了!谢谢。
  • 非常感谢。在真正找到您的答案之前,我不知道我需要它。这让我很开心。
【解决方案2】:

重新定义)键绑定:

{ "keys": [")"], "command": "insert", "args": {"characters": ")"} }

编辑:另一种方法是启用/禁用auto_match_enabled 设置(从而更改自动配对行为),您可以使用键盘快捷键随意切换:

{ "keys": ["alt+m"], "command": "toggle_setting", "args": {"setting": "auto_match_enabled"} }

【讨论】:

  • 但是我必须重新定义所有键) } ] " '。还有其他方法吗?
  • 另一种方法是完全禁用自动配对,将auto_match_enabled更改为false
  • 对不起,禁用自动匹配将禁用在我输入左括号时添加右括号。我对此没有问题,但我希望能够键入一个关闭的并查看它,因为自动匹配有时不起作用。简而言之 - 我希望 Sublime 表现得像 Textmate。
  • 抱歉,这是不正确的,关闭自动化会禁用一切。
  • 没错。我想停止配对引号,但我想继续配对大括号。禁用自动化并不是完美的解决方案。
【解决方案3】:

我在浏览键绑定文件“首选项/键绑定 - 默认”时发现,如果您选择一些文本并输入其中任何一个({[。它将在您的文本周围放置括号。

【讨论】:

  • 当然可以,但是当你在括号内输入一些文本并按) 之后,如果已经有一个括号,它就不会在最后添加额外的括号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 2021-09-15
  • 2021-05-01
  • 2011-01-12
  • 1970-01-01
相关资源
最近更新 更多