Skip to content
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

Closed
myusrn opened this issue Dec 23, 2017 · 23 comments

Comments

@myusrn
Copy link

myusrn commented Dec 23, 2017

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:

  • open .py file in vscode
  • place cursor on a line of script
  • enter "ctrl+enter"

Logs

Output from Python output panel

Output from Console window (Help->Developer Tools menu)

@myusrn
Copy link
Author

myusrn commented Dec 23, 2017

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%\Code\User\keybindings.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.

@DonJayamanne
Copy link
Contributor

Keeping focus on editor is duplicate of microsoft/vscode-python#60.

@DonJayamanne DonJayamanne self-assigned this Jan 9, 2018
@DonJayamanne DonJayamanne changed the title enabling ctrl+enter to execute current line in .py script enabling ctrl+enter to execute current line in .py script and moving cursor to next line Jan 9, 2018
@DonJayamanne DonJayamanne removed their assignment Jan 9, 2018
@myusrn
Copy link
Author

myusrn commented Feb 16, 2018

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 ]

@yelled1
Copy link

yelled1 commented Feb 19, 2018

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,

@myusrn
Copy link
Author

myusrn commented Feb 19, 2018

@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.

@yelled1
Copy link

yelled1 commented Feb 19, 2018

Ok, let me c if I can hlp w/ that.

  1. see if u have macros 1.2.1 installed & enabled (dumb but always ck simple)

  2. settings.json should have
    {
    "window.zoomLevel": 1, //-1 makes small
    "editor.fontSize": 11,
    "terminal.integrated.fontSize": 11,

    "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"
    }

  3. keybinding.json
    {"key": "ctrl+enter",
    "command": "-editor.action.insertLineAfter",
    "when": "editorTextFocus && !editorReadonly"

    { "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,

@myusrn
Copy link
Author

myusrn commented Feb 20, 2018

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.

@brettcannon
Copy link
Member

We will look at adding this after microsoft/vscode-python#1349 lands.

@kraigb
Copy link

kraigb commented Apr 24, 2018

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.

@numpynewb
Copy link

numpynewb commented Oct 12, 2018

@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,

@brettcannon
Copy link
Member

@numpynewb we haven't explicitly looked into it yet as other high-priority stuff has been cropping up as of late.

@cui-colin
Copy link

I can give it a try, can someone tell me where the json file is located? Thanks!

@matthewgson
Copy link

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. :)

@cui-colin
Copy link

Can you skip comments? And also run the entire function like R does?

@myusrn
Copy link
Author

myusrn commented Feb 13, 2019

@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.

@yeswzc
Copy link

yeswzc commented Mar 1, 2019

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.

@yeswzc
Copy link

yeswzc commented Mar 1, 2019

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...

@amccaugh
Copy link

amccaugh commented Mar 30, 2019

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,

@daeh
Copy link

daeh commented Apr 2, 2019

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')

@aldanor
Copy link

aldanor commented May 19, 2019

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

  • Shift-Enter - execute and move down to the next cell/line/block/whatever
  • Ctrl-Enter - execute and stay

This will then match how it works in Jupyter cells, so there's no need to invent a bicycle.

@rchiodo
Copy link
Contributor

rchiodo commented May 31, 2019

Our latest release binds CTRL+ENTER to Run Cell. SHIFT+ENTER already did run current cell and advance:
image

@vnijs
Copy link

vnijs commented Jun 1, 2019

@rchiodo Will SHIFT+ENTER also run + advance for lines and blocks of code when not setup as cells?

@rchiodo
Copy link
Contributor

rchiodo commented Jun 3, 2019

No, SHIFT+ENTER without cells does not move the cursor.

@rchiodo rchiodo closed this as completed Aug 6, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Aug 14, 2019
@microsoft microsoft unlocked this conversation Nov 14, 2020
@DonJayamanne DonJayamanne transferred this issue from microsoft/vscode-python Nov 14, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests