(Or, more simply: on move line up, do what you're doing, except instead of getting the first selection in the multiple selection, find the first selection that's within the visible viewport. Similarly, on move line down, instead of getting the last selection, find the last selection that's within the visible viewport.)

BTW, I think Randy might have meant `cm.getViewport()` (not `cm.visibleLines()` - I don't see that in the CM API). Note that `Editor.getFirstVisibleLine()` is *not* the same thing - it's sort of a misleading name; it refers to the first line that's shown in the editor when scrolled to the top, which is only really used for inline editors.)