I Have a problem like that
but answer in link above don't helped me. I get used to reindent whole file while coding and I liked Sublime Text very much. But this bug makes me mad. So I need to reindent whole PHP files with shortcut and don't get wrong indenting.
This is how i need:
And this is how it does:
As #BullfrogBlues mentions in a comment, the Sublime PHP Grammar plugin has a fix for this. I did not want that entire package, but fortunately it is very easy to extract out just the array indentation rule from there:
https://github.com/gerardroche/sublime-php-grammar/blob/master/Indentation%20Rules%20-%20Arrays.tmPreferences
Just save that file into the same directory where all your custom snippets etc. go (on Mac it's ~/Library/Application Support/Sublime Text 3/Packages/User, not sure about Windows/Linux) and voila!
The following solution was taken from SublimeText Forum.
Add this to your keybindings:
{ "keys": ["enter"], "command": "insert_snippet", "args": { "contents": "\n\t$0\n" }, "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "punctuation.section.array.end.php", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "array\\($", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
]
},
I'm using Sublime Text 3 and could solve this problem using the instructions on that page. I'm copying the same instructions here for reference, and I'll try to give some hints to find out why it isn't working for you.
First I've added those lines in my keybinding settings (Preferences -> Key bindings - User):
{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "Packages/User/Add Line in Braces.sublime-macro"}, "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\($", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
]
}
You must pay attention here that this settings file is a JSON-Array, and the above code must written in brackets:
[
// Copy above configuration here
]
If there are already some keybindings in your settings file, you must separate them with commas:
[
{
// Some existing keybindings
},
// Copy above configuration here
]
Then you should create a macro file in you user folder for Sublime Text 3. Where to find this folder depends on the operating system you are using. For instance, on Ubuntu it is:
~/.config/sublime-text-3/Packages/User
Create a new file in this folder, and name it (pay attention to cases and spaces):
Add Line in Braces.sublime-macro
In this file, copy the following script and save:
[
{"command": "insert", "args": {"characters": "\n\n"} },
{"command": "left_delete", "args": null},
{"command": "move", "args": {"by": "lines", "forward": false} },
{"command": "move_to", "args": {"to": "hardeol", "extend": false} },
{"command": "reindent", "args": {"single_line": true} }
]
This must be working, it works for me.
In sublime, if you type, alert("{<cursor> it will autocomplete the closing brackets and quotes to: alert("{<cursor>}").
In visual studio, if you hit tab, it will place the cursor at the end of the autocompleted characters.
How can I replicate this exact behaviour in sublime? I don't see much point in autocompletion if you have to type those characters anyway or use arrow keys.
Building on #AGS's answer and your comment, there are two possible options. The first (if you're not using OS X) is to just hit End, which will move the cursor to the end of the line (eol).
The second option is to slightly modify #AGS's keymap to the following:
{
"keys": ["shift+enter"], "command": "move_to", "args": {"to": "eol", "extend": false}, "context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\"\\]\\}\\$]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
}
The binds the eol functionality to ShiftEnter, and includes the regex support, which can be removed if you want.
I hope this helps!
Edit your .sublime-keymap file and add
// Skip past round and square autocomplete brackets
{
"keys": ["shift+enter"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\"\\]\\}\\$]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
},
In this case, shift + enter will function like tab in visual studio.
The solution is originally not mine - I found it either here or on the ST2 forum.
I'm trying to auto-close the asterisk (*) character in Markdown files.
I've been looking through all the language setting files, and am turning up nothing to use as an example. I've also tried writing a snippet, but found it inefficient (it doesn't wrap around the selection).
I searched around and found BracketHighlighter (which claims to allow custom auto-close pairings) but with no luck (installed through Package Control, also restarted).
Any ideas on where I should start or what I'm doing wrong?
Solution (thanks to #skuroda)
skuroda's answer will do fine - however, I've made a few tweaks that I would like to append to their answer:
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "$0**"}, "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\*\\*", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
}
Which adds two ** if the asterisk key is pressed next to two preceding asterisks (e.g. **| then ***| becomes **|** where | is the cursor. This helps a lot with emboldening text.
You may need to tweak the context some, but this should be a start. This is based on the auto pair key bindings for the built in brackets.
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*$0*"}, "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
},
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*${0:$SELECTION}*"}, "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
},
{ "keys": ["*"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\*", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
}
Use this
{ "keys": ["*"], "command": "insert_snippet", "args": {"name": "Packages/User/my-snippet.sublime-snippet" }}
now go to Preference>Browse Packages and then User folder
Create a file
my-snippet.sublime-snippet
and use following code inside
<snippet><content><![CDATA[
*${0:$SELECTION}*
]]></content></snippet>
Good Luck
Here's my version. It's also based on the built-in auto pair key bindings, but with some tweaks.
[
{ "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": "^(?:\\s|\\.|,|:|;|!|\\?|'|\"|‐|-|—|\\)|]|\\}|⟩|>|›|»|$)", "match_all": true },
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "\\S$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.plain, text.html.markdown", "match_all": true },
]
},
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*${0:$SELECTION}*"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.plain, text.html.markdown", "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": "selector", "operator": "equal", "operand": "text.plain, text.html.markdown", "match_all": true },
]
},
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\*$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\*", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.plain, text.html.markdown", "match_all": true },
]
},
]
Unlike the default, this version will:
make character pairs only when the caret is preceded by a whitespace character or beginning of a line (e.g. the default doesn't allow auto pairing after o, but does after ö or $ - this version excludes everything)
make pairs when the caret is followed by various characters (., :, !, " and more - the default only allows pairs to be made before a space, tab and some brackets)
only work in plain text Markdown files (.txt and .md)
I use this version for quotation marks too, replacing the default (of course allowing quotation marks globally).
Double characters
My version of double character functionality (similar to OP's/based on the default, but with a tweak):
[
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "$0**"}, "context":
[
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\*\\*", "match_all": true },
{ "key": "following_text", "operator": "not_regex_contains", "operand": "\\*", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.plain, text.html.markdown", "match_all": true },
]
},
]
This will make double characters, in this case asterisks (*), if there are already two consecutive asterisks anywhere in the line preceding the caret.
The tweak allows skipping asterisks (one at a time) when the caret is followed by any asterisk.
OP's version always adds two new asterisks if there are already two consecutive asterisks in the line preceding the caret, even if the caret is followed by an asterisk, which is undesired behaviour (for example just after adding a second double asterisk, pressing the asterisk key would add two more instead of skipping the new asterisks).
Explanations
From Sublime Text's default key bindings:
One rule states that in order to make a pair the caret must be preceded by lowercase letters, uppercase letters, numbers, the same symbol or an underscore:
{ "key": "preceding_text", "operator": ^"not_regex_contains", "operand": "[\"a-zA-Z0-9_]$"^, "match_all": true },
However, it still allow a pair if the caret is preceded by other letters or symbols (ö, ž, đ, ç, 蘋, Ψ, ?, ~, [, •, $, etc.)
It shouldn't do that so I tweaked it.
The following requires the caret to be preceded by a whitespace character or beginning of a line (it excludes all other symbols, not just basic letters and numbers):
"not_regex_contains", "operand": "\\S$"
It's a double negative to include beginnings of lines.
The following would only work after whitespace characters (space, tab, other) but NOT at the beginnings of lines:
"regex_contains", "operand": "\\s$"
Another rule states that in order to make a pair the caret must also be followed by certain characters: tab, space, ), ], }, >, end of line:
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
I thought that character pairing should also be allowed if other characters follow the caret, namely punctuation marks and other whitespace characters not included in the default.
The following includes more punctuation marks (., ,, :, ;, !, ?, ', ", ‐, -, —, ⟩, ›, ») and all whitespace characters:
"^(?:\\s|\\.|,|:|;|!|\\?|'|\"|‐|-|—|\\)|]|\\}|⟩|>|›|»|$)"
Note that a whitespace character preceding the caret is required, meaning that a pair would not be made when not wanted.
This would help in a situation where one wants to start writing formatted text at the end of a sentence but before the punctuation at the end of that sentence, for example (| represents the caret):
"This is a sentence|"
"This is a sentence |" // user types a space
"This is a sentence *|*" // User types a character and it gets a pair instead of staying single
"This is a sentence *with more words|*"
This question was asked several years ago, but hopefully my answer will be useful to everyone in the future who is interested.
Simple question, I just started coding with Sublime Text 2 on windows, and I want to change little things, like when I am writing an if statement such as if (i > 0), immediately after I type the "0", my cursor is between the "0" and ")", so if I hit enter, I want it to jump to after the ")". I am used to eclipse so I want to know how I can get the settings to mimic those of eclipse. I have tried editing the settings text files but couldn't find what I was looking for.
Try adding the following to your User key bindings (accessible through Preferences -> Key Bindings - User)
{ "keys": ["enter"], "command": "move", "args": {"by": "characters", "forward": true},
"context": [
{ "key": "selection_empty", "operator": "equal", "operand": true },
{ "key": "preceding_text", "operator": "not_regex_match", "operand": "[[:space:]]*", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^[\\)]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]}
edit: Update regex to only match parentheses.