Sublime Text 2 move cursor out of parenthesis, quotes, or brackets - sublimetext2

I need a fast way to make the cursor jump outside the auto wrap qoutes or other syntax elements. I don't want to have to reach down to my arrow keys each time, and definitely don't want to go to my mouse.
Is there a quick and easy way to solve this for my workflow?

You can use a shortcut (shift+space, or whatever you like) to move the cursor.
In your Key Bindings - User:
{ "keys": ["shift+space"], "command": "move", "args": {"by": "characters", "forward": true} }

I made a few key bindings out of macros as well.
You must make a macro for these, unless you want to spend more time building them, but it's really easy. Just go to Sublime Text, Tools > Record Macro, or hit ctrl Q. Save the file in Packages/User/ and then hit ⌘ , to open up your User Settings. Paste the settings below in there and boom. ( The | below represents my cursor )
Here are the one's I chose:
Function auto-bracketizer
When the cursour is here:
totallyAwesomeness(|)
Use the option + tilda shortcut.
⌥ ~
This prefills the function with brackets and the text ' # code... ' highlighted. It only works when inside the parenthesis.
Sublime User Settings
{
"keys": ["option+`"], "command": "run_macro_file", "args": {"file": "Packages/User/superBracketizeFunction.sublime-macro"}
},
Download Macro
Auto-End Line With Semicolon
When the cursour is here:
echo 'say what!!??|'
Use the command + semicolon shortcut.
⌘ ;
This adds a closing ; at the end of current line and moves you to the line below it. It actually works wherever you are on the line.
Sublime User Settings
{
"keys": ["super+;"], "command": "run_macro_file", "args": {"file": "Packages/User/superEndLineWiSemiColin.sublime-macro"}
},
Download Macro
Exit Argument & Exit Function
When your cursor is anywhere inside the function it will end up here:
public function totallyAwesomeness()
{
echo 'say what!!??';
} |
echo 'yep... that just happened';
Use the command + enter shortcut.
⌘ Enter
This will let you jump outside the argument and a space to the right as well as anywhere from within the function it will jump you out of it just being the closing bracket.
Sublime User Settings
{
"keys": ["option+enter"], "command": "run_macro_file", "args": {"file": "Packages/User/superExitFunctionArg.sublime-macro"}
},
Download Macro
Just in case you don't know what the path is to your User folder is, it is shown below.
/Users/alexcory/Library/Application Support/Sublime Text 3/Packages/User/
Also the Library folder is usually hidden, so you can download a program called Revealer that will allow you to toggle those hidden files.
If you want to know how I made these just hit me up and I'll show you! :D

Best solution for this is recording a macro on Sublime Text and then assigning it to a keyboard shortcut. Follow these steps:
Create a line such as alert('hello') and leave the cursor right
after letter 'o'.
Then go to Tools > Record a Macro to start recording.
Press Command+→ to go to the end of line.
Press ; and hit Enter
Stop recording the macro by going to Tools > Stop Recording Macro
You can now test your macro by Tools > Playback Macro (optional)
Save your macro by going to Tools > Save Macro (ex: EndOfLine.sublime-macro)
Create a shortcut by adding this between the square brackets in your
in your Preferences > Key Bindings - User file:
{
"keys": ["super+;"], "command": "run_macro_file", "args": {"file": "Packages/User/EndOfLine.sublime-macro"}
}
Now, every time you hit Command+;, it will
magically place the semicolon at the end of current line and move the cursor to the next line.
Happy coding!

A more complete way to make a key binding would be:
{ "keys": ["shift+space"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\"\\]]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
},
Assuming you want shift+space as the shortcut. Or you can change it to tab as well
As found in http://www.sublimetext.com/forum/viewtopic.php?f=3&t=5174#p23086

Following Riccardo Marotti's post;
If you would like to bypass the bracket on the next line, you can replace "characters" with "lines" in the args section.
{ "keys": ["shift+space"], "command": "move", "args": {"by": "lines", "forward": true} }

on a Dell XPS, Ctrl Enter does the trick for me

I just have this feature partially implemented with the help of a plugin named run_multiple_commands.py (see below)
(only tested on ST3, but the plugin is earlier than the first version of ST3 and should work on ST2 too).
Shortcut configuration is as below:
{
"keys": ["shift+space"],
"command": "run_multiple_commands",
"args": {
"commands": [
{"command": "move", "args": {"by": "characters", "forward": true} }
]
},
"context":
[
{ "key": "preceding_text", "operator": "regex_contains", "operand": "[)\\]}'\"]$", "match_all": true},
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
},
{
"keys": ["shift+space"],
"command": "run_multiple_commands",
"args": {
"commands": [
{"command": "move", "args": {"by": "characters", "forward": true} },
]
},
"context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\\]}'\"]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
},
{
"keys": ["shift+space"],
"command": "run_multiple_commands",
"args": {
"commands": [
{"command": "move_to", "args": {"to": "brackets"} },
]
},
"context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[(\\[{]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
},
{
"keys": ["shift+space"],
"command": "run_multiple_commands",
"args": {
"commands": [
{"command": "move_to", "args": {"to": "brackets"} },
{"command": "move", "args": {"by": "characters", "forward": true} },
]
},
"context":
[
{ "key": "following_text", "operator": "not_regex_contains", "operand": "^[)\\]}'\"]", "match_all": true },
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "[)\\]}'\"]$", "match_all": true},
{ "key": "following_text", "operator": "not_regex_contains", "operand": "^[(\\[{]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
},
One shortcut (shift+space) for four conditions:
cursor is just after quotes or closing parentheses/bracket:
move one character forward
cursor is just before quotes or closing parentheses/bracket:
move one character forward
cursor is just before opening parentheses/bracket:
move to closing parentheses/bracket
!1 && !2 && !3:
move to closing parentheses/bracket
and move one more character forward
To use this configuration in your ST, you should first add a file named run_multiple_commands.py to your .../Package/User/ directory, and the content of which is the second code piece of This Article
This solution is just fine for everyday use but is not perfect because:
the cursor is unable to jump out of quotes (just step over it when the cursor is directly followed by one).
the cursor is unable to jump out of the nearest parenthesis, quotes, or brackets when the code block is commented.

Ctrl + M is the default one that I have on windows machine.
Just do it

Perhaps the home and the end key are near to your fingers.

I use ctrl+f to move cursor one space forward. Also, on mac, I interchanged caps lock with ctrl. caps lock+f is much easier to reach. It works fairly well for me.

I found another way which lies within sublime keybindings itself. Basically, I just modify the keybindings for auto closing parens, that is, I replace "contents": "($0)" with "contents": "($1)$0". Then just hit Tab to get out of the parenthesis. So I add in my keybindings the following:
{ "keys": ["("], "command": "insert_snippet", "args": {"contents": "($1)$0"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true }
]
},
And similar for square brackets, curly brackets, and single and double quotes.

Ctrl + PgUp Cycle up through tabs
Ctrl + PgDn Cycle down through tabs
This can go to the end of brackets

Related

Sublime Text - Keyboard shortcut to go to next line or end of line after autocomplete?

In Sublime Text 2 (or 3), is there a keyboard shortcut of some sort (tab? enter?) to go from the cursor denoted by |:
$var->catch('someName|');
to
$var->catch('someName');|
or
$var->catch('someName');
|
For your first scenario, you can modify this answer slightly: https://stackoverflow.com/a/19957050/1569064 (note the addition of the single quote, ', and the semi-colon ;, in the operand values
// Move to end of line
{
"keys": ["shift+enter"], "command": "move_to", "args": {"to": "eol", "extend": false}, "context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\"';\\]\\}\\$]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
},
// Move to next line
{
"keys": ["ctrl+shift+enter"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": true}, "context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\"';\\]\\}\\$]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
}
My answer for your second example works when there is an empty line below. Otherwise, it moves the cursor to the end of that next, non-empty line.

Sublime Text 3 Reindent with proper array parenthesis in PHP

I Have a problem like that
but answer in link above don't helped me. I get used to reindent whole file while coding and I liked Sublime Text very much. But this bug makes me mad. So I need to reindent whole PHP files with shortcut and don't get wrong indenting.
This is how i need:
And this is how it does:
As #BullfrogBlues mentions in a comment, the Sublime PHP Grammar plugin has a fix for this. I did not want that entire package, but fortunately it is very easy to extract out just the array indentation rule from there:
https://github.com/gerardroche/sublime-php-grammar/blob/master/Indentation%20Rules%20-%20Arrays.tmPreferences
Just save that file into the same directory where all your custom snippets etc. go (on Mac it's ~/Library/Application Support/Sublime Text 3/Packages/User, not sure about Windows/Linux) and voila!
The following solution was taken from SublimeText Forum.
Add this to your keybindings:
{ "keys": ["enter"], "command": "insert_snippet", "args": { "contents": "\n\t$0\n" }, "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "punctuation.section.array.end.php", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "array\\($", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
]
},
I'm using Sublime Text 3 and could solve this problem using the instructions on that page. I'm copying the same instructions here for reference, and I'll try to give some hints to find out why it isn't working for you.
First I've added those lines in my keybinding settings (Preferences -> Key bindings - User):
{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "Packages/User/Add Line in Braces.sublime-macro"}, "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\($", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
]
}
You must pay attention here that this settings file is a JSON-Array, and the above code must written in brackets:
[
// Copy above configuration here
]
If there are already some keybindings in your settings file, you must separate them with commas:
[
{
// Some existing keybindings
},
// Copy above configuration here
]
Then you should create a macro file in you user folder for Sublime Text 3. Where to find this folder depends on the operating system you are using. For instance, on Ubuntu it is:
~/.config/sublime-text-3/Packages/User
Create a new file in this folder, and name it (pay attention to cases and spaces):
Add Line in Braces.sublime-macro
In this file, copy the following script and save:
[
{"command": "insert", "args": {"characters": "\n\n"} },
{"command": "left_delete", "args": null},
{"command": "move", "args": {"by": "lines", "forward": false} },
{"command": "move_to", "args": {"to": "hardeol", "extend": false} },
{"command": "reindent", "args": {"single_line": true} }
]
This must be working, it works for me.

Skip autocompleted brackets, commas etc. with tab in sublime-text

In sublime, if you type, alert("{<cursor> it will autocomplete the closing brackets and quotes to: alert("{<cursor>}").
In visual studio, if you hit tab, it will place the cursor at the end of the autocompleted characters.
How can I replicate this exact behaviour in sublime? I don't see much point in autocompletion if you have to type those characters anyway or use arrow keys.
Building on #AGS's answer and your comment, there are two possible options. The first (if you're not using OS X) is to just hit End, which will move the cursor to the end of the line (eol).
The second option is to slightly modify #AGS's keymap to the following:
{
"keys": ["shift+enter"], "command": "move_to", "args": {"to": "eol", "extend": false}, "context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\"\\]\\}\\$]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
}
The binds the eol functionality to ShiftEnter, and includes the regex support, which can be removed if you want.
I hope this helps!
Edit your .sublime-keymap file and add
// Skip past round and square autocomplete brackets
{
"keys": ["shift+enter"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\"\\]\\}\\$]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
},
In this case, shift + enter will function like tab in visual studio.
The solution is originally not mine - I found it either here or on the ST2 forum.

Keybind setting to skip past auto-pair end characters in Sublime Text 2

I want to be able to have the cursor move past the autopair ending character so I can continue typing my code.
I'm a noob to Sublime Text. I was looking through here at SO and found this post which uses this type of code (snippet):
//Tab skips to end of autopaired characters
{ "keys": ["tab"], "command": "move", "args": {"by": "characters", "forward": true},
And I then added to my Default(Windows)sublime-keymap -- User file like this:
//Tab skips to end of autopaired characters
{ "keys": ["tab"], "command": "move", "args": {"by": "characters", "forward": true},
"context":[
]
}
When I press the 'tab' key to make the cursor move past the closing autopair, the cursor moves to a tab stop (adding 4 spaces by default), it doesn't move "forward" like pressing an arrow key would do.
How can get the cursor just move forward using the tab key or some other key? What am I missing/doing wrong here? I don't want to have to use the arrow keys as doing so is not a natural keystroke from the home keys (esp. depending upon the user keyboard). Thanks!
The question you linked actually had it right—you just removed the context key array, which effectively told Sublime Text that you never wanted Tab to move the cursor forward by one character. Use the asker's complete key binding:
{ "keys": ["tab"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": true },
{ "key": "preceding_text", "operator": "not_regex_match", "operand": "[[:space:]]*", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^[\"'\\)\\}\\]\\_]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false },
{ "key": "has_next_field", "operator": "equal", "operand": false }
]
},
You can read more about key bindings and contexts at the Unofficial Docs Key Bindings page.

Coding with Sublime Text 2

Simple question, I just started coding with Sublime Text 2 on windows, and I want to change little things, like when I am writing an if statement such as if (i > 0), immediately after I type the "0", my cursor is between the "0" and ")", so if I hit enter, I want it to jump to after the ")". I am used to eclipse so I want to know how I can get the settings to mimic those of eclipse. I have tried editing the settings text files but couldn't find what I was looking for.
Try adding the following to your User key bindings (accessible through Preferences -> Key Bindings - User)
{ "keys": ["enter"], "command": "move", "args": {"by": "characters", "forward": true},
"context": [
{ "key": "selection_empty", "operator": "equal", "operand": true },
{ "key": "preceding_text", "operator": "not_regex_match", "operand": "[[:space:]]*", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^[\\)]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]}
edit: Update regex to only match parentheses.