125

I've just started using Sublime Text 2, coming from emacs.

I have a region of text selected. Within that region, I'd like to replace all text instances of '0' with '255'.

How do I do this kind of text replacement?

BryanH
  • 5,826
  • 3
  • 34
  • 47
user1419762
  • 3,571
  • 5
  • 20
  • 10

12 Answers12

213

This frustrated the heck out of me, and none of the above answers really got me what I wanted. I finally found the answer I was looking for, on a mac if you do + option + F it will bring up a Find-Replace bar at the bottom of your editor which is local to the file you have open.

There is an icon option which when hovered over says "In Selection" that you can select to find and replace within a selection. I've pointed to the correct icon in the screenshot below.

enter image description here

Hit replace all, and voila, all instances of '0' will be replaced with '255'.

Note: this feature is ONLY available when you use + option + F.

It does NOT appear when you use + shift + F.

Note: this will replace all instances of '0' with '255'. If you wanted to replace 0 (without the quotes) with 255, then just put 0 (without quotes) and 255 in the Find What: and Replace With: fields respectively.

Note:

option key is also labeled as the alt key.

key is also labeled as the command key.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Paul Pettengill
  • 4,843
  • 1
  • 29
  • 33
  • 2
    Thank you for this answer! The graphic helps too. I had thought there was only the `command+shift+F` dialogue. Had no idea there was another one. – Joseph Marikle Apr 22 '13 at 16:07
  • 2
    This is the only answer that actually answers the question -- how do i replace text *in a selection*. – ericsoco Jul 19 '13 at 05:54
  • 7
    `Ctrl+H` on GNU/Linux, or menu `Find` > `Replace...` – cbliard Dec 11 '13 at 08:55
  • YES @CBLIARD Ctrl+H DID THE JOB FOR UBUNTU THANKS – z atef Jun 04 '14 at 00:54
  • 15
    on `Windows OS` it's `Ctrl+H` or menu `Find > Replace...` , seems to be the same as for Linux (see @cbliard 's comment) – Adriano Jun 17 '14 at 12:36
  • @raychenon I think you mean `⌘+option+F` as `⌘+shift+F` will get you the global find and replace. – Paul Pettengill Apr 24 '15 at 18:33
  • This doesn't work with multiple selection, for example if you've selected few lines only and want to replace something with some other thing only within the selection. – shashwat Apr 09 '18 at 09:19
  • For anyone whose keyboard shortcuts are different on Mac, you can reach this option in the top menus at: `Find > Replace` and click the "in selection" icon as in the answer. – Rick Gladwin May 27 '19 at 17:00
36

On a Mac you can can select the text that you are after then press: cmd + ctrl + G

This will select every instance of your selected text within the same document. If you now start typing to replace your original highlighted text, you will replace all of the other occurrences at the same time.

SWilder
  • 817
  • 7
  • 11
  • This does not work properly. As soon as you press CMD-Ctrl-G, all other substrings that match your selected text also get selected and are then changed as you edit. – David Apr 03 '14 at 00:57
  • @David. Surely you've just described the same behaviour as I did? – SWilder Apr 11 '14 at 14:53
27

Some of the answers here haven't really helped.

People are showing you how to find stuff, but now how to replace it.

I just had a look, and it looks like it's Ctrl+H for replace, then you get the find dialog as well as a replace dialog. This worked for me.

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
Adam K Dean
  • 7,387
  • 10
  • 47
  • 68
  • It will not work for Mac user. As, Ctrl + H hides sublime in Mac. – Vineet Jain Nov 01 '18 at 08:52
  • I'm a Mac user too. It's `cmd`+`H` for hide, not `ctrl`, though that being said I use Atom instead of sublime now and the shortcut for replace is `alt`+`cmd`+`f` so things can change – Adam K Dean Nov 06 '18 at 11:27
17

ST2 has a feature for changing multiple selections at once.

  1. Double click the first instance of 0 that you want to change.
  2. Press the key for Find->Quick Add Next* to select the next instance of 0, and repeat until you've selected all the instances of 0 that you want to change.
    If this method selects an instance that you want to skip, press the key for Find->Quick Skip Next.
  3. Verify that the multiple highlighted fields are what you want to replace. Next, type in '255' and it should modify all of the selected instances simultaneously.

*Look at the Find menu on the menu bar to find the correct shortcut key for your system. For vanilla Windows, the menu tells you that Find->Quick Add Next is Ctrl+D and Find->Quick Skip Next is Ctrl+K,Ctrl+D.

Joe Bergevin
  • 3,158
  • 5
  • 26
  • 34
  • 1
    Yes, just double click any word and all other instances will be lightly highlighted and start pressing Command + D and it will start highlighting the other occurrences, and then you can just start typing and it will replace the text in all the selected areas. – WallMobile Jan 22 '13 at 19:15
12

I know this has been answered many times, and all are correct, but I though I would add another:

Similar to the Ctrl - D method to select individual occurrences of the current selection, you can select all occurrences in the file with Alt+F3 when using Windows or Linux (CMD+CTRL+G in Mac world).

This is helpful for mass-changes.

Heinrich Cloete
  • 702
  • 5
  • 12
jkloo
  • 171
  • 1
  • 4
9

First, select the portion of the text containing the bits you want to change. On Windows (sorry) it's Ctrl + H or Find > Replace... This opens up the Find/Replace boxes at the bottom of the file. Enter your details then click Replace All (or Ctrl + Alt + Enter)

Heinrich Cloete
  • 702
  • 5
  • 12
garyh
  • 2,782
  • 1
  • 26
  • 28
6

1) Ctrl + F (or Cmd + F on a Mac);
2) Enter the string you want to find on the input at the bottom of the window.
3) Press "Find All";

All of the appearances are now selected. Do whatever you want.

Aside

There are a bunch of options at the left of the input that opens on Ctrl + F. There's one that says something like "Find in selected text". Select a bunch of text, check that option and repeat the same steps above starting from 2). Now, only matches belonging to that selection are selected.

João Paulo Macedo
  • 15,297
  • 4
  • 31
  • 41
  • complications: I'm using linux, and am using emacs keybindings for ST2. So, CTRL-F just moves the cursor forward. Also, I'm wondering specifically, whether there is a way to replace all instances of a target string that exist soley within a selection. – user1419762 Jul 09 '12 at 23:21
  • See my aside for replacing solely within a selection. Now about linux... I can't really help you there as I've only used it on mac and windows. – João Paulo Macedo Jul 09 '12 at 23:24
4

You can use ctrl+F to find the text.
ctrl+h to enter the replacement text. Then ctrl+shift+h to replace the current selected text and move to next matched text.

This is for windows. But you can check in mac also for which you might want to check the key bindings under Preferences.

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
xpioneer
  • 3,075
  • 2
  • 16
  • 18
3
  1. Select the item you want to replace (double-click it, or Ctrl - F to find it)...

  2. Then do a (Ctrl - Apple - G) on Mac (aka. "Quick Find All"), to HIGHLIGHT all occurrences of the string at once.

  3. Now just TYPE your replacement text directly... All selection occurrences will be replaced as you type (as if your cursor was in multiple places at once!)

Very handy...

Heinrich Cloete
  • 702
  • 5
  • 12
Paul Brady
  • 503
  • 4
  • 10
3

Windows
1- Find: CTRL + F
2- Select-in: Alt + Enter

Now you can change all the selection in one shot like "seen-on-tv" ST homepage Spot.

Credit goes to : https://superuser.com/a/921806/342825

Community
  • 1
  • 1
elsadek
  • 1,028
  • 12
  • 39
2

As @JOPLOmacedo stated, ctrl + F is what you need, but if you can't use that shortcut you can check in menu:

  • Find -> Find..

    and there you have it.
    You can also set a custom keybind for Find going in:

  • Preferences -> Key Bindings - User

    As your request for the selection only request, there is a button right next to the search field where you can opt-in for "in selection".

  • Tshilidzi Mudau
    • 7,373
    • 6
    • 36
    • 49
    Gruber
    • 2,196
    • 5
    • 28
    • 50
    0

    option + command + F works in Mac for find and replace.

    Coderaemon
    • 3,619
    • 6
    • 27
    • 50