Show More
@@ -137,6 +137,11 b' class TerminalInteractiveShell(InteractiveShell):' | |||||
137 | help="Shortcut style to use at the prompt. 'vi' or 'emacs'.", |
|
137 | help="Shortcut style to use at the prompt. 'vi' or 'emacs'.", | |
138 | ).tag(config=True) |
|
138 | ).tag(config=True) | |
139 |
|
139 | |||
|
140 | emacs_bindings_in_vi_insert_mode = Bool( | |||
|
141 | True, | |||
|
142 | help="Add shortcuts from 'emacs' insert mode to 'vi' insert mode.", | |||
|
143 | ).tag(config=True) | |||
|
144 | ||||
140 | autoformatter = Unicode(None, |
|
145 | autoformatter = Unicode(None, | |
141 | help="Autoformatter to reformat Terminal code. Can be `'black'` or `None`", |
|
146 | help="Autoformatter to reformat Terminal code. Can be `'black'` or `None`", | |
142 | allow_none=True |
|
147 | allow_none=True |
@@ -18,6 +18,7 b' from prompt_toolkit.filters import (has_focus, has_selection, Condition,' | |||||
18 | vi_insert_mode, emacs_insert_mode, has_completions, vi_mode) |
|
18 | vi_insert_mode, emacs_insert_mode, has_completions, vi_mode) | |
19 | from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline |
|
19 | from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline | |
20 | from prompt_toolkit.key_binding import KeyBindings |
|
20 | from prompt_toolkit.key_binding import KeyBindings | |
|
21 | from prompt_toolkit.key_binding.bindings import named_commands as nc | |||
21 |
|
22 | |||
22 | from IPython.utils.decorators import undoc |
|
23 | from IPython.utils.decorators import undoc | |
23 |
|
24 | |||
@@ -89,8 +90,75 b' def create_ipython_shortcuts(shell):' | |||||
89 | & ~cursor_in_leading_ws |
|
90 | & ~cursor_in_leading_ws | |
90 | ))(display_completions_like_readline) |
|
91 | ))(display_completions_like_readline) | |
91 |
|
92 | |||
92 |
if sys.platform == |
|
93 | if sys.platform == "win32": | |
93 |
kb.add( |
|
94 | kb.add("c-v", filter=(has_focus(DEFAULT_BUFFER) & ~vi_mode))(win_paste) | |
|
95 | ||||
|
96 | @Condition | |||
|
97 | def ebivim(): | |||
|
98 | return shell.emacs_bindings_in_vi_insert_mode | |||
|
99 | ||||
|
100 | focused_insert = has_focus(DEFAULT_BUFFER) & vi_insert_mode | |||
|
101 | ||||
|
102 | # Needed for to accept autosuggestions in vi insert mode | |||
|
103 | @kb.add("c-e", filter=focused_insert & ebivim) | |||
|
104 | def _(event): | |||
|
105 | b = event.current_buffer | |||
|
106 | suggestion = b.suggestion | |||
|
107 | if suggestion: | |||
|
108 | b.insert_text(suggestion.text) | |||
|
109 | else: | |||
|
110 | nc.end_of_line(event) | |||
|
111 | ||||
|
112 | @kb.add("c-f", filter=focused_insert & ebivim) | |||
|
113 | def _(event): | |||
|
114 | b = event.current_buffer | |||
|
115 | suggestion = b.suggestion | |||
|
116 | if suggestion: | |||
|
117 | b.insert_text(suggestion.text) | |||
|
118 | else: | |||
|
119 | nc.forward_char(event) | |||
|
120 | ||||
|
121 | @kb.add("escape", "f", filter=focused_insert & ebivim) | |||
|
122 | def _(event): | |||
|
123 | b = event.current_buffer | |||
|
124 | suggestion = b.suggestion | |||
|
125 | if suggestion: | |||
|
126 | t = re.split(r"(\S+\s+)", suggestion.text) | |||
|
127 | b.insert_text(next((x for x in t if x), "")) | |||
|
128 | else: | |||
|
129 | nc.forward_word(event) | |||
|
130 | ||||
|
131 | # Simple Control keybindings | |||
|
132 | key_cmd_dict = { | |||
|
133 | "c-a": nc.beginning_of_line, | |||
|
134 | "c-b": nc.backward_char, | |||
|
135 | "c-k": nc.kill_line, | |||
|
136 | "c-w": nc.backward_kill_word, | |||
|
137 | "c-y": nc.yank, | |||
|
138 | "c-_": nc.undo, | |||
|
139 | } | |||
|
140 | ||||
|
141 | for key, cmd in key_cmd_dict.items(): | |||
|
142 | kb.add(key, filter=focused_insert & ebivim)(cmd) | |||
|
143 | ||||
|
144 | # Alt and Combo Control keybindings | |||
|
145 | keys_cmd_dict = { | |||
|
146 | # Control Combos | |||
|
147 | ("c-x", "c-e"): nc.edit_and_execute, | |||
|
148 | ("c-x", "e"): nc.edit_and_execute, | |||
|
149 | # Alt | |||
|
150 | ("escape", "b"): nc.backward_word, | |||
|
151 | ("escape", "c"): nc.capitalize_word, | |||
|
152 | ("escape", "d"): nc.kill_word, | |||
|
153 | ("escape", "h"): nc.backward_kill_word, | |||
|
154 | ("escape", "l"): nc.downcase_word, | |||
|
155 | ("escape", "u"): nc.uppercase_word, | |||
|
156 | ("escape", "y"): nc.yank_pop, | |||
|
157 | ("escape", "."): nc.yank_last_arg, | |||
|
158 | } | |||
|
159 | ||||
|
160 | for keys, cmd in keys_cmd_dict.items(): | |||
|
161 | kb.add(*keys, filter=focused_insert & ebivim)(cmd) | |||
94 |
|
162 | |||
95 | return kb |
|
163 | return kb | |
96 |
|
164 |
General Comments 0
You need to be logged in to leave comments.
Login now