0

I often have to switch two values in TextMate.

Original text:

@person = company.person

Needed text:

@company = person.company

What's the easiest way to do this using Search&Replace?

Thank you.

Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152
  • Addition: An easy (but a bit tedious) way is to replace "person" with "xxx", then replace "company" with "person", then replace "xxx" with "company". – Joshua Muheim Jul 16 '13 at 15:22

1 Answers1

1

You could do a simple string search and replace; that is

Search for: @person = company.person

Replace with: @company = person.company

If you want to do something slightly more general (@A = B.A to @B = A.B for any A and B), then I would turn to regular expressions:

Search for: [@](.*) ?= ?(.*)\.\1

Replace with: @$2 = $1.$2

That will swap all pairs of the form @A = B.A to @B = A.B (regardless of spacing around the =). Make sure that the regular expressions box is ticked in the Find & Replace window.

If you only want to switch any pairs that begin with @person or @company, but not anything else, then the following will do it:

Search for: [@](person||company) ?= ?(.*)\.\1

Replace with: @$2 = $1.$2

Add more terms using the regex “or” operator, ||. You can do the same for the second value. For example, if you wanted to match only items like manager. or resources., then you’d use:

Search for: [@](.*) ?= ?(manager||resources)\.\1

Replace with: @$2 = $1.$2

Community
  • 1
  • 1
alexwlchan
  • 5,699
  • 7
  • 38
  • 49
  • Thanks, this is very explanatory. Still, it seems a bit difficult to me. It doesn't look easier than my "old way" (replace person with xxx, replace company with person, replace xxx with company). Regexes are unbelievably powerful, but they are definitely not easy to learn. – Joshua Muheim Jul 16 '13 at 15:22