3

In JupyterLab you can run code in many different ways. And I often use the option to run the active line (where the mouse pointer is) using Run > Run Selected text or current line in console with a keyboard shortcut. After doing this, the pointer jumps to the next line, and you can keep going.

Can the same thing be done using VSCode?


And just to be clear, the following is not what I'm looking for:

  1. Ctrl+Enter will run the whole cell.

  2. Shift+Enter when highlighting a line or or other parts of the code will run that part.

  3. Shift+Enter with no highlighted code runs the whole cell and inserts a new cell below the active cell.

So, how can I run only the active line without highlighting it?

A similar question has been asked here: How to run the select code in VScode?, but that sends code to the Terminal and does not provide the answer I'm looking for.

vestland
  • 55,229
  • 37
  • 187
  • 305
  • Lets say your py script contains 3 statements: print(something) print(somethingelse) print(4*5). You want to run the 3rd line without highlighting it, right? Lets say the cursor is at the end of the 3rd line and you want to execute only this line. Is my understanding correct? – Saurabh Jain May 15 '20 at 09:09
  • @SaurabhJain Yes, you are! – vestland May 15 '20 at 09:12
  • I just pressed SHIFT + Enter at the end of statement and it is executing the 3rd line. Please try – Saurabh Jain May 15 '20 at 09:31
  • 1
    @SaurabhJain I'm not seeing the same behaviour. `Shift+Enter` runs the entire cell and skips to the next cell. – vestland May 15 '20 at 09:50

1 Answers1

2

Go to file > preferences > keyboard shortcuts, search for Run Selection / Line in interactive window and assign your desired keyboard shortcut. And make sure that there are not conflicts. I found out that several tasks had Shift + Enter assigned to it, and that's what had me confused in the first place.

vestland
  • 55,229
  • 37
  • 187
  • 305