Sublime Forum

Reverse Split into Lines

#1

Is there a command for the reverse operation of Split into Lines? One that would join the selection lines into a single selection?

0 Likes

#2

join_line: ctrl (or cmd on MAC OSX) + j ?

0 Likes

#3

No, that joins the selected lines. I need the selection, which was a single selection block before it was split into multiple selection lines to be a single selection block again. No mangling the selected text in any way. Just the selection.

0 Likes

#4

is soft undo (ctrl+u) okay for your use case? suppose you accidentally do a unwanted split.


otherwise you would have to write your own plugin.

import sublime
import sublime_plugin


class MergeSelectionCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sel = self.view.sel()

        if len(sel) == 0:
            return

        regions = sorted(r for r in sel)

        sel.clear()
        sel.add(sublime.Region(regions[0].begin(), regions[-1].end()))
1 Like

#5

It was the latter (no accidents). I have a ton of CREATE INDEX statements in a file that is dispersed to two lines per statement.

CREATE INDEX blablabla
  ON table_name;

I needed those to be on a single line so that I could sort them by index name. For this I used Quick Find All followed by the Join Lines you’ve already mentioned earlier. As a result, I had all the statements on a single line, selected, that is, ready to be sorted. But they could not be sorted by Sort Lines because that sorts each selection on its own. The only way to sort them was to remove line-by-line selections and re-select the whole bunch again manually.

The plugin you provided is exactly what I need. I guess. Have to put the code where it belongs first.

Anyway, thank you very much!

0 Likes

#6

You can also join selections by enlarging them until they overlap. If you just split the selection into lines, the way to procede is to expand the selection by two characters to the side the caret of the last selection is at (usually to the right) and then remove the expanded characters by doing the opposite.

1 Like

#7

Expand Selection to Line (Ctrl+l or Cmd+l depending on platform) is close to being an inverse of Split into Lines: if you already have a line selected, it’ll add the next line, and with multiple selected lines next to each other, they’ll be combined into a single selection.

Also, FWIW you can sort the selected lines without combining them into a single selection, via “Permute Selections: Sort” in the command palette (you’ll have to have more than one thing selected for it to show, however)

2 Likes

#8

All of these are working great, thanks for the suggestions.

Are there any relevant differences between the behavior of Sort Lines and Permute Selections: Sort when operating on the same set of lines (assuming one single selection for the first one and multiple selections for the second command)?

0 Likes

#9

No difference, they’ll do the same thing in that case

1 Like

#10

Thank you very much, @jps!

0 Likes