create Sublime2 key binding to mimic shift + arrowL/R - sublimetext2

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

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

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

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

Hotkey for cursor movement in windows sublime text

I use emacs and always use ctrl-n/p to move the cursor up/down one line.
My teammates use sublime text and they always use MOUSE!!
I googled a bit but can't find a keybinding of cursor-up/down in windows sublime.
How do you move your cursor in windows sublime?
Add the following to your user key bindings.
{ "keys": ["ctrl+n"], "command": "move", "args": {"by": "lines", "forward": false} },
{ "keys": ["ctrl+p"], "command": "move", "args": {"by": "lines", "forward": true} }
If you are an emacs user, you may also be interested in trying out https://github.com/grundprinzip/sublemacspro

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)

Use upper-case as word separator in Sublime Text 2

I can't find out how to use upper-case letters as word separators in Sublime Text 2.
What I want is the following behaviour: in some C++ IDE I like, using ctrl+left/right when cursor is in a word like thisIsAComposedWord will move cursor to next upper case in the word (or to the beginning/end of the word).
There's something in Sublime text called "word separators" that seems to do that, it appears his way in the default preferences file:
// Characters that are considered to separate words
"word_separators": "./\\()\"'-:,.;<>~!##$%^&*|+=[]{}`~?",
So can I insert upper-case in that list? Thanks.
You can move by "Subword" with the following Keybinds:
{ "keys": ["alt+left"], "command": "move", "args": {"by": "subwords", "forward": false} },
{ "keys": ["alt+right"], "command": "move", "args": {"by": "subword_ends", "forward": true} },
This recognizes camelCase and under_score.
You can also move by Word with
{ "keys": ["ctrl+left"], "command": "move", "args": {"by": "words", "forward": false} },
{ "keys": ["ctrl+right"], "command": "move", "args": {"by": "word_ends", "forward": true} }
This recognizes "word separators" as specified in your settings file.
I'm using Sublime Text in Linux (Ubuntu) and this works for me:
In your Settings-User add the same content of the "word-separators" by default (which is that you put in the question). Then, to the string add the regular expression of a Upper-Case letter [A-Z]. The result is:
{
"word_separators": "./\\()\"'-:,.;<>~!##$%^&*|+=[]{}`~?[A-Z]"
}
In order to move to the next upper case in a Word, I use Alt + Left/Right arrow.
Edit:
Regular expression doesn't work. The answer is not valid.