How to prevent Sublime Text 2 from swallowing closing brackets, quotes and parentheses?

后端 未结 3 1925
闹比i
闹比i 2020-12-23 09:08

Sublime has this behaviour which is really annoying sometimes when you have to type in constructions with lots of brackets. When you type ( it adds ()

相关标签:
3条回答
  • 2020-12-23 09:38

    Redefine the ) key binding:

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

    Edit: Another way is to enable/disable the auto_match_enabled setting (thus changing the auto-pairing behavior), you can toggle it at will using a keyboard shortcut:

    { "keys": ["alt+m"], "command": "toggle_setting", "args": {"setting": "auto_match_enabled"} }
    
    0 讨论(0)
  • 2020-12-23 09:39

    add this to your user keybindings file

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

    it will override the one keybinding that instead of inserting a closing bracket just moves the cursor one position forward. so essentially it should do exactly what you want.

    if you want to disable this behaviour completely, for all kinds of brackets and quotes, here is the complete user keybindings part:

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

    EDIT:

    In case you want to skip the closing bracket if the cursor is right after an opening bracket and print it in all other cases, you can split your keybindings up to distinguish beetween these two possibilities:

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

    The first one inserts the charcater if the preceding text doesn't end with an opening bracket. The second one moves the cursor one position forward if it does end with an opening bracket. If you are a little familiar with regular expressions you can do the same for all other kinds of brackets and quotes.

    0 讨论(0)
  • 2020-12-23 09:53

    I found out when browsing the keybindings file 'preferences/key bindings - default' that if you select some text and type any of these ({[. It will place the brackets round your text.

    0 讨论(0)
提交回复
热议问题