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.
6 Answers
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.
-
2For dummies like me: Pressing
F1
and starting to typeRun selected text in terminal
should bring up the command rather quickly. With the nextF1
press, it should be at the top of the command list Aug 31, 2021 at 14:52
@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
Now it's supported by default shortcut "shift" + "enter".
- select the proper code snippet
- press "shift" + "enter"
-
12
-
Probably you need to install the intellsense extension on VSCode for Python debug.– K YMay 30, 2022 at 21:28
-
-
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.
-
-
@Olsgaard I am not sure if this is provided by vs code by default. You may try to install Jupyter Extension for Visual Studio Code as well. Jan 25, 2022 at 14:36
-
this worked for me after I changed the key binding for jupyter.execSelectionInteractive to something other than shift-enter.– robbwhAug 11, 2022 at 15:18
Some language specific extensions have already an existing keybinding.
On Windows, for the PowerShell extension it is currently F8 to run the selected text.
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.– Community BotJan 9 at 13:16