How can I find and replace inside a selection in X

2019-03-22 11:23发布

问题:

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?

回答1:

UPDATE: This appears to be working 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.



回答2:

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



回答3:

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



回答4:

An few images to supplement the chosen answer:

And holding down Option:

See also

  • Find/Replace in Xcode using Regular Expressions


回答5:

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



回答6:

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.



回答7:

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)


回答8:

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



回答9:

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!



回答10:

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.



回答11:

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



标签: xcode replace