Sublime Text 2 move cursor out of parenthesis, quo

2019-01-30 00:08发布

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.

enter image description here

Is there a quick and easy way to solve this for my workflow?

12条回答
Bombasti
2楼-- · 2019-01-30 00:35

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:

  1. cursor is just after quotes or closing parentheses/bracket:

    move one character forward

  2. cursor is just before quotes or closing parentheses/bracket:

    move one character forward

  3. cursor is just before opening parentheses/bracket:

    move to closing parentheses/bracket

  4. !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:

  1. the cursor is unable to jump out of quotes (just step over it when the cursor is directly followed by one).
  2. the cursor is unable to jump out of the nearest parenthesis, quotes, or brackets when the code block is commented.
查看更多
在下西门庆
3楼-- · 2019-01-30 00:35
Ctrl + PgUp Cycle up through tabs

Ctrl + PgDn Cycle down through tabs

This can go to the end of brackets

查看更多
再贱就再见
4楼-- · 2019-01-30 00:40

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

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-30 00:40

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.

查看更多
不美不萌又怎样
6楼-- · 2019-01-30 00:41

Best solution for this is recording a macro on Sublime Text and then assigning it to a keyboard shortcut. Follow these steps:

  1. Create a line such as alert('hello') and leave the cursor right after letter 'o'.
  2. Then go to Tools > Record a Macro to start recording.
  3. Press Command+ to go to the end of line.
  4. Press ; and hit Enter
  5. Stop recording the macro by going to Tools > Stop Recording Macro
  6. You can now test your macro by Tools > Playback Macro (optional)
  7. Save your macro by going to Tools > Save Macro (ex: EndOfLine.sublime-macro)
  8. 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"}
    }
    
  9. 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!

查看更多
何必那么认真
7楼-- · 2019-01-30 00:48

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

查看更多
登录 后发表回答