 
         # Make sure the inserted text doesn't go into an existing fold
         if fold = @displayBuffer.largestFoldStartingAtBufferRow(precedingBufferRow)
-          rangesToRefold.push(fold.getBufferRange().translate([linesRange.getRowCount(), 0]))
+          rangesToRefold.push(fold.getBufferRange().translate([linesRange.getRowCount() - 1, 0]))
           fold.destroy()
 
         # Delete lines spanned by selection and insert them on the preceding buffer row

        for rangeToRefold in rangesToRefold
          @displayBuffer.createFold(rangeToRefold.start.row, rangeToRefold.end.row)

        for selection, i in selectionsToMove
          newSelectionRanges.push(rangesOfSelectionsToMove[i].translate([-insertDelta, 0]))

      @setSelectedBufferRanges(newSelectionRanges)


      while selections.length > 0
        # Find selections spanning a contiguous set of lines
        selection = selections.shift()
        selectionsToMove = [selection]

        while selection.end.row is selections[0]?.start.row
          selection = selections.shift()
          selectionsToMove.push(selection)

        # Compute the range spanned by all these selections...
        linesRangeStart = [selection.start.row, 0]
        if selection.end.row > selection.start.row and selection.end.column is 0
          # Don't move the last line of a multi-line selection if the selection ends at column 0
          linesRange = new Range(linesRangeStart, selection.end)
        else
          linesRange = new Range(linesRangeStart, [selection.end.row + 1, 0])

        # If selected line range is preceded by a fold, one line above on screen
        # could be multiple lines in the buffer.
