Repository: ipython/ipython

Issue:
%paste + history
From a user point of view, it seem like `%paste` should not store itself in IPython's history, but instead store the text that was pasted. It seems unlikely that a user will want to use history to get to paste again over get to the thing that was pasted.

![ipython-paste](https://user-images.githubusercontent.com/1813603/157872550-0ea6043c-c844-4e5e-b499-9cfe3e0c5ea9.gif)




Discussion:
That looks reasonable.

It seem like prompt_toolkit is keeping it's own history of what have been typed, so there might be multiple fixes needed.

Shouldn't this be configurable? I for one liked the prior behavior. I mosty paste long arbitrary code that has no relevance for future uses but now having to deal with the issue of seeing this text in the history, besides recalling older items by going through such multiline items is painful as both up arrow and Ctrl+P go line by line when it is multiline items.
BTW, I find it extremely hard to navigate through history once a long multiline entry gets in. On Mac with iTerm, PageUp and PageDown work against the iTerm buffer and Ctrl+Up and Ctrl+Down are used by mission control by default, so the only shortcuts usable by default are Ctrl+P and Ctrl+N and these also go the previous/next line when there are multiple lines so it becomes extremely tedious to use them to navigate. I have for now disabled the mission control shortcuts so that I can use Ctrl+Up and Ctrl+Down for navigating the history.
> On Mac with iTerm, PageUp and PageDown work against the iTerm buffer

For me Fn-Up/Down (which are I guess PgUp/PgDown) navigate history, and Fn-Ctrl-Up/Down control the iterm buffer.
> For me Fn-Up/Down (which are I guess PgUp/PgDown) navigate history, and Fn-Ctrl-Up/Down control the iterm buffer.

For me, the later set does nothing and I don't think I ever tweaked these shortcuts in iTerm2.

Resolving patch:
diff --git a/IPython/terminal/magics.py b/IPython/terminal/magics.py
--- a/IPython/terminal/magics.py
+++ b/IPython/terminal/magics.py
@@ -41,7 +41,7 @@ class TerminalMagics(Magics):
     def __init__(self, shell):
         super(TerminalMagics, self).__init__(shell)
 
-    def store_or_execute(self, block, name):
+    def store_or_execute(self, block, name, store_history=False):
         """ Execute a block, or store it in a variable, per the user's request.
         """
         if name:
@@ -53,7 +53,7 @@ def store_or_execute(self, block, name):
             self.shell.user_ns['pasted_block'] = b
             self.shell.using_paste_magics = True
             try:
-                self.shell.run_cell(b, store_history=True)
+                self.shell.run_cell(b, store_history)
             finally:
                 self.shell.using_paste_magics = False
 
@@ -147,7 +147,7 @@ def cpaste(self, parameter_s=''):
 
         sentinel = opts.get('s', u'--')
         block = '\n'.join(get_pasted_lines(sentinel, quiet=quiet))
-        self.store_or_execute(block, name)
+        self.store_or_execute(block, name, store_history=False)
 
     @line_magic
     def paste(self, parameter_s=''):
@@ -203,7 +203,7 @@ def paste(self, parameter_s=''):
                 sys.stdout.write("\n")
             sys.stdout.write("## -- End pasted text --\n")
 
-        self.store_or_execute(block, name)
+        self.store_or_execute(block, name, store_history=True)
 
     # Class-level: add a '%cls' magic only on Windows
     if sys.platform == 'win32':


Test patch: