Last active
September 14, 2016 13:44
-
-
Save konradkonrad/7143fa8407804e37132e4ea90175f2d8 to your computer and use it in GitHub Desktop.
restore yank-last-arg in ipython 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~/.ipython/profile_default/startup/01-esc-dot.py | |
from IPython import get_ipython | |
from prompt_toolkit.enums import DEFAULT_BUFFER | |
from prompt_toolkit.keys import Keys | |
from prompt_toolkit.filters import HasFocus, HasSelection, ViInsertMode, EmacsInsertMode | |
ip = get_ipython() | |
insert_mode = ViInsertMode() | EmacsInsertMode() | |
class State(object): | |
def __init__(self): | |
self.depth = 0 | |
state = State() | |
def reset_last_arg_depth(): | |
state.depth = 0 | |
def yank_last_arg(event): | |
"""'readline'-style yank-last-arg: | |
Insert last argument to the previous command (the last word of the previous history entry). | |
Successive calls to yank-last-arg move back through the history list, inserting the last | |
word of each line in turn. | |
see: https://www.gnu.org/software/bash/manual/bashref.html#Commands-For-History | |
note: this doesn't support the numeric argument option of readline's yank-last-arg | |
""" | |
b = event.current_buffer | |
hist = ip.history_manager.input_hist_raw | |
if state.depth == 0: | |
state.depth = 1 | |
else: | |
state.depth += 1 | |
if len(hist) and len(hist) >= state.depth: | |
lastline = hist[-state.depth] | |
if state.depth > 1: | |
b.undo() | |
b.save_to_undo_stack() | |
if len(lastline.split()): | |
b.insert_text(lastline.split()[-1]) | |
if getattr(ip, 'pt_cli'): | |
registry = ip.pt_cli.application.key_bindings_registry | |
registry.add_binding(Keys.Escape, u'.', | |
filter=(HasFocus(DEFAULT_BUFFER) | |
& ~HasSelection() | |
& insert_mode))(yank_last_arg) | |
registry.add_binding(Keys.Escape, u'_', | |
filter=(HasFocus(DEFAULT_BUFFER) | |
& ~HasSelection() | |
& insert_mode))(yank_last_arg) | |
ip.events.register('post_execute', reset_last_arg_depth) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very interesting, do you know if one could also add operate-and-get-next (C-o)?