49

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 6

91

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.

1
  • 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, 2021 at 14:52
23

@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

1
  • I find it seems to be cutting my last quote off in my string.
    – FilBot3
    Sep 15, 2020 at 14:49
12

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

  1. select the proper code snippet
  2. press "shift" + "enter"
4
  • 12
    This removes my code too, using Windows 10 and version 1.43.2
    – johnckane
    Mar 26, 2020 at 20:49
  • Probably you need to install the intellsense extension on VSCode for Python debug.
    – K Y
    May 30, 2022 at 21:28
  • It actually works on my Windows 10, version 21H2. Oct 6, 2022 at 12:13
  • works on debian
    – Johan
    May 3 at 10:43
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

3
  • Which extensions do you have installed?
    – Olsgaard
    Jan 25, 2022 at 9:51
  • @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.
    – robbwh
    Aug 11, 2022 at 15:18
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.

0

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

1
  • 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 Bot
    Jan 9 at 13:16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.