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

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"
},
]

Related

Is it possible to use C-x C-s for saving in Sublime Text 3

In Sublime Text 3 is it possible to use C-x C-s for saving the file (save_all), while keeping C-s for search option?
ctrl+x+s or ctrl+xctrl+s did not work on my end.
{ "keys": ["ctrl+s"], "command": "find_next", "context":
[{"key": "panel", "operand": "find"}, {"key": "panel_has_focus"}]
},
{ "keys": ["ctrl+x"], "command": "noop" },
{ "keys": ["ctrl+x+s"], "command": "save_all" },
{ "keys": ["ctrl+x+ctrl+s"], "command": "save_all" }
Binding key chords like this in Sublime requires you to specify both keys individually in the keys list (that's why it's a list).
So, what you want is:
{ "keys": ["ctrl+s"], "command": "find_next", "context":
[{"key": "panel", "operand": "find"}, {"key": "panel_has_focus"}]
},
{ "keys": ["ctrl+x"], "command": "noop" },
{ "keys": ["ctrl+x", "ctrl+s"], "command": "save_all" },
Note that the binding of Ctrl+x to noop is not needed for this to work, so you can leave that out. However using the same key for both single items and in chords like this may require you to hit the key twice to get Sublime to trigger the command, since it otherwise can't tell without some delay which of the bindings you intended.

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 to bind multiple key to a command in sublime

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" },

how to override shortcut for multiple cursors sublime text 3?

I am using linux [FEDORA 20]. I want to override shortcut key for multiple cursor which is
ctrl+alt+up/down
because it is used for switching workspace in fedora.
I tired to search shortcut in Preferences>key binding - default. But couldn't find it.
So how do i override it?
CtrlAlt↑/↓ is the keyboard shortcut for multiple selection in Windows, not Linux. The following is from the Linux keymap file:
{ "keys": ["alt+shift+up"], "command": "select_lines", "args": {"forward": false} },
{ "keys": ["alt+shift+down"], "command": "select_lines", "args": {"forward": true} },
Go to
Preferences > Key Binding - User and add following lines to override default key bindings.
{ "keys": ["ctrl+space+down"], "command": "select_lines", "args": {"forward": true} },
{ "keys": ["ctrl+space+up"], "command": "select_lines", "args": {"forward": false} }
where
ctrl+space+up/down
is your key bindings.

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