In Sublime Text 2 (or 3), is there a keyboard shortcut of some sort (tab? enter?) to go from the cursor denoted by |:
$var->catch('someName|');
to
$var->catch('someName');|
or
$var->catch('someName');
|
For your first scenario, you can modify this answer slightly: https://stackoverflow.com/a/19957050/1569064 (note the addition of the single quote, ', and the semi-colon ;, in the operand values
// Move to end of line
{
"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 }
]
},
// Move to next line
{
"keys": ["ctrl+shift+enter"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": true}, "context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\"';\\]\\}\\$]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
}
My answer for your second example works when there is an empty line below. Otherwise, it moves the cursor to the end of that next, non-empty line.
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.