21

Does Sublime Text 2 allow the user to perform find-and-replace, limited to the current selection?

I should think it must, only I've been unable after multiple searches to find how to carry it out.

23

There is a button for that in ST2.

Screenshot

  • 4
    True, there is a button. But how do you use it? In particular, how do you set "the selection"? From my experience ST2 uses the selection from the time when you opened the search bar. So if the bar is open already you have to close it first (Esc), then select the range of text you want to examine and then open it again (Ctrl+F or Ctrl+H). Pro tip: If the selection spans multiple lines it won't replace your current search expression. – Robert Apr 23 '13 at 18:28
  • 3
    Cmd+Opt+F on mac to open the find & replace console. – Micah May 31 '13 at 16:37
  • 2
    I'm completely puzzled as to why there is no keyboard shortcut for that "In selection", espacially since there is for all other options! Do I really have to move my hand to the mouse to enable that? Seems to go against all ST stands for... (I'm using ST3) – TheStoryCoder Mar 25 '15 at 15:16
  • 5
    @TheStoryCoder yeah it's dumb that that's no keyboard shortcut, but it turned out to be easy to make one, except the name of the command wasn't documented but I got lucky and guessed right. Here's the code to add to your user keybindings: { "keys": ["alt+y"], "command": "toggle_in_selection", "context": [ { "key": "setting.is_widget", "operator": "equal", "operand": true } ] }, – Owen_R May 31 '15 at 7:20
  • A note for other people who were confused like me: when you select several lines of text, then press Ctrl+H (which opens Find & Replace) and start typing in the "Find what" field, the selected lines stop looking selected, but they still are. If it wasn't for that problem, I wouldn't have even searched for a solution and found this post. BTW I seem to remember that problem wasn't there before, so maybe after a reboot or something it'll disappear. – Stefan Monov Nov 21 '16 at 18:15
7

If your selection includes a line break, the "within selection" button will be automatically activated - on ST2

The smart activation has been lost on ST3, unfortunately. You can follow up on that here http://www.sublimetext.com/forum/viewtopic.php?f=3&t=11679

  • 10
    It's now possible to bring back this functionality by using: "auto_find_in_selection": true – mdrozdziel Sep 24 '13 at 7:27
  • How do I invoke it automatically when it doesn't include a line break? For examples when you have a really long line that spans many "rows"? Everytime I have that line selected and invoke search, it disables the "in selection" option. – user2544542 Jun 23 '17 at 2:48
6

Here's what you need to add to your user keybindings:

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

Yeah, it's dumb that that's no default keybinding set.

Also the command name "toggle_in_selection" wasn't documented, but I guessed and got lucky.


EDIT TO ADD:

/*
btw, the way i want to use find by default
(actually, i use replace by default, but same dif),
is for "in_selection" to be true by default,
*unless* the selection is empty.
(
the default is that "in_selection" is *false* by default,
unless the selection contains a newline.
)
i finally got around to digging up how to get that,
which is apparently by changing the *keybindings* you use to pop open the find(/replace) panel?
*/
    { "keys": ["ctrl+f"], "command": "show_panel", "args": {"panel": "replace", "in_selection": true}},
    { "keys": ["ctrl+f"], "command": "show_panel", "args": {"panel": "replace", "in_selection": false},
    "context":
        [
            { "key": "selection_empty", "operator": "equal", "operand": true}
        ]
    },
  • The keybinding already exists by default. – elsadek Feb 12 '16 at 14:03
  • Huh. Wonder why I was missing it, then... the only "toggle_" things I had in my default bindings were: toggle_side_bar toggle_full_screen toggle_distraction_free toggle_overwrite toggle_record_macro toggle_setting toggle_comment toggle_case_sensitive toggle_regex toggle_whole_word toggle_preserve_case – Owen_R Feb 12 '16 at 18:31
3

(with Mac OS X specific shortcuts) to replace some name with new name:

  1. place the cursor above the text
  2. Find some name: cmdf, type some name (sometimes need to press return)
  3. Specify replace: shiftcmdr, tabtab to get to replace field, type new name, don't press return
  4. repeat altcmde until done.
  • Does anyone know a faster way of doing this?! It's pretty ridiculous and unnecessarily hard to remember – AJP Sep 9 '12 at 0:37
  • see @oliver-g 's answer - ST2 does so by default, and if you have disabled it, just click ctrl+H and left to "Find What:" there's a button titeled "In selection" – Peter Sep 30 '12 at 11:30
2

Just select the text you want to search, then click Replace from the find menu. It will automatically do it within your selection.

0

the key to work with replace within selection is to FIRST select the text then go to find > replace then fill in text for what your are gonna find & replace, then check the button tooltipped "in selection" then click on "Replace All"...

0

I'm adding another answer as the best and most helpful solution (in my opinion) was hidden in a comment and I almost missed it (thanks @mdrozdziel).

Select the text you want to search within, hit Shift+H to bring up the find and replace toolbar. There is a button to the right of the bank of buttons on the left which will activate search within selection.

This is off by default in ST3 but you can set it to default on when you select more than one line by adding "auto_find_in_selection": true to your preference file (found by inputting Alt,N,S,Enter).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.