M HYPE SPLASH
// general

Sublime Text: Find-and-replace within selection

By Emma Terry

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.

8 Answers

There is a button for that in ST2.

Screenshot

5

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} ] },
2

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

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

(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.
2

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).

cmd+option+F on mac will give you find and replace window

1

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"...

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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