29

Forgive me if this has been asked before; I've looked around and while I can find (vague) documentation for swapping lines in Sublime Text, I can't see anything about swapping selections.

For instance, say I have the following text:

<div class="fade-black">...</div>
<div class="fade-white">...</div>

and I want to switch the colors of the two divs. So I select black and white. Now what? Is there anything built in, or should I be looking into writing an add-on myself? (I know that this particular example is relatively simple, but I have a lot of needs for this type of function in my code, and I can't simply swap lines because the content is not otherwise identical.)

Hopefully this helps some others out too -- definitely seems like a nice feature to know about!

j6m8
  • 2,261
  • 2
  • 26
  • 34

3 Answers3

65

To swap two (or more) selections you need to use the transpose command, by default you can call it pressing ctrl + t (works on Sublime Text 3 too)

{ "keys": ["ctrl+t"], "command": "transpose" }
Ivan Castellanos
  • 8,041
  • 1
  • 47
  • 42
17

As mentioned in Joe's comment to Hugo's answer, you can swap two selections via the Edit -> Permute Selections -> Reverse menu item. Shuffle won't always change the ordering as you want it to.

You can map this operation to a key by adding something like this to your Preferences -> Key Bindings – User file, replacing f8 with a key binding of your choice:

{ "keys": ["f8"], "command": "permute_selection", "args": {"operation": "reverse"} },
angerson
  • 7,342
  • 1
  • 21
  • 24
2

Every day I'm shuffling...

Select both statements, Edit -> Permute Selections -> Shuffle.

Hugo Corrá
  • 14,546
  • 3
  • 27
  • 39
  • Seems like reverse would be the better option in this case. Especially if there were more than 2 items selected. – Joe Bergevin May 28 '13 at 21:37
  • 1
    The shuffle command doesn't guarantee an ordering change—shuffle enough times on two selections, and nothing will happen. As @JoeBergevin said, reversing is better. – angerson May 28 '13 at 21:51
  • Thank you -- even if enough shuffling gets you back to where you started, I never knew this feature existed, and I expect this to save me a ton of time in the future. – j6m8 May 29 '13 at 03:01