how to bind multiple key to a command in sublime - sublimetext3

In sublime, for example,I want to bind both ctrl+1 and ctrl+2 to a command, is there any way to set this?
Thanks.

You can simply repeat the config. For example:
[
{ "keys": ["super+ctrl+f"], "command": "toggle_full_screen" },
{ "keys": ["super+alt+f"], "command": "toggle_full_screen" }
]
Will result in both super+ctrl+f and super+alt+f working.
Note the , at the end of the lines except the last line if you're getting syntax errors.
Don't confuse it with using a pair of shortcuts for a single command, which is done like this:
{ "keys": ["super+k", "super+b"], "command": "toggle_side_bar" },

Related

Keybinding alt+up won't work in Sublime Text 3

I want to copy the Move Line Up/Down keyboard shortcut of VS Code to Sublime Text 3 which uses Ctrl+Up/Ctrl+Down key bindings, so I placed the following in the User-defined key-bindings file:
[
{
"keys": ["alt+up"], "command": "swap_line_up",
"keys": ["alt+down"], "command": "swap_line_down"
}
]
The swap_line_down works but the swap_line_up doesn't. I've already checked for conflicts within Default(Windows).sublime-keymap. I've tried to swap the commands to check if the problem is specific with alt+up
[
{
"keys": ["alt+up"], "command": "swap_line_down",
"keys": ["alt+down"], "command": "swap_line_up"
}
]
and, indeed, now swap_line_up works but swap_line_down doesn't. So it seems alt+up is the problem. What seems to be the issue here?
Your key bindings aren't specified correctly; each binding object should have a single keys and command (and optionally a single args and context key s well), but you have both specified in a single binding.
It should look more like this:
[
{
"keys": ["alt+up"], "command": "swap_line_up",
},
{
"keys": ["alt+down"], "command": "swap_line_down"
},
]

Visual-Block mode not working in Vim with C-v on WSL#Windows 10

I use WSL Ubuntu and Vim inside the new Windows Terminal, but if I have to enter the visual-block mode with C-v, I can't since it acts as paste.
I am okay with rebinding it to something else but I don't happen to have found the command that I have to add to .vimrc, I think it has something to do with inoremap.
Any advice?
CTRL+v is bound to paste in Windows Terminal by default. As of now, the only thing that's working is to disable that behaviour in the settings.json. You can do that by pressing CTRL+, or just select Settings in this menu, then click on "Open JSON file" at the bottom left corner and comment out the line:
"keybindings": [
...
// { "command": "paste", "keys": "ctrl+v" }, <------ THIS LINE
After doing this, you can switch to visual block mode as usual and paste with CTRL+SHIFT+v.
I've found these issues on the project's GitHub about this problem:
https://github.com/microsoft/terminal/issues/5790
https://github.com/microsoft/terminal/issues/5641
You can just change the default "settings.json"
Orignal :
{
"command":
{
"action": "copy",
"singleLine": false
},
"keys": "ctrl+c"
},
{
"command": "paste",
"keys": "ctrl+v"
},
Modified :
{
"command":
{
"action": "copy",
"singleLine": false
},
"keys": "ctrl+shift+c"
},
{
"command": "paste",
"keys": "ctrl+shift+v"
},

Sublime text 3 keymap only runs last command defined

I'm trying to create two shortcuts to add these codes on sublime when I hit these keys, but only the second one works, like it overrides the first one.
Does someone have any idea on how to get this working?
I tried this:
[{
"keys": ["ctrl+."],
"command": "insert_snippet",
"args": {
"contents": "<pre><?print_r()?></pre>"
},
"keys": ["ctrl+alt+."],
"command": "insert_snippet",
"args": {
"contents": "?><pre><?print_r()?></pre><?"
},
}]
And this:
[{
"keys": ["ctrl+.", "ctrl+alt+."],
"command": "insert_snippet",
"args": {
"contents": "<pre><?print_r()?></pre>",
"contents": "?><pre><?print_r()?></pre><?"
}
}]
Each individual key binding needs to be it's own distinct JSON object (i.e. inside of {} characters), with keys to specify what key triggers it, command to specify what to execute and args to specify the command arguments.
Assuming it's not a copy/paste error of some sort, the reason your examples don't work the way you want them to is because although they contain valid JSON, they don't contain valid key bindings. So the structure of the file is valid, but the way that Sublime interprets it is different than you intended.
To visualize this, here's your first example with the [] characters removed:
{
"keys": ["ctrl+."],
"command": "insert_snippet",
"args": {
"contents": "<pre><?print_r()?></pre>"
},
"keys": ["ctrl+alt+."],
"command": "insert_snippet",
"args": {
"contents": "?><pre><?print_r()?></pre><?"
},
}
Now we can see that the very first { character is opening the first key binding, but there is no matching } character after the args of that command; instead we see another set of key binding object keys and the } on the last line is the end of the key binding.
In any JSON object (this is not Sublime Text specific), object keys need to be unique; so this is actually a single JSON object with each key duplicated. Sublime's reaction to this is to ignore the first three keys in favor of the second three.
This ends you up with a single key binding instead of two of them, and it's the second one, so ctrl+alt+. works, but ctrl+. doesn't because it was ignored.
In your second example, the duplicate object key is in the args key of the key binding; that means that when the key binding triggers, the insert_snippet command will execute, but the argument that it gets will be the contents of that second key.
In this case this binding works, but what you (perhaps inadvertently) did was define a key binding that requires you to press two keys in sequence in order to trigger it. That is, you would need to press ctrl+. followed by ctrl+alt+. in order for the key binding to trigger.
I think based on your examples you intended to have two distinct key bindings that each insert something different, and the first example is closest to that; you just need to insert }, after the first args and a { before the second keys:
[
{
"keys": ["ctrl+."],
"command": "insert_snippet",
"args": {
"contents": "<pre><?print_r()?></pre>"
},
},
{
"keys": ["ctrl+alt+."],
"command": "insert_snippet",
"args": {
"contents": "?><pre><?print_r()?></pre><?"
},
},
]
Here I've reformatted your second example with the additional fix in place to help clarify that it's the {} that are defining the individual key bindings, the [] characters are for wrapping the list of bindings in the file.

How do I create a snippet that inserts \{\} using escape characters?

I am trying to create a snippet in sublime text that when I press ctrl+shift+[ it inserts the snippet \{\}, but I am having trouble working out how to use escape characters to do it.
Here is what I have:
{
"keys": ["ctrl+shift+["],
"command": "insert_snippet",
"args": {
"contents": "\\{\\}"
}
}
but when I use the keyboard shortcut, it only inserts {} without the backslashes! It is strange because when I change it to just "\\" then the output is \, but when something comes after the two backslashes, it ignores the backslash.
I have found a work around of sorts using the ChainOfCommand package that inserts each character separately like this:
{
"keys": ["ctrl+shift+["],
"command": "chain",
"args": {
"commands": [
["insert_snippet", {"contents": "\\"}],
["insert_snippet", {"contents": "{"}],
["insert_snippet", {"contents": "\\"}],
["insert_snippet", {"contents": "}"}]
]
}
}
but the problem with that is that if I then do a ctrl-z undo, it will remove each character one at a time, instead of removing the whole snippet, which is annoying. Also, its just the principle of the thing, I should be able to do it in one snippet I am sure!
Does anyone have any idea how I can make it work?
As un-intuitive as it may seem, using the following keybinding will insert \{\}:
{
"keys": ["ctrl+shift+["],
"command": "insert_snippet",
"args": {
"contents": "\\\\{\\\\}"
}
},
The reason for this is explained at: https://github.com/SublimeTextIssues/Core/issues/1878#issuecomment-328133400:
Due to compatibility with TextMate snippets, the following characters must be escaped to be literal:
\, $, {, }
Any other \{CHAR} will be produced verbatim. This explains why a single backslash is fine, but two become a single.
In case it helps, you can also execute the following in the ST console to see the difference that the JSON extra-escaping makes (compared to, for example, the same content inside a .sublime-snippet file):
view.run_command('insert_snippet', { 'contents': r'\\{\\}' })

Sublime text: how to add a key binding to hex_viewer package command

I've installed the Hex Viewer package on sublime text 3, to toggle it i use ctrl+shift+p to open the command palette, then i search for "hex" and select the command of the package to toggle the hex view.
I was wondering how to bind a key to the specific package command, I'm aware of the key bindings configuration file but I don't know what JSON line should I add to call the package command.
This is my first question on stackoverflow, sorry if I did something wrong, have a nice day!
EDIT: This is the github of the package: https://github.com/facelessuser/HexViewer
It says:
There are 10 commands available via the command palette or by key-bindings.
This is the one I should like to bind
Hex Viewer: Toggle Hex View
And this is the string I've tried to paste on the key-bindings JSON file:
{"keys":["ctrl+shift+h"] , "command":"Hex Viewer: Toggle Hex View"}
You need to add a key binding for the Hex Viewer keymap.
To do this, after installing Hex Viewer via Package Control, navigate to Package Settings -> Hex Viewer -> Key Bindings - Default and add the following:
[
{
"keys": ["ctrl+shift+h"],
"command": "hex_viewer"
}
]
To save the file, you need to ensure that the %APPDATA%\Sublime Text 3\Packages\HexViewer directory exists, assuming this is your package directory.
There's also an example key map available on the GitHub link you mentioned with the other available commands.
Example.sublime-keymap
[
{
"keys": ["ctrl+shift+b","ctrl+shift+h"],
"command": "hex_viewer"
},
{
"keys": ["ctrl+shift+b","ctrl+shift+i"],
"command": "hex_show_inspector"
},
{
"keys": ["ctrl+shift+b","ctrl+shift+f"],
"command": "hex_finder"
},
{
"keys": ["ctrl+shift+b","ctrl+shift+e"],
"command": "hex_editor"
},
{
"keys": ["ctrl+shift+b","ctrl+shift+x"],
"command": "hex_writer"
},
{
"keys": ["ctrl+shift+b","ctrl+shift+u"],
"command": "hex_discard_edits"
},
{
"keys": ["ctrl+shift+b","ctrl+shift+="],
"command": "hex_checksum",
"args": {"panel": true}
},
{
"keys": ["ctrl+shift+b","ctrl+shift+-"],
"command": "hash_selection"
},
{
"keys": ["ctrl+shift+b","ctrl+shift+g"],
"command": "hash_eval"
}
]
Your binding should be
{ "keys": ["ctrl+shift+h"] , "command":"hex_viewer"}
you could use something like this to assign a key biding to a plugin
-> Preference -> key - bending - user
then add this
[
{ "keys": ["ctrl+shift+x"], "command": "the name of plugin." }
]

Resources