{"action":"created","issue":{"url":"https://api.github.com/repos/ipython/ipython/issues/10405","repository_url":"https://api.github.com/repos/ipython/ipython","labels_url":"https://api.github.com/repos/ipython/ipython/issues/10405/labels{/name}","comments_url":"https://api.github.com/repos/ipython/ipython/issues/10405/comments","events_url":"https://api.github.com/repos/ipython/ipython/issues/10405/events","html_url":"https://github.com/ipython/ipython/issues/10405","id":216473792,"number":10405,"title":"ENTER within brackets of one-liner gives newline instead of executing","user":{"login":"mspacek","id":799467,"avatar_url":"https://avatars3.githubusercontent.com/u/799467?v=3","gravatar_id":"","url":"https://api.github.com/users/mspacek","html_url":"https://github.com/mspacek","followers_url":"https://api.github.com/users/mspacek/followers","following_url":"https://api.github.com/users/mspacek/following{/other_user}","gists_url":"https://api.github.com/users/mspacek/gists{/gist_id}","starred_url":"https://api.github.com/users/mspacek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mspacek/subscriptions","organizations_url":"https://api.github.com/users/mspacek/orgs","repos_url":"https://api.github.com/users/mspacek/repos","events_url":"https://api.github.com/users/mspacek/events{/privacy}","received_events_url":"https://api.github.com/users/mspacek/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-03-23T15:41:20Z","updated_at":"2017-03-29T14:57:34Z","closed_at":null,"body":"This could very well be due to the move away from readline, but I couldn't quite find an existing open issue on this, only an indirect mention of it [here](https://github.com/ipython/ipython/issues/2493#issuecomment-264454032):\r\n\r\nI've noticed a change of behaviour of the ENTER key in recent versions of terminal IPython. It used to be when I hit ENTER at any point on a line, the line would be executed. Now, if the cursor falls within some kind of brackets (parentheses, curly, or square), ENTER inserts a new line.\r\n```\r\nIn [1]: a = (1 + 1)\r\n# highlight the + sign with the cursor and hit ENTER:\r\nIn [2]: a = (1     \r\n   ...: + 1)\r\n```\r\nThis behaviour only really depends on the presence of the first bracket, not the second. More generally, when the cursor is to the right of an open bracket, but not at the end of the one-liner, the only way to make it execute instead of inserting a newline is to hit ALT+ENTER.\r\n\r\nFor one-liners, I'd prefer the opposite key mapping: ALT+ENTER inserts a newline, while ENTER executes regardless of cursor position (except maybe when the line ends with a colon and the cursor is at the end of the line).\r\n```\r\n{'commit_hash': '5a894b9',\r\n 'commit_source': 'installation',\r\n 'default_encoding': 'UTF-8',\r\n 'ipython_path': '/usr/local/lib/python3.5/dist-packages/IPython',\r\n 'ipython_version': '5.3.0',\r\n 'os_name': 'posix',\r\n 'platform': 'Linux-4.4.0-64-generic-x86_64-with-Ubuntu-16.04-xenial',\r\n 'sys_executable': '/usr/bin/python3',\r\n 'sys_platform': 'linux',\r\n 'sys_version': '3.5.2 (default, Nov 17 2016, 17:05:23) \\n[GCC 5.4.0 20160609]'}\r\n\r\nName: prompt-toolkit\r\nVersion: 1.0.13\r\n```"},"comment":{"url":"https://api.github.com/repos/ipython/ipython/issues/comments/290116943","html_url":"https://github.com/ipython/ipython/issues/10405#issuecomment-290116943","issue_url":"https://api.github.com/repos/ipython/ipython/issues/10405","id":290116943,"user":{"login":"ixarka","id":5183614,"avatar_url":"https://avatars0.githubusercontent.com/u/5183614?v=3","gravatar_id":"","url":"https://api.github.com/users/ixarka","html_url":"https://github.com/ixarka","followers_url":"https://api.github.com/users/ixarka/followers","following_url":"https://api.github.com/users/ixarka/following{/other_user}","gists_url":"https://api.github.com/users/ixarka/gists{/gist_id}","starred_url":"https://api.github.com/users/ixarka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ixarka/subscriptions","organizations_url":"https://api.github.com/users/ixarka/orgs","repos_url":"https://api.github.com/users/ixarka/repos","events_url":"https://api.github.com/users/ixarka/events{/privacy}","received_events_url":"https://api.github.com/users/ixarka/received_events","type":"User","site_admin":false},"created_at":"2017-03-29T14:57:34Z","updated_at":"2017-03-29T14:57:34Z","body":"In case someone else comes across this, the following code did the trick for me, stuck inside of <profile>/startup/disableSmartEnter.py\r\n\r\n```python\r\nfrom IPython import get_ipython\r\nfrom prompt_toolkit.enums import DEFAULT_BUFFER\r\nfrom prompt_toolkit.keys import Keys\r\nfrom prompt_toolkit.filters import HasFocus, HasSelection, ViInsertMode, EmacsInsertMode\r\n\r\nip = get_ipython()\r\ninsert_mode = ViInsertMode() | EmacsInsertMode()\r\n\r\ndef insert_unexpected(event):\r\n    buf = event.current_buffer\r\n    buf.accept_action.validate_and_handle(event.cli, buf)\r\n\r\n# Register the shortcut if IPython is using prompt_toolkit\r\nif getattr(ip, 'pt_cli'):\r\n    registry = ip.pt_cli.application.key_bindings_registry\r\n    registry.add_binding(Keys.Enter,\r\n                     filter=(HasFocus(DEFAULT_BUFFER)\r\n                             & ~HasSelection()\r\n                             & insert_mode))(insert_unexpected)\r\n```"}}
{"actor":{"display_login":"ixarka"}}