-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enabling ctrl+enter to execute current line in .py script and moving cursor to next line #3897
Comments
I reviewed what the R 0.5.2 extension by Yuki Ueda adds to default keybindings as part of its install to enable this I then opened the %appdata%\Code\User\keybindings.json user customized keyboard settings file and added this entry 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
keybindings.json
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 execCurLnPy works well just goes down to next line & I can just keep on hitting C-Enter My problem is with
I am unlikely to push half of loop into the terminal. So, this is desired & mimics Emacs elpy. |
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.
|
Hi @yelled1 |
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! |
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. |
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. |
@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. |
Environment data
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)
Actual behavior
ctrl+enter does nothing
Expected behavior
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.
Steps to reproduce:
Logs
Output from
Python
output panelOutput from
Console window
(Help->Developer Tools menu)The text was updated successfully, but these errors were encountered: