Show More
@@ -228,3 +228,45 b' a :ref:`startup file <startup_files>`::' | |||
|
228 | 228 | For more information on filters and what you can do with the ``event`` object, |
|
229 | 229 | `see the prompt_toolkit docs |
|
230 | 230 | <http://python-prompt-toolkit.readthedocs.io/en/latest/pages/building_prompts.html#adding-custom-key-bindings>`__. |
|
231 | ||
|
232 | ||
|
233 | Enter to execute | |
|
234 | ---------------- | |
|
235 | ||
|
236 | In the Terminal IPython shell – which by default use the ``prompt_toolkit`` | |
|
237 | interface, the semantic of pressing the :kbd:`Enter` key can be ambiguous, in | |
|
238 | some case :kbd:`Enter` should execute code, and in others it should add a new | |
|
239 | line. IPython is using some heuristics to decide whether to execute or insert a | |
|
240 | new line at cursor position. For example, if we detect that the current code is | |
|
241 | not valid Python, then the user is likely editing code and the right behavior is | |
|
242 | to likely to insert a new line. If the current code is a simple statement like | |
|
243 | `ord('*')`, then the right behavior is likely to execute. Though the exact | |
|
244 | desired semantics often varies from users to users. | |
|
245 | ||
|
246 | As the exact behavior of :kbd:`Enter` is subject to disagreement, it has been | |
|
247 | special cased in order for users to completely configure the behavior they like. | |
|
248 | Hence you can have enter to simply alway execute code, if you prefer more fancy | |
|
249 | behavior : start your coffee machine on even days of odd month. You'll have to | |
|
250 | get your hands dirty and read prompt_toolkit and IPython documentation though. | |
|
251 | See :ghpull:`10500`, set the `c.TerminalInteractiveShell.handle_return` option | |
|
252 | and get inspiration from the following example that insert new lines only after | |
|
253 | a pipe (``|``). Place the following in your configuration to do so:: | |
|
254 | ||
|
255 | def new_line_after_pipe(shell): | |
|
256 | # shell is the same as get_ipython() | |
|
257 | def insert(event): | |
|
258 | """When the user presses return, insert""" | |
|
259 | b = event.current_buffer | |
|
260 | d = b.document | |
|
261 | ||
|
262 | # if character before cursor is `|` | |
|
263 | if d.text[d.cursor_position-1] == '|': | |
|
264 | # insert a new line | |
|
265 | b.insert_text('\n') | |
|
266 | else: | |
|
267 | # otherwise execute. | |
|
268 | b.accept_action.validate_and_handle(event.cli, b) | |
|
269 | return insert | |
|
270 | ||
|
271 | # set the heuristic to our new function | |
|
272 | c.TerminalInteractiveShell.handle_return = new_line_after_pipe |
General Comments 0
You need to be logged in to leave comments.
Login now