如何防止Sublime Text 2吞噬闭合括号、引号和圆括号?

64

有时候在输入带有许多括号的结构时,Sublime会表现出一种非常烦人的行为。当你输入(时,它会添加()并将光标放在中间位置,这很好,但如果你接下来输入),它会悄悄地吞掉右括号。

这在输入长正则表达式时特别烦人,因为括号很快就会失衡,这让我抓狂。所以问题是 - 是否有一种方法可以禁用这个功能?如果我键入一个右括号,我希望它保留不被吞掉。

我已经检查了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。 - chuff
谢谢,但这是用于突出显示括号的。我试图找到的是当光标位于闭合括号上并且您正在输入它时,防止丢失括号的方法。 - firedev
2
一个解决该问题的替代方法是暂时关闭括号匹配,例如:在使用正则表达式时。这个答案就是这样做的:http://superuser.com/questions/392200/turn-off-parenthesis-matching-in-sublime-text-2/482898#482898 - Fabien Snauwaert
这真是太简单了!:O - Manu
3个回答

50

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

{ "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 }
    ]
},

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


好的,我可以为方括号和花括号重复一遍。但是对于双引号和单引号,我该怎么办? - firedev
我刚刚编辑了我的答案。我实际上是查看了默认键绑定文件,并复制了每个按键中出现一个“)”字符时会触发“move”命令的部分内容。您可以通过搜索“Auto-pair”来查找这些部分。然后,我只需将“move”命令更改为一个插入适当字符的“插入”命令即可。 - basilikum
这个方法是可行的,但有一个需要注意的地方,你认为是否可能让它在紧跟着开括号后面的闭括号被忽略掉呢?这样当我输入 function() 时就不会变成 function()) 了。就像在文本前面放置开括号时,它不会自动添加闭括号一样。 - firedev
非常感谢您。在找到您的答案之前,我不知道我需要它。这让我的一天变得更美好了。 - Bite code
我的问题是:为什么Sublime Text 3不修复显然存在的错误? - Argent

8
重新定义)键绑定:
{ "keys": [")"], "command": "insert", "args": {"characters": ")"} }

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

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

但是那样我就必须重新定义所有的键)} ]" '。还有其他方法吗? - firedev
6
另一种方法是完全禁用自动配对,将 auto_match_enabled 改为 false - dusan
抱歉,禁用自动匹配将禁止在我输入开括号时添加闭括号。我对此没有问题,但我希望能够键入一个闭括号并看到它,因为自动匹配有时不起作用。简而言之 - 我希望Sublime的行为像Textmate一样。 - firedev
抱歉,但这是不正确的,关闭自动匹配会禁用所有功能。 - firedev
没错。我想停止配对引号,但是我想保留配对大括号。禁用自动匹配并不是完美的解决方案。 - fIwJlxSzApHEZIl

-2
我在浏览键绑定文件“preferences/key bindings - default”时发现,如果您选择一些文本并键入其中任何一个字符({[),它将在您的文本周围放置括号。

当然,但是如果您在括号内输入一些文本并按 ) 键,则不会在末尾添加额外的括号(如果已经存在)。 - firedev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接