diff --git a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java
index c04dd26ad042..c1bfba61af7b 100644
--- a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java
+++ b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java
@@ -58,7 +58,7 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
final int line = document.getLineNumber(selectionModel.getSelectionStart());
final int lineOffset = getLineStartSafeOffset(document, line);
final PsiElement ws = file.findElementAt(lineOffset);
- int offset = ws instanceof PsiWhiteSpace? ws.getTextRange().getEndOffset() : caretOffset;
+ int offset = ws instanceof PsiWhiteSpace? ws.getTextRange().getEndOffset() : selectionModel.getSelectionStart();
if (text.equals(selectionModel.getSelectedText())) return text;
caretModel.moveToOffset(offset);
selectionModel.setSelection(offset, selectionModel.getSelectionEnd());
diff --git a/python/testData/copyPaste/ReplaceSelection.after.py b/python/testData/copyPaste/ReplaceSelection.after.py
new file mode 100644
index 000000000000..503f64c1b012
--- /dev/null
+++ b/python/testData/copyPaste/ReplaceSelection.after.py
@@ -0,0 +1,4 @@
+a = 1
+b = 1
+a = 1
+b = 1
diff --git a/python/testData/copyPaste/ReplaceSelection.dst.py b/python/testData/copyPaste/ReplaceSelection.dst.py
new file mode 100644
index 000000000000..c744cd35f678
--- /dev/null
+++ b/python/testData/copyPaste/ReplaceSelection.dst.py
@@ -0,0 +1,4 @@
+a = 1
+b = 1
+c = 1
+
\ No newline at end of file
diff --git a/python/testData/copyPaste/ReplaceSelection.src.py b/python/testData/copyPaste/ReplaceSelection.src.py
new file mode 100644
index 000000000000..d054631bf997
--- /dev/null
+++ b/python/testData/copyPaste/ReplaceSelection.src.py
@@ -0,0 +1,3 @@
+a = 1
+b = 1
+c = 1
diff --git a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java
index 4f0887ad1e3d..2ccc5910f558 100644
--- a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java
+++ b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java
@@ -263,6 +263,10 @@ public class PyCopyPasteTest extends PyTestCase {
doTest();
}
+ public void testReplaceSelection() { //PY-8744
+ doTest();
+ }
+
private void doTest() {
String name = getTestName(false);