DougW
DougW

Reputation: 30035

How can I find and replace inside a selection in Xcode?

In Xcode < 4, you could hold the "option" key, and the "Replace All" button would change to "Replace in Selection". As of Xcode 4, this does nothing. Anyone know if there's a new way to do it, or is it bug filing time?

Upvotes: 53

Views: 25336

Answers (11)

Suragch
Suragch

Reputation: 512206

A few images to supplement the chosen answer:

enter image description here

enter image description here

And holding down Option:

enter image description here

See also

Upvotes: 13

Lou Alicegary
Lou Alicegary

Reputation: 866

This appears to be working again now, at least in Xcode 4.4.1.

When the find/replace bar appears at the top of the editor, holding down the option key on the keyboard causes "Replace in Selection" to appear in lieu of "Replace All."

I'm glad, because this was an ANNOYING omission.

Upvotes: 74

Mythlandia
Mythlandia

Reputation: 584

To replace text in a selection using Xcode 9

  1. Press Option-Command-F to bring up the find/replace box.
  2. Enter the search and replace string. Changing the search string will lose any existing selection, so..
  3. Make your selection (again). (If you don't do this, the selection will be the first search string found only)
  4. Hold down the key and "Replace All" will change to "Replace Selection", then click it.

Once you understand that you make your selection AFTER you have entered the search string, then this is not that clumbersome and works fine.

Upvotes: 1

rattletrap99
rattletrap99

Reputation: 1479

EthenA.Wilson asked in a comment to the OP a couple of days ago:

"Is there a way to do this in Xcode 5?"

For the benefit of those who, like me, had been searching for it, here's how:

After you put your Find and Replace terms in the bars at the top left-hand side of the editor page, select the text you want to search in, then look at the top right-hand side (same bar). You'll see where it says "All", right next to "Replace." Now press the Option key. "All" will change to "All in Selection." Click it, and you're done. Could be a bit more intuitive, but the functionality is there in Xcode 5.

Naturally, good idea to take a snapshot before you click!

HTH!

Upvotes: 0

jodell
jodell

Reputation: 1047

I find alt-command-f easier for local find and replace (4.3) and then working around your selection.

Upvotes: 0

eborisch
eborisch

Reputation: 676

Not ideal, but not too bad:

  1. Do a find and replace in workspace (cmd-opt-shift-f) enter your desired find/replace
  2. Enter your desired search term and hit return
  3. Select the range of replacements from the list of matches on the left
  4. Hit replace (not replace all)

Upvotes: 1

Doug Sjoquist
Doug Sjoquist

Reputation: 735

Another workaround:

  1. In Xcode, select the text, press copy
  2. In a terminal session:

    pbpaste|sed 's/SOURCETEXT/NEWTEXT/g'|pbcopy
    
  3. Return to Xcode window, press paste

Since the original should still be selected, it will just be replaced. You could probably build a simple shell script to do this.

Doug

Upvotes: 16

Chris
Chris

Reputation: 1475

There is another way only replace the matches you find, rather than just this one or all of them.

I suggest you save a copy first, just in case....

  • In Find and Replace, Show Find Options (you can do this by pressing the magnifying glass).
  • Press Preview.
  • Uncheck all the ones you don't want replacing.
  • Press Replace

Hope that helps, it did me.

Upvotes: 2

user677626
user677626

Reputation: 31

I'm upset that they took out this functionality, as I used it constantly, but here's my workaround. Copy your selected text from Xcode4 to TextEdit or some other word processor, do the find and replace there, and then copy the results back into Xcode.

It's not sexy but it's worth it if you do a lot of these "find and replace on my selection", and you leave the word processor open in Spaces as you work.

They should add "my selection" as an alternative to "workspace" and "my scope".

Upvotes: 3

Lily Ballard
Lily Ballard

Reputation: 185711

Seems like missing functionality. You should file a bug report.

Upvotes: 9

Gary C
Gary C

Reputation: 19

Not sure which feature prior to Xcode 4 you're referring to, but the shortcut Command+Shift+E gives you "Use Selection for Replace". If you're talking about "Find and Replace in Workspace" (Command+Option+Shift+F), then what you need to do is run your find and then hold down "Shift" or "Command" on the selections shown and then hit "Replace".

Upvotes: -1

Related Questions