Expand multiple selection to all suggestions (boxes,hints) in Sublime Text 2
Asked Answered
L

3

12

I am wondering if there already exists a way to expand selection to all areas suggested by Sublime Text 2 boxes, so one does not have to keep pressing Ctrl-D and sometimes Ctrl-K-D as shown in How do I skip a match when using Ctrl+D for multiple selections in Sublime Text 2?

90% of time the boxes on the screen are exactly what I want to multiple select, and it would be very handy if there was a one key option to do so.

for example, if you have

foo(2,42,23,2,2)

and you click on the first 2, the boxes will be shown around only the three single 2s. These I would like to select with a single command/macro.

If you go with Ctrl-D you have to skip the 2s in 42 and 23 with Ctrl-K-D.

If nothing like this exists, any pointers on writing a plugin for such functionality would be much appreciated.

UPDATE: I've gotten a great suggestion to use Alt+F3, which is awesome for making changes in the whole file. However, it would be also beneficial to limit the scope of multiple select to current visible page or tag or brackets or something else.

Luminescence answered 10/1, 2013 at 11:24 Comment(0)
H
8

I did the following, for such cases:

Put an entry in Key bindings - User:

{ "keys": ["alt+s"], "command": "toggle_in_selection", "context":
    [
        { "key": "setting.is_widget", "operator": "equal", "operand": true }
    ]
},

Note: you can choose another key combination.

Then, select the text you want to search in. You can use ctrl+L to select one line, ctrl+shift+m to select content of brackets you're in, or any other way listed in Selection menu, or any completely other way.


TIP: Make new file, choose the filename User.sublime-commands and store it that Packages/User folder, where new plugins or user-key-bindings are stored. And put this snippet into that file:

[
    // Selection Menu
    { "caption": "Selection: Expand to Paragraph", "command": "expand_selection_to_paragraph" },
    { "caption": "Selection: Expand to Scope", "command": "expand_selection", "args": {"to": "scope"} },
    { "caption": "Selection: Expand to Brackets", "command": "expand_selection", "args": {"to": "brackets"} },
    { "caption": "Selection: Expand to Indentation", "command": "expand_selection", "args": {"to": "indentation"} },
    { "caption": "Selection: Expand to Tag", "command": "expand_selection", "args":     {"to": "tag"} }
]

It will add those expand-selection options, that are in Selection menu, into command palette as well, so you don't have to remember the shortcuts. You can change "caption"s to what suits you best.


Once you selected the text you want to search in, press ctrl+f or ctrl+i (search or incremental search), use the shortcut from above, to make the "in-selection" button toggled (sixth button from left side on the search bar, looks like arrow pointing right and down on vertical bar). (optional: press alt+w to toggle "whole-words" button (looks like quotes) , in case you want to match free 2 in foo(2,42,23,2,2), and not match 2 in 42). Once your selection is tweaked, alt+enter selects all matches -> done.

You don't have to write what you want to search for, you can select it first and press ctrl+e ("slurp_find_string" command) to put it in search bar, and open the search bar later and it's gonna be there.

So the overall process goes like:

  1. (optional) ctrl+e on selected text you want to search for, so you don't have to write it later
  2. use whatever way to select all the text you want to search in
  3. ctrl+f or ctrl+i to open a search bar, if you didn't do 1., write what you search for in
  4. toggle in-selection if not toggled, toggle whole-words if you want
  5. find all with alt+enter and you're done

Sounds a bit complicated at first, but once you do it 10 times, the whole process (except of step 2.) won't take you more than a second. Plus, if you tweak some sublime settings, you can make it auto-toggle some things for you, for example auto ctrl+e upon any selection, or auto toggle-selection whenever you open searchbar with open selection.

NOTE: Your shortcuts may differ if you use mac or windows.

Hope it helps, if there's anything unclear, ask more...

EDIT: I was playing with key settings for a while, and ended up with this:

// without whole-words
{ "keys": ["ctrl+space", "f"], "command": "show_panel", "args": {"panel": "incremental_find", "reverse":false, "in_selection": false, "whole_word": false}, "context":
    [
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }
    ]
},
{ "keys": ["ctrl+space", "f"], "command": "show_panel", "args": {"panel": "incremental_find", "reverse":false, "in_selection": true, "whole_word": false}, "context":
    [
        { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
    ]
},

//with whole-words
{ "keys": ["ctrl+space", "w"], "command": "show_panel", "args": {"panel": "incremental_find", "reverse":false, "in_selection": false, "whole_word": true}, "context":
    [
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }
    ]
},
{ "keys": ["ctrl+space", "w"], "command": "show_panel", "args": {"panel": "incremental_find", "reverse":false, "in_selection": true, "whole_word": true}, "context":
    [
        { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
    ]
},

If you put it somewhere in Key bindings - User, it gives you two shortcuts, "ctrl+space", "f" (ctrl+space first, followed by f, similar like with "ctrl+k", "ctrl+d"), and "ctrl+space", "w". First one gives you incremental search panel with whole-words disabled, second one with whole-words enabled. Both of them will have in-selection pre-selected based on whether you had something selected when you pressed the shortcut. If you look at that, you should pretty much get the idea about how to tweak it to your own desires.

Hamza answered 20/1, 2013 at 17:26 Comment(1)
Thanks, you gave me a good idea on what to look for when doing my own key bindings.Luminescence
P
12

You can click before or after any '2' and then select all 2's with Atl+F3 on Windows and Linux or Ctrl++G on OS X.

Phyl answered 16/1, 2013 at 15:15 Comment(2)
This is awesome! One downside is that Alt+F3 selects ALL particular matches in the document so it is very dangerous to use. Any way to limit the scope to be more local(visible page would be best, brackets, tags, and so on)?Luminescence
@Luminescence The only way i know of - is to use Find All with "Whole word" and "In selection" turned on, but i down think it is achievable with a single shortcut. (Without writing some sort of plugin of course )Phyl
H
8

I did the following, for such cases:

Put an entry in Key bindings - User:

{ "keys": ["alt+s"], "command": "toggle_in_selection", "context":
    [
        { "key": "setting.is_widget", "operator": "equal", "operand": true }
    ]
},

Note: you can choose another key combination.

Then, select the text you want to search in. You can use ctrl+L to select one line, ctrl+shift+m to select content of brackets you're in, or any other way listed in Selection menu, or any completely other way.


TIP: Make new file, choose the filename User.sublime-commands and store it that Packages/User folder, where new plugins or user-key-bindings are stored. And put this snippet into that file:

[
    // Selection Menu
    { "caption": "Selection: Expand to Paragraph", "command": "expand_selection_to_paragraph" },
    { "caption": "Selection: Expand to Scope", "command": "expand_selection", "args": {"to": "scope"} },
    { "caption": "Selection: Expand to Brackets", "command": "expand_selection", "args": {"to": "brackets"} },
    { "caption": "Selection: Expand to Indentation", "command": "expand_selection", "args": {"to": "indentation"} },
    { "caption": "Selection: Expand to Tag", "command": "expand_selection", "args":     {"to": "tag"} }
]

It will add those expand-selection options, that are in Selection menu, into command palette as well, so you don't have to remember the shortcuts. You can change "caption"s to what suits you best.


Once you selected the text you want to search in, press ctrl+f or ctrl+i (search or incremental search), use the shortcut from above, to make the "in-selection" button toggled (sixth button from left side on the search bar, looks like arrow pointing right and down on vertical bar). (optional: press alt+w to toggle "whole-words" button (looks like quotes) , in case you want to match free 2 in foo(2,42,23,2,2), and not match 2 in 42). Once your selection is tweaked, alt+enter selects all matches -> done.

You don't have to write what you want to search for, you can select it first and press ctrl+e ("slurp_find_string" command) to put it in search bar, and open the search bar later and it's gonna be there.

So the overall process goes like:

  1. (optional) ctrl+e on selected text you want to search for, so you don't have to write it later
  2. use whatever way to select all the text you want to search in
  3. ctrl+f or ctrl+i to open a search bar, if you didn't do 1., write what you search for in
  4. toggle in-selection if not toggled, toggle whole-words if you want
  5. find all with alt+enter and you're done

Sounds a bit complicated at first, but once you do it 10 times, the whole process (except of step 2.) won't take you more than a second. Plus, if you tweak some sublime settings, you can make it auto-toggle some things for you, for example auto ctrl+e upon any selection, or auto toggle-selection whenever you open searchbar with open selection.

NOTE: Your shortcuts may differ if you use mac or windows.

Hope it helps, if there's anything unclear, ask more...

EDIT: I was playing with key settings for a while, and ended up with this:

// without whole-words
{ "keys": ["ctrl+space", "f"], "command": "show_panel", "args": {"panel": "incremental_find", "reverse":false, "in_selection": false, "whole_word": false}, "context":
    [
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }
    ]
},
{ "keys": ["ctrl+space", "f"], "command": "show_panel", "args": {"panel": "incremental_find", "reverse":false, "in_selection": true, "whole_word": false}, "context":
    [
        { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
    ]
},

//with whole-words
{ "keys": ["ctrl+space", "w"], "command": "show_panel", "args": {"panel": "incremental_find", "reverse":false, "in_selection": false, "whole_word": true}, "context":
    [
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }
    ]
},
{ "keys": ["ctrl+space", "w"], "command": "show_panel", "args": {"panel": "incremental_find", "reverse":false, "in_selection": true, "whole_word": true}, "context":
    [
        { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
    ]
},

If you put it somewhere in Key bindings - User, it gives you two shortcuts, "ctrl+space", "f" (ctrl+space first, followed by f, similar like with "ctrl+k", "ctrl+d"), and "ctrl+space", "w". First one gives you incremental search panel with whole-words disabled, second one with whole-words enabled. Both of them will have in-selection pre-selected based on whether you had something selected when you pressed the shortcut. If you look at that, you should pretty much get the idea about how to tweak it to your own desires.

Hamza answered 20/1, 2013 at 17:26 Comment(1)
Thanks, you gave me a good idea on what to look for when doing my own key bindings.Luminescence
I
1

In the example given "foo(2,42,23,2,2)" if you just want to multi-select the single '2's and exclude the '23' - you can just put your cursor after one of the ',2'| occurances, then without selecting any text press [CTRL]+[D] or [ALT]+[F3]

Indue answered 28/1, 2022 at 17:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.