make ctrl+enter add semicolon to the end of the line - sublimetext2

Ctrl+enter in sublimetext is a default shortcut to create a new line, no matter in what position your cursor are in the current line. However, sometimes I need to add a semicolon at the end of the current line before jump to a new line.
How can i make the "ctrl+enter" shortcut add a semicolon to the end of the current line before create a new line? Is it even possible?
sorry for my english.

You can simply modify the macro /Default/Add Line.sublime-macro to insert the semicolon.
From this
[
{"command": "move_to", "args": {"to": "hardeol"}},
{"command": "insert", "args": {"characters": "\n"}}
]
to this
[
{"command": "move_to", "args": {"to": "hardeol"}},
{"command": "insert", "args": {"characters": ";\n"}}
]

After AGS's answer, I solved my problem with similar approach. You can do this:
1. Create a macro file in the folder ~/.config/sublime-text-3/Packages/User with:
[
{
"args":
{
"to": "hardeol"
},
"command": "move_to"
},
{
"args":
{
"characters": ";"
},
"command": "insert"
},
{
"args":
{
"characters": "\n"
},
"command": "insert"
}
]
2. Then edit your keybindings Preferences > Key Bindings - User. Add this:
{ "keys": ["ctrl+enter"], "command": "run_macro_file", "args": {"file": "res://Packages/User/FILENAME.sublime-macro"} },

Related

How do I create a open folder keyboard shortcut in Sublime Text?

I know to add keyboard shortcuts, I go into Keyboard Bindings - User and edit the JSON file. I already have a lot of keyboard customizations.
[
{ "keys": ["ctrl+shift+."], "command": "erb" },
{ "keys": ["alt+i"], "command": "expand_tabs" },
{ "keys": ["alt+ctrl+w"], "command": "close_all" },
{ "keys": ["ctrl+t"], "command": "show_overlay", "args": {"overlay": "goto", "show_files": true} },
// swap the keybindings for paste and paste_and_indent
{ "keys": ["ctrl+v"], "command": "paste_and_indent" },
{ "keys": ["ctrl+shift+v"], "command": "paste" },
// swap the keybindings for save and save_all
{ "keys": ["ctrl+s"], "command": "save_all" },
{ "keys": ["ctrl+shift+v"], "command": "save" }
]
I just need to know the command for open folder. I tried the obvious open_folder it didn't work.
I found the answer in the Sublime Text 2 forums. The command is prompt_open_folder. So I just added this to my keyboard bindings.
[
// Open folder shortcut
{ "keys": ["ctrl+shift+o"], "command": "prompt_open_folder"}
]

create Sublime2 key binding to mimic shift + arrowL/R

In Sublime2, when you hold down shift and use the L/R arrow keys, your cursor moves while selecting text characters. I want to map this behavior to something else with Sublime key bindings, so I don't have to move my hands to the arrow keys to do this really common action.
I can't figure out how to do this, or if it's possible. I want to do something like this:
{ "keys": ["super+k"], "command": "move_and_select", "args": {"by": "characters", "forward": true} },
{ "keys": ["super+j"], "command": "move_and_select", "args": {"by": "characters", "forward": false} },
But I've just made up the move_and_select command. If you use the command move, it moves left and right by a character, but I want to move and select.
Is this possible? Can it be done with a python script plugin?
Figured it out. set "extend": true in args to make the movement also select. So the correct answer should be:
{ "keys": ["super+j"], "command": "move", "args": {"by": "characters", "forward": false, "extend": true} },
{ "keys": ["super+l"], "command": "move", "args": {"by": "characters", "forward": true, "extend": true} },

SumblimeText2 Vintage command repeat

How I can bind some command with repeats?
For example: I need to bind a command to move the cursor down to 10 lines.
For one line it's like this:
{ "keys": ["alt+j"], "command": "set_motion", "args": {
"motion": "move",
"motion_args": {"repeat": 1,"by": "lines", "forward": true, "extend": true },
"linewise": true },
"context": [{"key": "setting.command_mode"}]
}
Write a plugin:
import sublime, sublime_plugin
class CommandWithRepeats(sublime_plugin.TextCommand):
def run(self, edit, repeats, **args):
for x in xrange(repeats):
self.view.run_command(args['command'], args['args'])
And then we can add any command in key bindings with repeats:
"keys": ["alt+j"],
"command": "command_with_repeats",
"context": [{"key": "setting.command_mode"}],
"args": {
"repeats": 10,
"command": "set_motion",
"args": {
"motion": "move",
"motion_args": {
"by": "lines",
"forward": true,
"extend": true
},
"linewise": true
}
}
Wrap any command in command_with_repeats() and it executes some times (repeats arguement)

Keyboard shortcut to comment lines in Sublime Text 2

In Sublime Text 2, how do I enclose a selection in a comment?
Is there a keyboard shortcut for this action?
By default on Linux/Windows for an English keyboard the shortcut is Ctrl+Shift+/ to toggle a block comment, and Ctrl+/ to toggle a line comment.
If you go into Preferences->Key Bindings - Default, you can find all the shortcuts, below are the lines for commenting.
{ "keys": ["ctrl+/"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+/"], "command": "toggle_comment", "args": { "block": true } },
In the "Preferences->Key Bindings - User"
[
{ "keys": ["ctrl+7"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+7"], "command": "toggle_comment", "args": { "block": true } }
]
Just paste it, these are will work great !
I'd like to add, that on my mac by default block comment toggle shortcut is cmd+alt+/
For German keyboards use ctrl+shift+# to toggle a block comment and ctrl+# to toggle a line comment.
The shortcut in Preferences->Key Bindings - Default is set to Ctrl+Shift+/ and Ctrl+/, but to actually use the functions, press the keys stated above.
In a Brazilian Portuguese ABNT2 keyboard I have a similar issue to the one reported by JoshDM. In the file sublime-keymap I have:
{ "keys": ["ctrl+/"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+/"], "command": "toggle_comment", "args": { "block": true } },
But I have to use ctrl+; and ctrl+shift+;. On my keyboard, ; is on the left of /.
It seems like a bug.
you need to replace "/" with "7", it works on non english keyboard layout.
This did the trick for me coming from Brackets and being used to ctrl+/ on the numpad.
[
{ "keys": ["ctrl+keypad_divide"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+keypad_divide"], "command": "toggle_comment", "args": { "block": true } }
]
In my keyboard (Swedish) it´s the key to the right of "ä": "*".
ctrl+*
In keyboard (Spanish), SO: Win7.
Go into Preferences->Key Bindings - Default,
replace..."ctrl+/"]... by "ctrl+7"...
And don't use the numpad, it doesn't work.
Just use the numbers above the letters
On a Mac with a US keyboard, you want cmd+/.
Seems like some kind of keyboard mapping bug. I'm Portuguese, so I'm using a PT/PT keyboard. Sublime Text 3 apparently is handling / as ~.
Max OS: If you want to toggle comment multiple individual lines versus block comment an entire selection, you can do multi line edit, shift+cmd+L, then cmd+/ in that sequence.
First Open The Sublime Text 2.
And top menu bar on select the Preferences.
And than select the Key Bindings -User.
And than put this code,
[
{ "keys": ["ctrl+shift+c"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+c"], "command": "toggle_comment", "args": { "block": true } }
]
I use Ctrl+Shift+C, You also different short cut key use.
Ctrl+d and Ctrl+Shift+d....
[
{ "keys": ["ctrl+d"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+d"], "command": "toggle_comment", "args": { "block": true } },
]
On my laptop with spanish keyboard, the problem seems to be the "/" on the key binding, I changed it to ctrl+shift+c and now it works.
{ "keys": ["ctrl+shift+c"], "command": "toggle_comment", "args": { "block": true } },

In Sublime Text 2 - reopen build output

In Sublimt Text 2, when I use the build system (make) to run tests, the output is displayed in the build output pane.
However, if I press escape to close the output pane (e.g. to make a fix), I can't find a way to redisplay the output pane to see what else was borked. Have tried to create a custom keybinding to execute show_panel "output", but can't get it working.
Meep?
The menu shortcut is under Tools -> Build Results -> Show Build Results.
I wish this was under the View menu like all the rest of view options...
As you can see in Packages/Default/Main.sublime-menu the command for "Show build results" is this:
{
"command": "show_panel",
"args": {
"panel": "output.exec"
},
"caption": "Show Build Results",
"mnemonic": "S"
},
so a custom key binding could be this:
{
"keys": ["ctrl+alt+super+r"],
"command": "show_panel",
"args": {
"panel": "output.exec"
}
}
And the key binding to hide the panel:
{
"keys": ["ctrl+shift+2"],
"command": "hide_panel",
"args": {
"panel": "output.exec"
}
},
Building on akirk's answer you can make it toggle the build results panel by copying some of the syntax used for the escape shortcuts.
Adding the following lines to the user key bindings will do part of the trick. As reported by some of the previous answers the hide_panel command will hide any panel, and pressing it a second time will reveal build_results.
{
"keys": ["alt+b"], "command": "show_panel", "args": {"panel": "output.exec"},"context":
[
{ "key": "panel_visible", "operator": "equal", "operand": false }
]
},
{
"keys": ["alt+b"], "command": "hide_panel", "args": {"panel": "output.exec"},"context":
[
{ "key": "panel_visible", "operator": "equal", "operand": true }
]
},