46

I want to run the select code of python in VS Code. Till now, I have to copy the code and paste it in the terminal below to run. The system is mac OS. So is there any way to run the code directly after I selected the code? For example, like the shortcuts.

starball
  • 20,030
  • 7
  • 43
  • 238
yanachen
  • 3,401
  • 8
  • 32
  • 64

6 Answers6

88

There is no default keybindings for command "Run Selected Text in Active Terminal", but you can create one.

  • Press Ctrl+K, Ctrl+S to open File → Preferences → Keyboard Shortcuts.
  • Search for workbench.action.terminal.runSelectedText in keybindings.
  • Press the icon on the left to open a windnow with this message "Press desired key combination..." and make your choice. (I've pressed Ctrl+Alt+R - as this combination was not used yet.)
  • Press Enter to store your keybinding.

Tested on VSCode 1.30.2 on Windows 10 Pro.

slhck
  • 36,575
  • 28
  • 148
  • 201
Piotr
  • 891
  • 2
  • 7
  • 4
  • 2
    For dummies like me: Pressing `F1` and starting to type `Run selected text in terminal` should bring up the command rather quickly. With the next `F1` press, it should be at the top of the command list – gebbissimo Aug 31 '21 at 14:52
22

@yanachen, this is now possible in VS Code. All you need to do is:
1. Ensure python is running in the VS Code terminal window
2. Select the text you wish to execute in python
3. Invoke the command 'workbench.action.terminal.runSelectedText' as defined in the following link: https://code.visualstudio.com/docs/editor/integrated-terminal#_key-bindings

Don
  • 6,632
  • 3
  • 26
  • 34
12

Now it's supported by default shortcut "shift" + "enter".

  1. select the proper code snippet
  2. press "shift" + "enter"
NebulaFox
  • 7,813
  • 9
  • 47
  • 65
BinhuiLiu
  • 145
  • 1
  • 2
5

Here is the setting for running selection in "interactive window."

In my VS Code (version 1.56.2), I do the following things: go to Settings, search for interactive window, in the resulting left panel choose Jupyter, and finally check the box next to Jupyter: Send selection to interactive window. That's it. One more step for some users (including me) is to modify the keybinding for running selection to your preference. For example, got to keyboard shortcuts, type run selection, you should see a list of keybindings and you may need to redefine them if conflicts exist.

enter image description here

Li-Pin Juan
  • 1,156
  • 1
  • 13
  • 22
1

Some language specific extensions have already an existing keybinding.
On Windows, for the PowerShell extension it is currently F8 to run the selected text.

Luke
  • 1,734
  • 2
  • 15
  • 18
0

Install the vscode extension Node.js Exec. then select the block of code you want to run and press f8. worked for me.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 09 '23 at 13:16