Prevent ipython %paste from polluting shell previous commands - ipython

There seems to have been a recent change in ipython (I'm using 8.4.0) where %paste acts as if it were a literal paste (ctrl+v).
This is undesired because
I can already do direct pastes with ctrl+shift+v in my terminal
I like being able to quickly access previous manually typed commands instead of stuff pasted in.
I sometimes paste large dumps
Example
In both example I paste in a few commands and press the up arrow.
clipboard
Suppose I have the following in my clipboard
a=[1,2,3,4,5]
b=[4,5,6,7,8]
ipython 7.13.0 (desired behavior)
In [1]: %paste
a=[1,2,3,4,5]
b=[4,5,6,7,8]
## -- End pasted text --
In [2]: %paste
ipython 8.4.0 (new behavior)
In [1]: %paste
a=[1,2,3,4,5]
b=[4,5,6,7,8]
## -- End pasted text --
In [3]: a=[1,2,3,4,5]
...: b=[4,5,6,7,8]
conclusion
Are there any ipython configurations I can change to bring back the old paste magic behavior?

Related

How to clear the entire terminal (PowerShell)

I had an issue. Using the clear or cls command in powershell clears only the visible portion of the terminal,I would like to know how to clear the entire terminal?
I use VSCode by the way.
To also clear the scrollback buffer, not just the visible portion of the terminal in Visual Studio Code's integrated terminal, use one of the following methods:
Use the command palette:
Press Ctrl+Shift+P and type tclear to match the Terminal: Clear command and press Enter
Use the integrated terminal's context menu:
Right-click in the terminal and select Clear from the context menu.
On Windows, you may have to enable the integrated terminal's context menu first, given that by default right-clicking pastes text from the clipboard:
Open the settings (Ctrl+,) and change setting terminal.integrated.rightClickBehavior to either default or selectWord (the latter selects the word under the cursor before showing the context menu).
Use a keyboard shortcut from inside the integrated terminal (current as of v1.71 of VSCode):
On macOS, a shortcut exists by default: Cmd+K
On Linux and Windows, you can define an analogous custom key binding, Ctrl+K, as follows, by directly editing file keybindings.json (command Preferences: Open Keyboard Shortcuts (JSON) from the command palette), and placing the following object inside the existing array ([ ... ]):
{
"key": "ctrl+k",
"command": "workbench.action.terminal.clear",
"when": "terminalFocus && terminalHasBeenCreated || terminalFocus && terminalProcessSupported"
}
Using a command you can invoke from a shell in the integrated terminal:
Note: A truly cross-platform solution would require executing the VSCode-internal workbench.action.terminal.clear command from a shell, but I don't know how to do that / if it is possible at all - do tell us if you know.
Linux (at least as observed on Ubuntu):
Use the standard clear utility (/usr/bin/clear), which also clears the scrollback buffer.
From PowerShell, you may also use Clear-Host or its built-in alias, cls.
By contrast, [Console]::Clear() does NOT clear the scrollback buffer and clear just one screenful.
macOS:
Unfortunately, neither /usr/bin/clear nor PowerShell's Clear-Host (cls) nor .NET's [Console]::Clear() clear the scrollback buffer - they all clear just one screenful.
Print the following ANSI control sequence: '\e[2J\e[3J\e[H' (\e represents the ESC char. (0x1b, 27); e.g., from bash: printf '\e[2J\e[3J\e[H'; from PowerShell: "`e[2J`e[3J`e[H"
You can easily wrap this call in a shell script for use from any shell: create a file named, say, cclear, in a directory listed in your system's PATH variable, then make it executable with chmod a+x; then save the following content to it:
#!/bin/bash
# Clears the terminal screen *and the scrollback buffer*.
# (Needed only on macOS, where /usr/bin/clear doesn't do the latter.)
printf '\e[2J\e[3J\e[H'
Windows:
NO solution that I'm aware of: cmd.exe's internal cls command and PowerShell's internal Clear-Host command clear only one screenful in the integrated terminal (not also the scrollback buffer - even though they also do the latter in a regular console window and in Windows Terminal).
Unfortunately, the escape sequence that works on macOS ("`e[2J`e[3J`e[H" or, for Windows PowerShell, "$([char]27)[2J$([char]27)[3J$([char]27)[H") is not effective: on Windows it just clears one screenful.
(By contrast, all of these methods do also clear the scrollback buffer in regular console windows and Windows Terminal.)
right click on the powershell button,
then select clear,
when you are at the command window, type "clear" command, to clear the terminal window.

Pasting in a screen session into ipython

When running ipython 5 and 6 in a gnu-screen session, pasting multiple lines does not work.
If I run ipython in a normal terminal session, I see the following when I paste 3 import lines separated by newlines:
In [1]: import datetime
...: import os
...: import glob
...:
...:
In [2]:
If I do the same into a gnu-screen session, I see:
In [1]: import datetime
In [2]:
i.e., the results are truncated at the first newline.
Is there any ipython or screen configuration item that can be used to work around this and achieve the same paste behavior in both terminals?
EDIT: This issue has apparently been reported somewhere on the ipython mailing list, as referenced on this issue.
I ran into this problem as well with Gnu screen. The workaround I'm using is that I switched to Tmux. Tmux seems to have similar functionality to screen, and has a handy status bar that lists open virtual windows at the bottom. The default prefix key is CTRL-b.
More importantly for this question, pasting into an ipython terminal within a Tmux session works as expected for me.

How to get the current Vi mode in IPython

I'm writing a Powerline theme for IPython and I'd like to show the Vi mode in the prompt. That will be useful but is it possible? Does IPython provide a method? I couldn't find it.
This is what I want (now the INSERT segment is hard coded):
Since IPython has switched to prompt_toolkit it does not, indeed, respect .inputrc, there are plane to do so but prompt_toolkit (PTK) author need to find time (or funds to work on it...).
PTK have a vi input mode which is not perfect (Bug reports and PR welcome), and can be enable in IPython. Better than just give you the answer, here is how to find it yourself. run ipython with --help-all option, grep for vi, and keep a couple of lines after (-A2) and after before (-B2):
$ ipython --help-all | grep vi -B2 -A2
...snip...
--TerminalInteractiveShell.editing_mode=<Unicode>
Default: 'emacs'
Shortcut style to use at the prompt. 'vi' or 'emacs'.
...snip...
So there you go, you can start IPython with --TerminalInteractiveShell.editing_mode='vi', or set the configuration option in the IPython configuration file c.TerminalInteractiveShell.editing_mode='vi'.
You can also find all configuration options in the online IPython documentation.

Ctrl+c not working in integrated terminal which uses Powershell

I'm using Powershell in the integrated terminal by adding the following line to the settings.json file.
"terminal.integrated.shell.windows": "C:\\WINDOWS\\system32\\WindowsPowershell\\v1.0\\powershell.exe",
It works very well, but usually, when I'm in Powershell, typing ctrl+c cancels what I had typed and opens a new line.
But in the integrated terminal it just prints ^C.
Is there a way to fix it or find an alternative method to achieve this?
Thanks
This is with VSCode and not necessarily with the PowerShell Extension. You can see this by just using the default cmd.exe terminal, CTRL+C does nothing. It does not print the ^C at all, and creates no new line.
If you want this to work as expected in the normal command prompt or PowerShell.exe you will need to submit an issue to VSCode repository and request it.
I would expect this is all tied to the keybindings.json file. I went through that file but could not find a command available to the same function that occurs in the full command prompt or console. So this will likely need a new command added for VSCode.
If you search through the keybindings file you can see the terminal has that key CTRL+C bound to copySelection when terminalFocus && terminalTextSelected. This is why the ^C is being output, and no new line is being added.
A workaround:
Pressing Esc will erase the line back to the beginning.

IPython: run script starting from a specific line

I am writing my script interactively with IPython. This is what I currently do:
write a chunk of code,
run in ipython with "run -i file_name.py".
make changes and repeat 2 until I think it is OK .
comment out the entire previous chunk.
write new chunk of code that is based on the previous one.
go back to step 2.
......
Is there more efficient way? Can I start a script from a specific line while using all the variables in current namespace?
Use ipdb ("pip install ipdb" on the command line to install it).
Suppose you want to run script "foo.py" from line 18 to 23.
You'll want to start like this:
ipdb foo.py
Now, let's jump to line 18 (i.e., ignore all the lines before the 18th):
ipdb> j 18
Next, we set a breakpoint at line 23 (we don't want to go further):
ipdb> b 23
Finally, let's execute:
ipdb> c
Job done :)
I'd personally also use the ipython notebook, but you call also use you favorite text editor and always copy out the chunk of code you want to run and use the magic command %paste to run that chunk in the ipython shell. It will take care of indentation for you.
Use the magic of %edit stuff.py (first use) and %ed -p (after the first use) and it will invoke your $EDITOR from inside of ipython. Upon exiting from the editor ipython will run the script (unless you called %ed -x). That is by far the fastest way I found to work in CLI-ipython. The notebooks are nice, but I like having a real editor for code.
(Based on lev's answer)
From the interactive shell:
%run -i -d foo.py
should then enter the debugger, and proceed with:
j <line_number>
c
etc.
EDIT: unfortunately, this seems to sort of break ipython's magic %debug command.
An IPython Notebook allows you to interactively run scripts line by line. It comes with IPython, just run:
ipython notebook
from the terminal to launch it. Its a web interface to IPython, where you can save the notebooks to *.py files by clicking save as in the settings.
Here's some more info from this video.
For something fast as well as flexible use http://qtconsole.readthedocs.io/en/stable/
It is similar to the Jupyter notebook based on your browsers (as pointed out by #agonti and #magellan88, but presumably much faster. It also has emacs style keybindings.
I use ipdb, ipython, comupled with tmux and vim and get almost IDE like features and much faster.