Sublime Forum

Misbehaving curly braces

#1

Hi all,
This is my first time posting here. So please ignore any mistakes that I make while writing this post.

I have been using sublime text 3 for almost a year now. But somehow, just recently the curly brace functionality in my sublime text started to misbehave.

Earlier when I used to type { at the cursor’s position, I used to get {} as usual.

But now, when I type {, I get the following:

Curly%20braces%202

As you can see, the closing brace now appears after 2 lines of code.

Can anyone please tell me how to correct this thing as it used to work normally earlier.

0 Likes

#3

It may be due to a misbehaving package. Which packages do you have installed?

0 Likes

#4

Actually, I have reinstalled sublime text and the problem seems to be fixed now. I had not installed any additional packages. I installed sublime via apt-get earlier and now as well.

0 Likes

#5

Sublime Text calls wrap_block if you type a { at the end of a line with "auto_match_enabled": true set in the Preferences.sublime-settings.

It tries to wrap a block of code into braces based on its indention level (only). This functionality comes with a set of edge cases, which might render it annyoing in several situations.

If the function was not confused by mixed spaces/tabs used for indention I can’t see a reason why it should have been triggered ass cerr has the same indention level as tests(t).

The command is implemented in the Default/block.py.

1 Like

#6

Thanks for your answer. But is there a way to disable the feature of putting the auto-complete bracket after some piece of text/code ? I mean what I want is that instead the auto-complete bracket gets put like this {} always. Is there some way to do this ?

0 Likes

#7

The Default/Default (Windows).sublime-keybinding contains a blocks like

	// Auto-pair curly brackets
	{ "keys": ["{"], "command": "insert_snippet", "args": {"contents": "{$0}"}, "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": "^(?:\t| |\\)|]|\\}|$)", "match_all": true }
		]
	},
	{ "keys": ["{"], "command": "wrap_block", "args": {"begin": "{", "end": "}"}, "context":
		[
			{ "key": "indented_block", "match_all": true },
			{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
			{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
			{ "key": "following_text", "operator": "regex_match", "operand": "^$", "match_all": true },
		]
	},

Note: Depending on OS, you might see slightly different key bindings as ST loads different preferences for different OS. If you work on macOs just replace Windows by OSX

Copying the first one to User/Default (Windows).sublime-keybinding should do the trick as it overwrites the block which triggers wrap_block.

	// Auto-pair curly brackets
	{ "keys": ["{"], "command": "insert_snippet", "args": {"contents": "{$0}"}, "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": "^(?:\t| |\\)|]|\\}|$)", "match_all": true }
		]
	},
1 Like

#8

Thanks a lot :smile:
That’ll solve the problem. But are there any side effects of it?

0 Likes

#9

Didn’t see any so far. Using that trick for … can’t remember how long.

1 Like