Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invert console history order in pycharm for copy pasting?

Tags:

python

pycharm

In Pycharm, the console history has entries from newest (top) to oldest (bottom). Which is fine in a way, but it's horrible for copy pasting several lines of code from history. What happens is that you get your code flow upside down when copying from history. Basically, you have to copy+paste one line at a time, at the cost of opening the history and scrolling to the desired line every single time.

It doesn't matter if you Ctrl select your lines in the order you want them to be re-entered. The console history pop-up will sort them according to the order shown (i.e., newest on top, oldest in the bottom).

Example: Say you ran the following two lines on console

import pandas as pd
df = pd.read_csv('path_to_file')

When you look it up on history, this is what you'll see:

1 df = pd.read_csv('path_to_file')
2 import pandas as pd

So, if you select those two lines to paste it in the console or in your script, they'll be in the incorrect order, breaking down code flow.

I have searched for a way to either: (1) invert how console history is displayed (i.e., oldest (top) to newest (bottom)). (2) preserve selecting order (i.e, ignore position on history, order by Ctrl+click, so that in the example above I could select line #2 first, line #1 second and this order would be preserved for pasting).

Applications:

a) Rerun previously entered code slices in console;

b) copy from console history to script file.

like image 254
Marcelo do Pagode Avatar asked Apr 15 '16 15:04

Marcelo do Pagode


People also ask

How do I use python console in PyCharm?

You can assign a shortcut to open Python console: press Ctrl+Alt+S , navigate to Keymap, specify a shortcut for Main menu | Tools | Python or Debug Console. The main reason for using the Python console within PyCharm is to benefit from the main IDE features, such as code completion, code analysis, and quick fixes.

How do I restart PyCharm terminal?

From the main menu, select File | Manage IDE Settings | Restore Default Settings. Alternatively, press Shift twice and type Restore default settings . Click Restore and Restart. The IDE will be restarted with the default configuration.

How do I run a single line of code in PyCharm?

The trick is to press Alt + Shift + E all at the same time.


2 Answers

Another option is that if you have ipython installed, then your python console will by default use ipython. And you could use the %history magic command of ipython to print out your history for copy.

c.f. http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-history

like image 133
taper Avatar answered Sep 19 '22 03:09

taper


just write a short code to reverse it:

#triple string quotes over multiple lines
code= """
df = pd.read_csv('path_to_file')
import pandas as pd
""" #end of multiline quote

split_by_line = code.split("\n")

split_by_line.reverse()

print("\n".join(split_by_line))

note: I have never worked with pycharm so this maay not work properly for blocks (if, for etc)

like image 42
Tadhg McDonald-Jensen Avatar answered Sep 18 '22 03:09

Tadhg McDonald-Jensen