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:
- (optional)
ctrl+e
on selected text you want to search for, so you don't have to write it later
- use whatever way to select all the text you want to search in
ctrl+f
or ctrl+i
to open a search bar, if you didn't do 1., write what you search for in
- toggle in-selection if not toggled, toggle whole-words if you want
- 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.