58

Can I create a custom snippet that takes a parameter that is replaced multiple times inside the code?

I tried something like:

<#class#> instanceOf<#class#>;

but it doesn't replace both class placeholders when I insert the snippet and write over the first parameter.

lkraider
  • 4,111
  • 3
  • 28
  • 31
  • I was going to ask this also. Apparently, it does not work that way? – Tony Eichelberger Jul 08 '11 at 19:49
  • No it does not, it simply regards those as two separate fields. – lkraider Jul 08 '11 at 19:58
  • What exactly are you trying to do? – AWF4vk Jul 27 '11 at 14:43
  • I have some complex snippets that require the same identifier to be entered multiple times. If Xcode would replace the same marker in multiple positions inside the snippet it would save a lot of time and help avoid typos. – lkraider Jul 27 '11 at 16:32
  • The funny thing is this used to work in Xcode 3.X. I remember copy/pasting a commented-out part of code that included a placeholder and when I changed the code outside of the comment, it updated the placeholder inside too. Let's submit bug reports https://bugreport.apple.com/ – Filip Radelic Aug 05 '11 at 23:14
  • When Apple "revolutionizes" stuff, old stuff gets left on the floor, duh. Except, of course, replacing folders of the same name instead of merge. That has existed since OS9 and they won't change it because it would break the user experience. Because I really wanted to delete the contents of Pictures that one time and would have stopped using my mac if it hadn't done so. I digress: no multi-replace like in eclipse. –  Aug 31 '11 at 09:14
  • @fichek: I just reported the bug. Feel free to dupe it! :) – Regexident Sep 03 '11 at 12:27
  • 2
    Anyone know what the state of this is in 2015 with Xcode 7? – tettoffensive Dec 01 '15 at 23:57
  • @tettoffensive : or 2016 with Xcode version 8.0? – cocoseis Sep 20 '16 at 13:57
  • Doesn't seem to have changed at all. – lkraider Sep 20 '16 at 16:05
  • Still trying to make this work in Xcode 15... – Nils Nielsen Aug 04 '23 at 08:51

2 Answers2

29

In Xcode 10 we can replace placeholder tokens with the same name by doing this:

  1. Highlight the first token.
  2. Add other tokens with the same name to the selection by pressing ⌥⌘E once for each token. ⌥⇧⌘E selects previous tokens.
  3. Start typing. This activates multiple cursors so each token gets replaced.

It's not as quick as replacing every token with the same name by default, but it does give you more control over what gets replaced.

Juan Arzola
  • 453
  • 4
  • 6
  • 1
    Yeah, not perfect but better than "Find and replace" method I was using before – Simon Moshenko Nov 06 '19 at 15:45
  • This selects all tokens, so if there are multiple token types they end up all being the same :(. Holding ctrl-shift and clicking lets you select the right ones. – Bryan Bryce May 17 '23 at 15:22
26

This is not possible in Xcode 4.x at the time of writing (Sept. 2011).

If you want this feature back in Xcode 4.x, please go to bugreport.apple.com and report a duplicate for this rdar that I just preported:

Summary: Xcode 4 snippets with tokens of same name should sync while filling one of them.

Steps to Reproduce:
1. Define this snippet: extern NSString * const <#constant#>; NSString * const <#constant#> = @"<#constant#>";

  1. Drop it into your code.
  2. Hit tab to select instance of token <#constant#>.
  3. Type "NSMySuperCoolConstantString".

Expected Results:
5. NSString * const <#constant#> = @"<#constant#>"; should turn into NSString * const NSMySuperCoolConstantString = @"NSMySuperCoolConstantString"; as it used to do in v3.x.

Actual Results:
5. NSString * const <#constant#> = @"<#constant#>"; stays unchanged/unsynced.

Regression:

Notes:

Here is the rdar reference for duping:
rdar://10071607

And here a copy on OpenRadar:
http://openradar.appspot.com/radar?id=1327411


While you're at it: Feel free to dupe this rdar as well:

"Xcode 4: snippet functionality regressions"
rdar://9192757
http://openradar.appspot.com/9192757

Regexident
  • 29,441
  • 10
  • 93
  • 100