Depending on the size of what you're selecting and why, multiple-cursors may give you a solution.
– Jordon BiondoMay 7 '15 at 13:09

@wasamasa I followed the instruction given in the link you provided. After the first selection, I selected the second region my keeping the Meta key. It was selected, but there are two problems: (1) the second selection seems to be not a real selection, as copying or deleting do not work. (2) it is not possible to select a third selection by this way.
– NameMay 7 '15 at 13:17

1

The Emacs Wiki has a good section about the secondary selection. Various enhancements make it more usable from the keyboard, for instance.
– DrewMay 7 '15 at 15:35

Even with the basic stuff, there's always some new things to learn! This is pretty useful, as I usually waste a lot of key pressing by travelling the kill ring when I need to do this. Wish I could upvote this some more...
– Meaningful UsernameMay 8 '15 at 7:48

You can use registers combined with the mark ring. Push the current marks with C-<SPC> C-<SPC>, recover them later with C-u C-<SPC>, or save them in a register, say b, via C-x r <SPC> b, and jump back later with C-x r j b.

Now mark another region. C-u C-x r s a will copy the marked region to register a and delete the text. Then mark the next region and C-u C-x r + a will append another selection to that register a and delete it.

The new rectangle-region support in Emacs-24.4 actually introduced (a big part of) the infrastructure needed for this functionality [ after all, a rectangular region is a non-contiguous region].

So, it should be possible now to implement a "split-region" package which provides just the functionality you're asking for.

Admittedly, lots of Emacs commands still haven't been adapted to use the new infrastructure (e.g. commands like upcase-region, or undo-in-region, query-replace, ...) and hence don't work properly on rectangles yet (so they wouldn't work properly on a "split-region" either), but these are bugs which should hopefully get fixed over time.

If the lines you want to manipulate can be distinguished via a regex match, you can use occur-mode to view them in a new window. You can then use occur-edit-mode to edit them, with the original buffer reflecting the changes that you make in the Occur buffer.

This works wonderfully, for example, in combination with multiple-cursors-mode: because the lines you've selected to appear in the Occur buffer are displayed contiguously regardless of how far they are in the original buffer, you can mark all of them or a subset and then activate mc/edit-lines and edit them all at once. (Yes, I know that multiple-cursors can do this all by itself with mc/mark-all-like-this, but it's nice to have options.)

Any other editing operation that would be more easily done if you were seeing only the lines you wanted to work on, such as setting up a complicated regex search and replace, or a macro that needs to run on each line of a buffer in turn, can also be done this way.

This responds to your general question, but not particularly to the use case you mention (moving bits of code around).

Emacs now has what it calls a noncontiguous region, which is the kind of selection that you are asking about. Vanilla Emacs does not provide any special way to create a noncontiguous region, except if it is a rectangle. But library Zones does.

Library Zones lets you define multiple zones in your buffer. A list of zones is a noncontiguous region. You can also think of it as multiple regions, depending on how you want to make use of it. You can have multiple zone lists defined at any time, each assigned to a different zones variable, and you can switch among them.

If you also use library Bookmark+ then you can save lists of zones persistently and restore them in later Emacs sessions. Bookmark+ also lets you assign arbitrary tags to a bookmark. This means that you can tag a list of zones.

The zones in a given zone list are typically in the same buffer, but they need not be. In this, a zone list is more general than a noncontiguous region. It is also more general in that each zone can contain additional information, besides just the zone limits, and it can have an identifier.

There are several ways to create a zone. One of the easiest is to use C-x n a: it defines a zone from the active region, and adds it to the current zone set. (C-x n n (narrow-to-region) also does this, in addition to narrowing.)

For completeness sake, you can achieve your reordering of paragraphs (sequentially) with marked regions and M-0 M-x transpose-paragraph (the zero argument makes tra-par swap the paragraphs where point and mark are in, i.e. the region begins and ends)!

By the way this works for all transpose-.* commands (-words, -sexp, -sentences, ...) and is extremely handy in many situations.

Another way is to use the objed package (I'm the author). You can mark any text object with m the point is moved to the next object of current type automatically. Unmark any object by navigating to it and press m again. Here is an example screencast (shows marking lines but works for other objects like paragraphs, defuns... as well):