Search code examples
textmatetextmate2

Switch two values using Search&Replace in TextMate2?


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.


Solution

  • 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