Lets say I have a shortcut that fires a macro to add curly braces after a function. But my curly braces are different for php than for javascript:
php:
function()
{
.....
}
javascript:
function(){
.....
}
Is there a way to have the same key binding for a macro that behaves different depending on the syntax I am writing in?
You can bind multiple commands (or macros in this case) to the same key and differentiate them with context entries.
You will probably want to define the context as
{ "key": "selector", "operator": "equal", "operand": "source.js", "match_all": true }
and
{ "key": "selector", "operator": "equal", "operand": "source.php", "match_all": true }
Related
So I have a text for example "this is sample text" and I want it to become this "<![CDATA[this is sample text]]>"
I want to work this function like this: I select any text and use hotkey like ctrl+t or so.
I use Sublime Text 3.
How can I do this for the selection of text?
You can create a new keybinding to use the insert_snippet command to wrap your selection. (Preferences menu -> Keybindings)
{ "keys": ["ctrl+t"], "command": "insert_snippet", "args": {"contents": "<![CDATA[${0:$SELECTION}]]>"}, "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
I'm trying to get "TAB" to exit parenthesis, quotes, etc. but only when it is NOT the only character on the line. If | represents the cursor:
Exit here:
function(e|) {}
But not here:
function() {
return;
|}
To do so I'm trying to set the context to check for that condition without any success. Here is what I have so far:
{ "keys": ["tab"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)'}\"\\]]", "match_all": true },
{ "key": "preceding_text", "operator": "not_regex_match", "operand": "\\n", "match_all":true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
}
The second line of the context is what I can't get right. Any help would be greatly appreciated!
Best,
Michael
Since you want to match until the line start you should use ^. The regex match is only per line, hence you can use ^ for the begin and $ for the end of the line.
Change it to ^\\s* if you also want to disable it for lines with indent, e.g. to press the tab multiple times to indent the bracket.
(Google turns up this http://www.sublimetext.com/forum/viewtopic.php?f=2&t=14005 but that answer isn't satisfactory to me)
In Sublime Text 2, when you hit tab:
If you have nothing selected it will add a tab (or spaces) at your cursor location. This is good.
If you have multiple lines selected it will indent them all. This is good.
If you have a selection within a single line it will replace this text with a tab. This isn't BAD exactly, but it's not what I want. I'd rather it indents that line, as with multiple lines selected.
I'd think the way to do this would be to create a keyboard shortcut that activates if you have text selected when you hit tab, that "command": "indent"s, but I can't figure out how to say "if you have text selected". The keymap documentation seems to be somewhere between impenetrable and nonexistent....
Does anyone know how to get Sublime Text 2 to do what I want?
It's worth noting that if you select text that doesn't include a newline with multiple cursors it replaces the text with a tab.
With that in mind, putting this in my keymap does what I want:
{ "keys": ["tab"], "command": "indent", "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]
}
Thank you ! It works fine. Additionaly, you should want the same principle for unindent (shift+tab) :
{ "keys": ["shift+tab"], "command": "unindent", "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]
}
Question
What's the proper way to create a key binding for a specific language?
Background
I'd like to insert a semi-colon after each line automatically when working on java files. I've created a macro to accomplish and have been able to bind it to super+enter. Now I'd like to scope the key binding to just java files. What am I doing wrong?
[
{
"keys": ["super+enter"], "command": "run_macro_file",
"args": {"file": "Packages/User/Add Line SemiColon.sublime-macro"},
"context": [
{ "key": "selector", "operator": "equals", "operand": "source.java" }
]
}
]
You're going to love this—the comparison operator that you're looking for isn't equals, it's equal:
Context Operators
equal, not_equal— Test for equality.
regex_match, not_regex_match— Match against a regular expression.
regex_contains, not_regex_contains— Match against a regular expression (containment).
Change that, and you shouldn't have any more trouble.
In the Default (OSX).sublime-keymap file, I see:
{ "keys": ["ctrl+space"], "command": "auto_complete" },
{ "keys": ["ctrl+space"], "command": "replace_completion_with_auto_complete", "context":
[
{ "key": "last_command", "operator": "equal", "operand": "insert_best_completion" },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false },
{ "key": "setting.tab_completion", "operator": "equal", "operand": true }
]
},
But I'd like it to function more like Eclipse, where methods available get popped up after hitting . and waiting for a split second. Is this possible?
Keep in mind that Sublime Text is not an IDE. As such, Eclipse like completion (where it only brings up methods that you can call for the particular variable) is not built in. You may try something like SublimeJava for completions more like Eclipse. I have used it a bit, with mixed results. It did not work as well as Eclipse, but for what I was doing, it was acceptable. To bring up your current set of completions (from within the file by default) when you press ".", you may add the following to your User Preferences (accessible through Preferences -> Settings - User).
"auto_complete_triggers": [{"selector": "source.java", "characters": "."}]
This will cause the auto complete pop up to be displayed when ever you enter . in java files.