Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing multiple lines of input in pycharm console from first line

When working with the interactive console in PyCharm (via IPython), is there any way to execute the currently loaded multiline statement from not the last line?

In Jupyter's QtConsole, this can be done by pressing Shift+Enter on any line of input. i.e.

In [1]: print("line 1")
   ...  print("line 2")

Can be evaluated by pressing Shift+Enter regardless of where the caret is located. In the IPython-based interactive console in PyCharm, pressing Shift+Enter creates a new line below the current one and moves the caret to it, while Ctrl+Enter splits the line at the current caret position.

I could not find any settings that control this behavior; perhaps it is a limitation of IPython?

EDIT: In the regular IPython REPL, Alt+Enter accomplishes what I'm after, but this does not work in the PyCharm console, even after removing the default shortcut assigned to that keystroke ("Show Intention Actions")

like image 209
jat255 Avatar asked Feb 13 '19 20:02

jat255


2 Answers

Perhaps this link can help you:

https://www.jetbrains.com/help/pycharm/loading-code-from-editor-into-console.html

pressing Alt + Shift + E on a selection will execute the selected code in the console.

like image 132
Mostafa Lotfi Avatar answered Sep 23 '22 16:09

Mostafa Lotfi


If there is no direct shortcut for this, at least you can do it with a workaround:

  • Position to start with (call it PositionEND, since it is the end of the selection that you must paradoxically start with): Put the cursor in the end position up to which you want to run your code.
  • Make an edit of whatever choice, it could be just pressing a letter, and then revert the edit. This puts this position as the last edit position in memory. This "last edit"-trick is to get back to that position after the execution of the following selection.
  • Now press Ctrl+Shift+Pos1, or if you want to run all, take Ctrl+A instead.
  • Execute the selection with Alt+Shift+E.
  • Get back to the "last edit"-trick position Ctrl+Shift+Backspace to go to the last edit according to the question body at Keyboard shortcut for Jump to Previous View Location (Navigate back/forward) in IntelliJ IDEA

If you have selected some code, and you want to get to the bottom of the selection; or if you have just applied Alt+Shift+E and you realise that you have forgotten to edit something at PositonEND in order to use the trick above with Ctrl+Shift+Backspace, and you want to go back to PositionEND, you must simply copy / cut and paste the selected code, that will put your cursor to the end of the selection:

  • Ctrl+c (Ctrl+x would work as well)
  • Ctrl+v

By the way, if you are in somewhat else than in the editor and you want to turn back, do not click inside the editor, but just click on the needed ".py"-tab then so that your latest selection information does not get lost.


If

  • you can take a "risk" / try your luck /
  • coming back to a nearby place is enough,

you may use Ctrl+Alt+← (Left Arrow), though this is very often not helping you to get to that exact previous cursor place, being confusingly called: "Jump to Previous View Location". It does not always do what you would expect (I think).

like image 41
questionto42standswithUkraine Avatar answered Sep 22 '22 16:09

questionto42standswithUkraine