VS Code version: 1.19.1
Python Extension version: 0.9.1
Python Version: 3.6.2 anaconda
OS and version: win10 version 1709 (build 17025.1000)
ctrl+enter does nothing
ctrl+enter executes "Run Selection/Line in Python Terminal" [ / python.execSelectionInTerminal ] context menu option and indexes cursor in editor to next line, i.e. like behavior with vscode R extension environment and with vs17 ide python script support.
Output from Python output panel
Output from Console window (Help->Developer Tools menu)
I reviewed what the R 0.5.2 extension by Yuki Ueda adds to default keybindings as part of its install to enable this
{ "key": "ctrl+enter", "command": "r.runSelection", "when": "editorTextFocus && editorLangId == 'r'" }
I then opened the %appdata%CodeUserkeybindings.json user customized keyboard settings file and added this entry
{ "key": "ctrl+enter", "command": "python.execSelectionInTerminal", "when": "editorTextFocus && editorLangId == 'python'" }
After which when i have a .py file open in vscode and use keyboard shortcut ctrl+enter it successfully executes the msft python extension's "Run Selection/Line in Python Terminal" [ / python.execSelectionInTerminal ] command. The other problem with this python extension command currently is whether you execute it from the command pallet or context menu after execution it doesn't index cursor to next line in script editor but rather leaves you with cursor on the python terminal command prompt.
So my ask would be to setup this keyboard shortcut setting in the default Keybindings settings as part of the extension install so we have parity with how R works and how this is supported in vs17 ide python. Also change the "Run Selection/Line in Python Terminal" [ / python.execSelectionInTerminal ] command such that it leaves cursor on next line in script editor and not on python terminal command prompt.
Keeping focus on editor is duplicate of microsoft/vscode-python#60.
Hey folks, I see the duplicate microsoft/vscode-python#60 item as closed in January 2018 but haven't seen update to extension that addresses executing line and moving cursor to next line in source file. Likewise no default setup by extension of a ctrl+enter keybindings that wires up "when": "editorTextFocus && editorLangId == 'python'" to "command": "python.execSelectionInTerminal" and "cursorDown".
Was the closure of microsoft/vscode-python#60 based on the assumption that users would manually add settings to do these actions on ctrl+enter as shown below?
settings.json
"macros": { // update: requires macros extension by publisher:"geddski"
"pythonExecSelectionAndCursorDown": [
"python.execSelectionInTerminal",
//"vscode.window.activeTextEditor.show()",
"cursorDown"
]
},
keybindings.json
{
"key": "ctrl+enter",
//"command": [ "python.execSelectionInTerminal", "cursorDown" ],
"command": "macros.pythonExecSelectionAndCursorDown",
"when": "editorTextFocus && editorLangId == 'python'"
}
note - as part of trying to get crtl+enter keybindings.json entry to execute the multiple commands needed when it was used I tried the macros option I had seen mentioned in other articles but it produced a warning stating "command 'macros.pythonExecSelectionAndCursorDown' not found." [ update: as highlighted in latter issue comments this warning was because I did not have required macros extension by publisher:"geddski" installed ]
I have a similar issue but little diff
"macros": {
"execCurLnPy": [
"expandLineSelection",
"python.execSelectionInTerminal",
"cancelSelection"
],
"execCurSelPy": [
"Enter",
"python.execSelectionInTerminal",
"cancelSelection",
],
},
execCurLnPy works well just goes down to next line & I can just keep on hitting C-Enter
for above problem this may solve it, as u can be anywhere on the line & it automatically adv to next line.
My problem is with
execCurSelPy
fileDict = {}
soupDict = {}
for f in htmlFiles:
... with open(f) as fp:
... lns = fp.read()
... js = HTMLtoJSONParser.to_json(lns)
... soup = BeautifulSoup(lns, "html.parser")
... fileDict[f] = js
... soupDict[f] = soup
...
And just hangs unless either I select in next blank line when I send it
or I go down to terminal & hit the Enter myself.
Either is a bit of pain:
is there anyway I can attach an Enter/line feed to selection automatically from macros?
Or send a Enter/line after python.execSelectionInTerminal?:
something like python.execInTerminal(line feed)
I am unlikely to push half of loop into the terminal. So, this is desired & mimics Emacs elpy.
Thanks,
@yelled1
The "cursorDown" command will do a crlf in editor for you. I've tested linking just that command to ctrl+enter and it does what its expected to.
My blocking issue is getting two commands to execute when I use ctrl+enter keyboard shortcut. It sounds like your keybindings.json reference/use of a settings.json defined macros setting with multiple commands defined, e.g. command: "macros.execCurLnPy" and command: "macro.execCurSelPy", is successfully being executed when you use associated ctrl+enter keyboard shortcut. If so did you do something different than I did setting that up that macros definition to make it visible to the keybindings.json keyboard shortcut settings?
I ask because as noted in my earlier post i'm getting the error "command 'macros.<macro defined in settings.json>' not found." result.
If that piece was working I believe i'd have my interim workaround that facilitated the same ctrl+enter behavior as vs17 python editor experience provides.
I raised a question about why this isn't multiple commands macros associated keyboard shortcut setting isn't working for me in this SO post but have gotten no responses with solutions to it.
Ok, let me c if I can hlp w/ that.
"macros": {
"execCurLnPy": [
"expandLineSelection",
"python.execSelectionInTerminal",
"cancelSelection"
],
"execCurSelPy": [
"cursorDown",
"python.execSelectionInTerminal",
"cancelSelection",
],
"execCurLn": [
"expandLineSelection",
"workbench.action.terminal.runSelectedText",
"cancelSelection"
],
"execCurSel": [
"workbench.action.terminal.runSelectedText",
"cancelSelection",
],
},
"julia.executablePath": "/opt/julia/bin/julia",
"python.pythonPath": "/opt/miniconda3/envs/SPk/bin/python"
}
{ "key": "ctrl+enter", "command": "macros.execCurSelPy", "when": "editorTextFocus && editorHasSelection && editorLangId == 'python'" },
{ "key": "ctrl+enter", "command": "macros.execCurLnPy", "when": "editorTextFocus && !editorHasSelection && editorLangId == 'python'" },
{ "key": "ctrl+enter", "command": "macros.execCurSel", "when": "editorTextFocus && editorHasSelection && editorLangId != 'python'" },
{ "key": "ctrl+enter", "command": "macros.execCurLn", "when": "editorTextFocus && !editorHasSelection && editorLangId != 'python'" },
}
Line "command": "-editor.action.insertLineAfter", might be important as it takes out the default behavior.
Your cursorDown seems to NOT quite work. Let me know if you figure it out.
So, I am just currently placing an empty line after each loops / with etc...
Not clean but works. Let me know if u find a workaround.
Thanks,
Hi @yelled1
It was item 1) from your comment. The article I had originally found outlining use macros didn't comment on needing the "macros" extension by publisher:"geddski" installed for it to work. I added it and now things using the simple two commands I outlined in my earlier comment are providing the desired result, thanks.
We will look at adding this after microsoft/vscode-python#1349 lands.
Yes, Ctrl+Enter should move to the next line, which makes it consistent with the behavior in PTVS. The auto-move to the next line makes Ctrl+Enter a super-useful tool to step through code. Without it, the utility is much more limited.
@myusrn thank you for your macro and keybinding code. This works very nicely for me! @brettcannon, will this be integrated into VSCode, now that microsoft/vscode-python#1349 was merged?
This has been a very nice add for me personally, but feel that it has broad appeal. Avoiding the extra steps needed to install the additional extension and manually add the shortcut would be great. Loving VSCode, and seemingly small improvements like this keep me from swapping into other dev. envs. (like Jupyter Notebook, for example, when running local exploratory analysis in Python).
Cheers,
@numpynewb we haven't explicitly looked into it yet as other high-priority stuff has been cropping up as of late.
I can give it a try, can someone tell me where the json file is located? Thanks!
Hey folks, I see the duplicate microsoft/vscode-python#60 item as closed in January 2018 but haven't seen update to extension that addresses executing line and moving cursor to next line in source file. Likewise no default setup by extension of a ctrl+enter keybindings that wires up "when": "editorTextFocus && editorLangId == 'python'" to "command": "python.execSelectionInTerminal" and "cursorDown".
Was the closure of microsoft/vscode-python#60 based on the assumption that users would manually add settings to do these actions on ctrl+enter as shown below?
settings.json
"macros": { // update: requires macros extension by publisher:"geddski" "pythonExecSelectionAndCursorDown": [ "python.execSelectionInTerminal", //"vscode.window.activeTextEditor.show()", "cursorDown" ] },keybindings.json
{ "key": "ctrl+enter", //"command": [ "python.execSelectionInTerminal", "cursorDown" ], "command": "macros.pythonExecSelectionAndCursorDown", "when": "editorTextFocus && editorLangId == 'python'" }note - as part of trying to get crtl+enter keybindings.json entry to execute the multiple commands needed when it was used I tried the macros option I had seen mentioned in other articles but it produced a warning stating "command 'macros.pythonExecSelectionAndCursorDown' not found." [ update: as highlighted in latter issue comments this warning was because I did not have required macros extension by publisher:"geddski" installed ]
Many thanks!! This worked for me after installing geddiki macro extension. :)
Can you skip comments? And also run the entire function like R does?
@colstat, The settings.json is accessed by visiting file | preferences | settings [ or ctrl+, ] and then clicking the "{ }" icon in top right corner to access.
The keyboardsettings.json is accessed by visiting file | preferences | keyboard shortcuts [ or ctrl+k ctrl+s ] and then clicking the edit keybindings.json link.
The required macros extension is accessed by clicking extensions icon on vscode left side navigation pane [ or ctrl+shift+x ] and entering "macros geddski" in search box. The first returned result should be the macros 1.2.1 extension authored by geddski.
Once the macros extension and associated settings.json and keybindings.json content is in place the ctrl+enter when working with a .py file operates just like R Studio, VsCode R language support and Visual Studio R language support does where ctrl+enter executes whatever you have highlighted and then advances ide cursor to next line in source file.
I'm finding that using ctrl+enter on a .py source file comment line still sends it to the terminal and doesn't complete line execution until the next non-comment line is sent to terminal.
Is there any simple way to fix this? I am using Mac, the 'shift+enter' always uses jupter which I do not want to (I mean I want to use terminal instead). And also, I have to select the full text before that, else it just jump to next line.
Is there any simple way to fix this? I am using Mac, the 'shift+enter' always uses jupter which I do not want to (I mean I want to use terminal instead). And also, I have to select the full text before that, else it just jump to next line.
At first time, when I start to use VSC, it works fine. I can run each line by 'shift+enter' and was run in terminal. But next time I open VSC, things changed, I can't now. Weird...
Also wanted to pitch in with what @kraigb said -- running (shift-enter) the line where the cursor is should move the cursor down a line (which is also similar to the behavior in Spyder). However, if there is text selected, it should remain on that selection
Additionally, this would match the behavior of the Python Interactive Window, where ctrl-enter is "Run Cell and Move to Next Cell"
@yeswzc Use the "python.dataScience.sendSelectionToInteractiveWindow": true,
ideally this would be indent aware too so that if I execute the first line of a code block the whole block is sent to the kernel (so that you can actually step through your code)
for i in range(2):
### some code
for j in range(2): ### execution on this line should run the loop
print(j)
print('this is still part of the group')
print('cursor ends up here')
Agreed with both @daeh and @amccaugh here.
One thing to note though: in order to be consistent with Jupyter Notebook / JupyterLab behaviour (which many of Python users are very used to by now, so it might be considered a win here), wouldn't it make sense to make
This will then match how it works in Jupyter cells, so there's no need to invent a bicycle.
Our latest release binds CTRL+ENTER to Run Cell. SHIFT+ENTER already did run current cell and advance:

@rchiodo Will SHIFT+ENTER also run + advance for lines and blocks of code when not setup as cells?
No, SHIFT+ENTER without cells does not move the cursor.
Most helpful comment
ideally this would be indent aware too so that if I execute the first line of a code block the whole block is sent to the kernel (so that you can actually step through your code)