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.
Related
Quick Markup (https://github.com/redmunds/brackets-quick-markup) is an extension available for Brackets IDE for HTML development. The extension can be activated with a shortcut Ctrl+Alt+M. When it is active, we can use custom shortcuts to insert custom HTML tags instead of typing them. For example, to make insert <b></b> tags around a word, we just need to select the word and hit Ctrl+B. The shortcuts and their corresponding tags can be customized.
I'm looking for a similar extension or feature like this in Visual Studio Code. I tried searching but couldn't find anything so far.
keybindings.json
{
"key": "ctrl+b",
"command": "editor.emmet.action.wrapWithAbbreviation",
"when": "editorHasSelection",
"args": {
"abbreviation": "b"
}
},
{
"key": "ctrl+shift+b",
"command": "editor.emmet.action.wrapWithAbbreviation",
"when": "editorHasSelection",
"args": {
"abbreviation": "pre>code"
}
},
You can also call it without args and just type the tags every time.
Or just typing anything:
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "<br />"
}
}
(CTRL + SHIFT + F) it's really a nice shortcut to keep code well organized in eclipse. Is there any thing equivalent for sublime text editor?
Unfortunately there isn't a default key binding equivalent to CTRL + SHIFT + F in eclise. However, there is the reindent command that can be used to make your own key binding.
Open the "Key Bindings - User" from your preferences and add this JSON:
[
{
"keys": ["CTRL+\\"],
"command": "reindent",
"args": {
"single_line": false
},
"context": [{
"key": "selector",
"operator": "not_equal",
"operand": "source.js,source.json,text.html"
}]
}, {
"keys": ["CTRL+\\"],
"command": "htmlprettify",
"context": [{
"key": "selector",
"operator": "equal",
"operand": "text.html"
}]
}, {
"keys": ["CTRL+\\"],
"command": "js_format",
"context": [{
"key": "selector",
"operator": "equal",
"operand": "source.js,source.json"
}]
}
]
This will bind -\ to the reindent command (CTRL + SHIFT + F is already taken by "Find in Files"). single_line is false to force it to reindent the whole page, just like in eclipse.
There are two additional variants of the key bindings for working with HTML and JavaScript. These require that you have the htmlprettify and js_format plugins installed. I found the default formatting inferior for HTML and Javascript, so if you are editing these files I do recommend the plugins. If you don't care about these formats, then you can delete the last two key binding entries.
Go to "preferences/Key Bindings" and add on the right panel this line
{"keys": ["ctrl+shift+f"], "command": "reindent", "args": {"single_line": false}}
that line is meant for indenting all your code in one shot so you would ctrl+a then ctrl+shift+f
Sublime has an build-in feature that does indent lines for you. You can find this when opening the command palette, then look for Indentation: Reindent Lines.
Specific actions to organize your code is harder, because of the different syntaxes of languages.
Luckily enough, Will Bond has been so nice to create a Package Manager for Sublime Text. Using this, features can be added into the editor. These packages do can add code formatting features. You can see a list of formatting packages here.
As one of the tags in your question contains HTML, I'll assume you're looking for a formatting tool for HTML. I've found a package for that, although I've got no personal experience with it. You may install it from here.
A plugin for Sublime Text , that formats (indents) HTML source code. It makes code easier for humans to read.
I would recommend reading through the docs for more information about the package.
you can set shortcut key for one line by press [ctrl+shift+f] easy!!!
goto menu Preferences -> Key Bindings – User
{ "keys": ["ctrl+shift+f"], "command": "reindent"}
see detail at http://how-to-sublime-text.blogspot.com/2014/11/reformat-code.html
It's impossible to unselect my selection by pressing ESC (It keeps the selection I had made)
I want that when I press ESC, that it unselects everything. Is this possible?
This behavior is present in Visual Studio too
VIKASH_DASH's answer is on the right track, but overriding Escape's functionality without context is going to hurt some feelings. Escape is a rather dignified key; take a look at Preferences -> Key Bindings – Default and you'll see that Escape has six important, er, escape-related functions. You definitely wouldn't want to overwrite all of them all of the time!
Thankfully, Sublime Text's keybindings can have contexts. Coupled with some sneaky usage of the move command (have you noticed that hitting Left or Right effectively deselects everything?) you can create a keybinding to add to Preferences -> Key Bindings – User that only executes if you have something selected and doesn't clobber Escape's other functions:
{ "keys": ["escape"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": false }
]
}
The "forward": true argument to the move command will move your cursor to the end of the current selection before deselecting (it will also split your cursor if you have multiple selections, just mash it again to escape from that as well!). To move to the beginning instead, change "forward": true} to "forward": false}. Instead of acting almost exactly like Right, Escape is now a glorified Left!
There is a nice deselect package -
it doesn't move the cursor
its in In
preferences->key bindings usr ..
use put these code hope its works;
[
{ "keys": ["escape"], "command": "move_to", "args": {"to": "eof", "extend": false} },
]
I am having problems triggering autocomplete when I type (. Other characters work perfectly fine. Here is the code in my syntex-specific setting file:
"auto_complete_triggers": [
{"selector": "source.r - string - comment", "characters": "("},
{"selector": "source.r - string - comment", "characters": ","}
]
Now , triggers autocomplete but ( does not. When I change ( to other characters it also works. Why does ( not work?
The problem seems to be related to the fact that ( gets somehow overwritten by a snippet for entering the closing ). So I come up with this solution based a key binding that chains together the snippet and the auto complete.
First, a command to chain commands:
# http://stackoverflow.com/a/10863489/2503795
import sublime_plugin
class ChainedActionsCommand(sublime_plugin.TextCommand):
def run(self, edit, actions, args):
for i, action in enumerate(actions):
self.view.run_command(action, args[i])
Second, the keybinding, which first inserts the snippet and then starts auto_complete
{
"keys": ["("],
"command": "chained_actions",
"args": {
"actions":["insert_snippet","auto_complete"],
"args":[{"contents": "(${0:$SELECTION})"},{}]
},
"context": [
{"key": "selector", "operator": "equal", "operand": "source.r"}
]
}
You might want to try escaping the ( with a \ character:
"auto_complete_triggers": [
{"selector": "source.r - string - comment", "characters": "\("},
{"selector": "source.r - string - comment", "characters": ","}
]
This may allow the interpreter to "see" the ( and not think it's part of the JSON syntax.
Good luck!
In Sublime Text we can change key bindings for our needs. But I can't find way to overwrite basic binding for unfold functionality. I have next code in my Key bindings - User file:
{ "keys": ["ctrl+keypad4"], "command": "fold" },
{ "keys": ["ctrl+keypad5]"], "command": "unfold" },
ctrl+keypad4 works as expected, but binding for ctrl+keypad5 not work at all. How to fix it?
I don't want to change global keymap.
You have an extra close bracket in there. It should be:
{ "keys": ["ctrl+keypad5"], "command": "unfold" },