How do you find and replace within selection in sublime text 3?
Solution 1
- Ensure the find and replace panel is closed.
- Select the text in which you want to perform the find/replace - i.e. define your selection.
- Open the Find/Replace panel - if desired, you can use a keybinding to run the
show_panel
command by going to preferences -> keybindings, then adding this line to the user keybindings:{ "keys": ["ctrl+shift+s"], "command": "show_panel", "args": { "panel": "replace", "in_selection": true } }
to ensure that the "in selection" mode is active and skip the next step. (see the forums for more available args.) - Turn on "in selection" mode if it is not already active by clicking on the button in the panel.
- Enter your find/replace terms and perform your find/replace
If you want to change the selection you are searching within, you will need to close and re-open the panel.
If the region/selection you are searching within always contains multiple lines, you may want to consider setting the following preference for convenience:
// When auto_find_in_selection is enabled, the "Find in Selection" flag
// will be enabled automatically when multiple lines of text are selected
"auto_find_in_selection": true,
Solution 2
Select the word by pressing CMD+D it selects 1 from whole file and show transparent selection of all related word you can press more times CMD+D for selecting all word then edit it all words take places
Solution 3
You can (using ctrl-h, then toggling the 'in selection' option from the dialog), but it's a bad design IMO. Firstly, if you have an area of text highlighted when you request 'replace', it should assume "replace in selection" automatically. Secondly, as soon as you choose 'replace' it thoughtfully cancels the text selection for you. The whole process ends up involving cumbersome repetition, or an illogical sequence of steps. Visual Studio gets the UI design right; Sublime does not.
Solution 4
Searching and/or replacing within only a selected area in Sublime is indeed possible.
If you have text selected, once you open the Find
or Replace
panels, you can select the button for Find in Selection
(which is turned off by default unless you have the auto_find_in_selection
turned on and select multiple lines) to have the operation you carry out only apply to the selected area.
In this example I'm doing the following:
- Pressing Alt+F3 to select all instances of the word
to
, showing how many of them there are - Select the first paragraph and then open the
Find and Replace
panel with Ctrl+H - Turn on the option to operate within the selection
- Replace all instances of the word
to
with the wordFROM
- Show that the instances of
to
outside of the selection are still there untouched.
Solution 5
Select the line/word/code and Press Ctrl+d. Matched piece of text will be selected and will have cursor after them. You can edit them together.
Ctrl+h is the shortcut for 'Copy and Replace' in all versions of Sublime.
stevec
Updated on June 15, 2022Comments
-
stevec almost 2 years
Presently I'm copy/pasting my code into another program in order to find and replace within the selection, pretty stupid...
I've read this thread indicating how to find and replace within selection in ST2. Despite reading this thread but still cannot figure out if it's possible to do a simple find and replace within selection in ST3.
-
Cerberus over 5 yearsPerhaps it would help to add step 0: make sure the find/replace dialogue is closed.
-
Cerberus over 5 yearsGreat! You already had my up-vote, by the way. Your answer made me realise that the reason it normally always fails for me is that you really must have the search area selected right before you trigger the find/replace dialogue, or it won't work. Really buggy and/or counter-intuitive. Sublime should really change this to make more sense! What I normally do is double-click whatever word I want to replace then press control-H, because then the word is automatically placed in the search field. But then there is no way to get 'search in selection' to work.
-
stevec over 5 years@KeithHall if I add
{ "keys": ["ctrl+shift+command+s"], "command": "show_panel", "args": { "panel": "find", "in_selection": true } }
to keymap, then press ctrl+shift+command+s in a file, I get search box (the 'In Selection' button is already pressed) but there is only one search box (no second one for the replace field) -
Keith Hall over 5 years@user5783745 try
{ "keys": ["ctrl+shift+command+s"], "command": "show_panel", "args": { "panel": "replace", "in_selection": true } }
instead (i.e. thepanel
argument should bereplace
instead offind
if you want the find and replace panel to show