question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Allow to configure additional vi mode Escape mapping

See original GitHub issue

Description

GNU read line allows to globally map jj key sequence to ESC for quick insert mode -> command mode transition.
I’m not aware of any global configuration for prompt_toolkit.

Your environment

> pgcli --version  

Version: 1.7.0

> uname -a  

Linux myuser 4.11.9-1-ARCH #1 SMP PREEMPT Wed Jul 5 18:23:08 CEST 2017 x86_64 GNU/Linux

Currently I’ve patched the key_bindings.py like so:

diff --git a/pgcli/key_bindings.py b/pgcli/key_bindings.py                                                                                                                                                                                    
index 646e6f7..b623c62 100644
--- a/pgcli/key_bindings.py
+++ b/pgcli/key_bindings.py
@@ -2,7 +2,8 @@ import logging
 from prompt_toolkit.enums import EditingMode
 from prompt_toolkit.keys import Keys
 from prompt_toolkit.key_binding.manager import KeyBindingManager
-from prompt_toolkit.filters import Condition
+from prompt_toolkit.key_binding.input_processor import KeyPress
+from prompt_toolkit.filters import Condition, ViInsertMode
 from .filters import HasSelectedCompletion
 
 _logger = logging.getLogger(__name__)
@@ -22,6 +23,14 @@ def pgcli_bindings(get_vi_mode_enabled, set_vi_mode_enabled):
         enable_search=True,
         enable_abort_and_exit_bindings=True)
 
+    @key_binding_manager.registry.add_binding('j', 'j', filter=ViInsertMode())
+    def _(event):
+        """
+        Typing 'jj' in Insert mode, should go back to navigation mode.
+        """
+        _logger.debug('Detected jj keys.')
+        event.cli.input_processor.feed(KeyPress(Keys.Escape))
+
     @key_binding_manager.registry.add_binding(Keys.F2)
     def _(event):
         """

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
cswinglecommented, Apr 22, 2020

@jonathanslenders

One question maybe for everyone: would it make sense to have a global .prompt_toolkit.config.py file or something like this, were these custom bindings could be configured for every prompt_toolkit application. Similar to .inputrc for all GNU readline applications?

For my part that would be optimal, and I think it’s pretty likely that anyone going to the trouble to configure a vi_insert_mode key combination for one application is going to want it for every application using prompt_toolkit, just like the way it works with readline.

3reactions
caticoa3commented, Jul 9, 2020

You’ll need to edit key_binding.py

After modifying the above examples (btw thanks everyone for sharing those snippets), the following worked for me: The changes appear to be necessary due to updates to the prompt-toolkit library.

diff --git a/pgcli/key_bindings.py b/pgcli/key_bindings.py
index 23174b6..9437a83 100644
--- a/pgcli/key_bindings.py
+++ b/pgcli/key_bindings.py
@@ -1,7 +1,10 @@
 import logging
 from prompt_toolkit.enums import EditingMode
+from prompt_toolkit.keys import Keys
 from prompt_toolkit.key_binding import KeyBindings
+from prompt_toolkit.key_binding.key_processor import KeyPress
 from prompt_toolkit.filters import (
+    ViInsertMode,
     completion_is_selected,
     is_searching,
     has_completions,
@@ -124,4 +127,12 @@ def pgcli_bindings(pgcli):
         """Move down in history."""
         event.current_buffer.history_forward(count=event.arg)

+    @kb.add("k", "j", filter=ViInsertMode())
+    def _(event):
+        """
+        Typing 'kj' in Insert mode, should go back to navigation mode.
+        """
+        _logger.debug('Detected kj keys.')
+        event.cli.key_processor.feed(KeyPress(Keys.Escape))
+
     return kb
Read more comments on GitHub >

github_iconTop Results From Across the Web

Add builtin settings to enable jj and jk map to escape #718
Some developers like to map jj , jk , or some other two key combination, to <Esc> in Vim. This is not so...
Read more >
In bash vi mode, map jk to exit insert mode
As an addendum this is controlled by :set timeoutlen=<miliseconds> in Vim (see :h timeoutlen ). Several shell's or X11 has no such timeout ......
Read more >
Avoid the escape key | Vim Tips Wiki - Fandom
Mappings. It can be convenient to use a mapping so that pressing a key, or sequence of keys, generates Escape. The :imap command...
Read more >
VI Mode - Kate
Kate's VI mode is a project to bring Vim-like, modal editing to the Kate text editor and by extension to other KDE programs...
Read more >
Basic VI Editor Commands - Marquette University
When entering a file, vi is in command mode. To enter text, you must enter insert mode. If in insert mode, enter command...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found