如何在 Xcode 中的选择中查找和替换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5254650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:44:01  来源:igfitidea点击:

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

xcodereplace

提问by DougW

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?

在 Xcode < 4 中,您可以按住“选项”键,“全部替换”按钮将更改为“在选择中替换”。从 Xcode 4 开始,这没有任何作用。任何人都知道是否有新的方法可以做到这一点,或者是提交错误的时间?

回答by Lou Alicegary

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

更新:这似乎现在有效,至少在 Xcode 4.4.1 中。当查找/替换栏出现在编辑器的顶部时,按住键盘上的选项键会导致“在选择中替换”代替“全部替换”出现。

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

我很高兴,因为这是一个令人讨厌的遗漏。

回答by Doug Sjoquist

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

  1. 在 Xcode 中,选择文本,按复制
  2. 在终端会话中:

    pbpaste|sed 's/SOURCETEXT/NEWTEXT/g'|pbcopy
    
  3. 返回 Xcode 窗口,按粘贴

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

由于仍应选择原件,因此将仅替换它。您可能可以构建一个简单的 shell 脚本来执行此操作。

Doug

道格

回答by Lily Ballard

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

似乎缺少功能。您应该提交错误报告

回答by Suragch

An few images to supplement the chosen answer:

一些图片来补充所选的答案:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

And holding down Option:

并按住Option

enter image description here

在此处输入图片说明

See also

也可以看看

回答by user677626

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.

我很不高兴他们取消了这个功能,因为我经常使用它,但这是我的解决方法。将您选择的文本从 Xcode4 复制到 TextEdit 或其他一些文字处理器,在那里进行查找和替换,然后将结果复制回 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.

这并不性感,但如果您执行大量“根据我的选择进行查找和替换”,并且在工作时让文字处理器在 Spaces 中保持打开状态,那么这是值得的。

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

他们应该添加“我的选择”作为“工作区”和“我的范围”的替代。

回答by Chris

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.
  • Uncheckall the ones you don'twant replacing.
  • Press Replace
  • 在查找和替换中,显示查找选项(您可以通过按放大镜来执行此操作)。
  • 按预览。
  • 取消选中所有你的那些希望更换。
  • 按替换

Hope that helps, it did me.

希望有帮助,它帮助了我。

回答by eborisch

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 (notreplace all)
  1. 在工作区中进行查找和替换 (cmd-opt-shift-f) 输入您想要的查找/替换
  2. 输入您想要的搜索词并按回车键
  3. 从左侧的匹配列表中选择替换范围
  4. 点击替换(不是全部替换)

回答by jodell

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

我发现 alt-command-f 更容易进行本地查找和替换(4.3),然后解决您的选择。

回答by rattletrap99

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

EthenA.Wilson 几天前在对 OP 的评论中问道:

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

“有没有办法在 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.

在编辑器页面左上角的栏中放置“查找和替换”术语后,选择要搜索的文本,然后查看右上角(同一栏)。您会在“替换”旁边看到“全部”字样。现在按下选项键。“全部”将更改为“选择中的全部”。点击它,你就完成了。可能更直观一些,但功能在 Xcode 5 中。

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

当然,在单击之前拍摄快照是个好主意!

HTH!

哼!

回答by Mythlandia

To replace text in a selection using XCode 9

使用 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.
  1. 按 Option-Command-F 调出查找/替换框。
  2. 输入搜索和替换字符串。更改搜索字符串将丢失任何现有选择,因此..
  3. 做出选择(再次)。(如果您不这样做,则选择将仅是找到的第一个搜索字符串)
  4. 按住 键,“全部替换”将变为“替换选择”,然后单击它。

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

一旦您了解您是在输入搜索字符串后进行选择,那么这就不那么麻烦并且工作正常。