Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swapping text selections in Sublime Text 2

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!

like image 805
j6m8 Avatar asked May 28 '13 20:05

j6m8


People also ask

How do I change all occurrences in Sublime Text?

Alt+F3 gives a really simple way to do find and replace: Use it to select all occurrences of the current word or selection, then just start typing to replace or edit them all at once.

How do I replace multiple lines in Sublime Text?

Select a block of lines, and then split it into many selections, one per line, using: Windows/Linux: Ctrl+Shift+L. Mac: ⇧+⌘+L.


3 Answers

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" }
like image 165
Ivan Castellanos Avatar answered Oct 04 '22 21:10

Ivan Castellanos


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"} },
like image 33
angerson Avatar answered Oct 04 '22 19:10

angerson


Every day I'm shuffling...

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

like image 24
Hugo Corrá Avatar answered Oct 04 '22 21:10

Hugo Corrá