Replace parameter multiple times in Xcode code snippets

XcodeXcode4Code Snippets

Xcode Problem Overview


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.

Xcode Solutions


Solution 1 - Xcode

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#>"; > > 2. Drop it into your code. > 3. Hit tab to select instance of token <#constant#>. > 4. 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

Solution 2 - Xcode

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlkraiderView Question on Stackoverflow
Solution 1 - XcodeRegexidentView Answer on Stackoverflow
Solution 2 - XcodeJuan ArzolaView Answer on Stackoverflow