diff --git a/lib/codemirror.js b/lib/codemirror.js
index 09ea0a31340c5d72967348f1699f8e068879b149..31c6d71be524e044abf0cdd5a75df2832d4847a0 100644
--- a/lib/codemirror.js
+++ b/lib/codemirror.js
@@ -2417,7 +2417,7 @@
     var multiPaste = null;
     if (cm.state.pasteIncoming && doc.sel.ranges.length > 1) {
       if (lastCopied && lastCopied.join("\n") == inserted)
-        multiPaste = lastCopied.length == doc.sel.ranges.length && map(lastCopied, splitLines);
+        multiPaste = doc.sel.ranges.length % lastCopied.length == 0 && map(lastCopied, splitLines);
       else if (textLines.length == doc.sel.ranges.length)
         multiPaste = map(textLines, function(l) { return [l]; });
     }
@@ -2433,7 +2433,7 @@
       else if (cm.state.overwrite && range.empty() && !cm.state.pasteIncoming)
         to = Pos(to.line, Math.min(getLine(doc, to.line).text.length, to.ch + lst(textLines).length));
       var updateInput = cm.curOp.updateInput;
-      var changeEvent = {from: from, to: to, text: multiPaste ? multiPaste[i] : textLines,
+      var changeEvent = {from: from, to: to, text: multiPaste ? multiPaste[i % multiPaste.length] : textLines,
                          origin: cm.state.pasteIncoming ? "paste" : cm.state.cutIncoming ? "cut" : "+input"};
       makeChange(cm.doc, changeEvent);
       signalLater(cm, "inputRead", cm, changeEvent);