Jeffkrop
Jeffkrop

Reputation: 389

python Shift + enter not working in VScode with jupyter

I have a new install of VS Code Version 1.50.1 with the python extension that now added the Jupyter extension. The Jupyter extension build number is 2020.12.411183115 When I press shift enter on the default it adds a new line below. You can see in the video that shift + enter should work to run the line. At this point the only way I can run code in the interactive window is with ctrl + shift + p and select run selected cell.

Edit after working with Danny Varod and the comments below:
Changing the keyboard shortcut to ctrl + enter and nothing happens (it does not add a new line below). I press ctrl + shift + p and I see that the correct shortcut is crtl + enter but it will not trigger the action

enter image description here

Here is a screenshot of my keyboard shortcuts before the change.

enter image description here

Changing my keyboard shortcuts to match the comment below. Now when I press ctrl + enter nothing happens.

enter image description here

Upvotes: 18

Views: 23706

Answers (8)

Han Qi
Han Qi

Reputation: 501

jupyter execution selection setting

Even though settings UI hardcodes description as shift+enter. I believe it is actually referring to the shortcut associated with the setting Jupyter: Run Selection/Line in Interactive Window I changed that shortcut from default shift+enter to ctrl+shift+enter (the only setting using this shortcut) to eliminate errors due to clashing shortcut. After this shortcut change, the previous checkbox responds to ctrl+shift+enter instead

jupyter run selection in interactive window setting

Another possible solution that does not involve checking "jupyter.interactiveWindow.textEditor.executeSelection": true came from a brute force ablation study where i gradually removed conditions from the when of the Shortcuts using right-click Change When Expression from Keyboard Shortcuts UI. Removing && jupyter.ownsSelection from Jupyter: Run Selection/Line in Interactive Window makes the shift+enter (ctrl+shift+enter for me) work too.

Can anyone explain how to learn what when expressions like jupyter.ownsSelection refer to?

Overall i still prefer the checkbox method which is less error prone than manipulating when

Upvotes: 0

Jill Cheng
Jill Cheng

Reputation: 10372

Please use the following shortcut key settings:

{
    "key": "shift+enter",
    "command": "jupyter.execSelectionInteractive",
    "when": "editorTextFocus"
},

This shortcut key is set with the use conditions, and it can be used only when it is confirmed (including the control panel is opened). Therefore, we can remove the use conditions for this shortcut key.

enter image description here

Upvotes: 7

PaulMest
PaulMest

Reputation: 15065

Looks like there is a new setting to toggle this behavior. I just had to add this line to the settings file:

Configure for Workspace

// file: .vscode/settings.json
{
  "jupyter.interactiveWindow.textEditor.executeSelection": true
}

Set as default for user

  1. Open command palette and selected "Preferences: Open User Settings" enter image description here
  2. Type "Jupyter execute selection" enter image description here
  3. Ensure the checkbox is checked

Upvotes: 2

Igor Micev
Igor Micev

Reputation: 1672

In settings.json under .vscode add the following line:

"jupyter.interactiveWindow.textEditor.executeSelection": true

Then Shift+Enter should work.

Upvotes: 23

Phil
Phil

Reputation: 666

For me, it was solved by removing the Command Insert Line Bellow which was supposedly conflicting with my shift + enter hotkey in notebooks.

Upvotes: 1

ShirinJZ
ShirinJZ

Reputation: 111

In my case, the problem was with extensions; I installed Python and Jupyter from the Extension menu on the left panel, then it worked.

Note: I had already added the following lines to ‍‍‍‍setting.json:

"jupyter.sendSelectionToInteractiveWindow": true

Upvotes: 0

nbss
nbss

Reputation: 1

What you need to change is Notebook: Execute cell:

enter image description here

Upvotes: -1

Danny Varod
Danny Varod

Reputation: 18118

Shift+Enter also inserted a new line for me, unmapping this prevented this.

I changed to mappings to:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

These mappings work for me, however, in Jupyter notebooks (.ipynb) the Ctrl+Enter and Shift+Enter are swapped vs. what I defined 🤔.

Also, I am using the following Python extensions:

  • "MagicPython" magicstack.magicpython
  • "Jupyter" ms-toolsai.jupyter

Both installed by:

  • "Python Extension Pack" donjayamanne.python-extension-pack

When I open .ipynb files they open as Jupyter notebooks. When I open .py files I can either use Jupyter or MagicPython (and nether say Python).

enter image description here

enter image description here

Upvotes: 3

Related Questions