Skip to content

Commit 4ec1a1e

Browse files
committed
add emacs keybindings to vi insert mode
1 parent 86e1571 commit 4ec1a1e

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

Diff for: ptpython/key_bindings.py

+68
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212
from prompt_toolkit.key_binding import KeyBindings
1313
from prompt_toolkit.keys import Keys
14+
from prompt_toolkit.key_binding.bindings import named_commands as nc
1415

1516
from .utils import document_is_multiline_python
1617

@@ -256,6 +257,73 @@ def _(event):
256257
python_input.show_sidebar = False
257258
event.app.layout.focus_last()
258259

260+
@Condition
261+
def ebivim():
262+
return True #repl.emacs_bindings_in_vi_insert_mode
263+
264+
focused_insert = has_focus(DEFAULT_BUFFER) & vi_insert_mode
265+
266+
# Needed for to accept autosuggestions in vi insert mode
267+
@handle("c-e", filter=focused_insert & ebivim)
268+
def _(event):
269+
b = event.current_buffer
270+
suggestion = b.suggestion
271+
if suggestion:
272+
b.insert_text(suggestion.text)
273+
else:
274+
nc.end_of_line(event)
275+
276+
@handle("c-f", filter=focused_insert & ebivim)
277+
def _(event):
278+
b = event.current_buffer
279+
suggestion = b.suggestion
280+
if suggestion:
281+
b.insert_text(suggestion.text)
282+
else:
283+
nc.forward_char(event)
284+
285+
@handle("escape", "f", filter=focused_insert & ebivim)
286+
def _(event):
287+
b = event.current_buffer
288+
suggestion = b.suggestion
289+
if suggestion:
290+
t = re.split(r"(\S+\s+)", suggestion.text)
291+
b.insert_text(next((x for x in t if x), ""))
292+
else:
293+
nc.forward_word(event)
294+
295+
# Simple Control keybindings
296+
key_cmd_dict = {
297+
"c-a": nc.beginning_of_line,
298+
"c-b": nc.backward_char,
299+
"c-k": nc.kill_line,
300+
"c-w": nc.backward_kill_word,
301+
"c-y": nc.yank,
302+
"c-_": nc.undo,
303+
}
304+
305+
for key, cmd in key_cmd_dict.items():
306+
handle(key, filter=focused_insert & ebivim)(cmd)
307+
308+
# Alt and Combo Control keybindings
309+
keys_cmd_dict = {
310+
# Control Combos
311+
("c-x", "c-e"): nc.edit_and_execute,
312+
("c-x", "e"): nc.edit_and_execute,
313+
# Alt
314+
("escape", "b"): nc.backward_word,
315+
("escape", "c"): nc.capitalize_word,
316+
("escape", "d"): nc.kill_word,
317+
("escape", "h"): nc.backward_kill_word,
318+
("escape", "l"): nc.downcase_word,
319+
("escape", "u"): nc.uppercase_word,
320+
("escape", "y"): nc.yank_pop,
321+
("escape", "."): nc.yank_last_arg,
322+
}
323+
324+
for keys, cmd in keys_cmd_dict.items():
325+
handle(*keys, filter=focused_insert & ebivim)(cmd)
326+
259327
return bindings
260328

261329

0 commit comments

Comments
 (0)