1

I'd like to send code I have selected from the editor to the interpreter running in the debugging console during an active debugging session. Can I do this in VSCode? If so, how?

Update 1

While Mark (accepted answer) provided what seems to be the right command (it works for me from the contextual menu with the mouse), this isn't working for me yet as a keyboard binding, and I reported this issue here.

Update 2

This started working again as of April 21, 2020 (latest Insiders version).

2 Answers 2

3

See https://code.visualstudio.com/docs/python/editing#_run-selectionline-in-terminal-repl

Run Selection/Line in Terminal (REPL)

The Python: Run Selection/Line in Python Terminal command (Shift+Enter) is a simple way to take whatever code is selected, or the code on the current line if there is no selection, and run it in the Python Terminal. An identical Run Selection/Line in Python Terminal command is also available on the context menu for a selection in the editor.

VS Code automatically removes indents based on the first non-empty line of the selection, shifting all other lines left accordingly.

Source code that runs in the terminal/REPL is cumulative until the current instance of the terminal is closed.

The command opens the Python Terminal if necessary; you can also open the interactive REPL environment directly using the Python: Start REPL command. (Initial startup might take a few moments especially if the first statement you run is an import.)

On first use of the Python: Run Selection/Line in Python Terminal command, VS Code may send the text to the REPL before that environment is ready, in which case the selection or line is not run. If you encounter this behavior, try the command again when the REPL has finished loading.

And see Use IPython REPL in VS Code for info about the IPython REPL.


--------- generic info for other languages -------------------------------------------------------------

It sounds like you want to send it the repl. There is an unbound command:

editor.debug.action.selectionToRepl

which will send selected text to the debug repl.

{
  "key": "alt+y",           // whatever you want here
  "command": "editor.debug.action.selectionToRepl"
},
Sign up to request clarification or add additional context in comments.

3 Comments

Weird, this isn't working for me (in the latest version of Insiders), and I see no error or message anywhere. I guess I'll file a ticket
It works for me but I don't use python so I can't test it there.
Thanks - I think this is indeed the correct answer even if it doesn't quite work for me (I opened a ticket w/ them in case others run into a similar problem)
0

No, you cannot send a selection of code to the debugger. You need to debug the whole file or use the Interactive Window support to debug a specific cell.

5 Comments

Thanks Brett - Do you know if that's a design choice? If so, are you aware of any threads where that idea was discussed / discarded, or where the basis for ruling out this feature was stablished? Or do you think that's more of a limitation of the way these two entities (debugger console process and editor) can communicate today?
Also, I'd be interested in knowing how you would reconcile what you wrote here with the commandID that @mark posted in another answer.
Mark's answer is different because he was aware of a VS Code-specific command that I wasn't. I was speaking from the perspective of the Python extension where there isn't specific support for this.
Got it - Thanks @Brett
BTW I was coincidentally just now opening a related feature request on this topic (FYI if you are interested). Thanks again.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.