6

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.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • Related bug report on Pycharm's bug tracker: https://youtrack.jetbrains.com/issue/PY-23114 – AXO Sep 03 '17 at 18:19

2 Answers2

2

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

taper
  • 528
  • 5
  • 22
1

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)

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
  • Although it helps, it doesn't solve the problem completely since it works only if you want to keep the exact inverse order. If one wants to swap the order of a few lines, that must be done afterwards. But it is definitely better than what I started with. Say I want lines 1, 5, 10 and 15, but written in the order 15, 10, 1 and 5. The example is minimalistic but I do have this kind of situation (with larger number of lines involved and a few of them not reversed sometimes. – Marcelo do Pagode Apr 15 '16 at 17:59
  • Another example of where it would break down: If you entered on the console a multiline command, say a dict with entries by line: `d = {k0: v0,\n `k1: v1\n` `k2: v2}` with your anwser this would be reversed as well, with the closing `}` appearing before the dict opening `d = {k0: v0,` I'm giving a +1 since it helps, but I'm not accepting as an answer yet since it doesn't solve it per se. Nonetheless, thanks a lot!!! – Marcelo do Pagode Apr 15 '16 at 18:02