-
Notifications
You must be signed in to change notification settings - Fork 557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Allow to configure additional vi mode Escape mapping #760
Comments
Can we do a more general solution to that -- to enable any arbitrary key bindings (in vi mode or not) from a config file? PtPython allows it but its config file is a python file, so you can simply put your mappings as they are like in the proposed solution. In pgcli's case I am not sure where would be the place of these mappings though. I'll be happy to implement it when I have time as soon as we agree on how would the user specify their key bindings. |
The new version uses |
Answering my own question, here's a patch that adds a diff --git a/pgcli/key_bindings.py b/pgcli/key_bindings.py
index f1eaaa39..0e21904c 100644
--- a/pgcli/key_bindings.py
+++ b/pgcli/key_bindings.py
@@ -4,6 +4,7 @@ import logging
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.filters import completion_is_selected
+from prompt_toolkit.key_binding.vi_state import InputMode
_logger = logging.getLogger(__name__)
@@ -12,6 +13,12 @@ def pgcli_bindings(pgcli):
"""Custom key bindings for pgcli."""
kb = KeyBindings()
+ @kb.add('j', 'k')
+ def _(event):
+ """vi Normal mode."""
+ _logger.debug('Detected jk keystroke.')
+ event.cli.vi_state.input_mode = InputMode.NAVIGATION
+
@kb.add('f2')
def _(event):
"""Enable/Disable SmartCompletion Mode.""" |
@jonathanslenders , do you have any advice here? |
Any updates on the progress of this feature? The above patch seems to work as a temporary fix. I found this project a few hours ago and I am loving it! |
Would maintainers be interested in merging this patch? |
Providing a way to override specific features in pgcli is one thing, but overriding how vi keybindings are handled in pgcli seems sufficiently obscure that I'm not sure it is worth adding complexity to the code base. I wonder if we can have a prompt-toolkit level configuration that will apply to all apps that use prompt-toolkit as readline replacement. @jonathanslenders Thoughts? |
This is a feature that can be configured in ptpython. Ideally if you have a Python configuration file, it should be possible to define a function in that file (lets call it These key bindings can then be merged into the main pgcli key bindings like this: The configuration function should then look something like this: from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.key_binding.input_processor import KeyPress
from prompt_toolkit.filters import vi_insert_mode
def create_key_bindings():
bindings = KeyBindings()
@bindings.add('j', 'j', filter=vi_insert_mode)
def esc(event):
event.cli.input_processor.feed(KeyPress(Keys.Escape))
return bindings In the ptpython config, you can see that the key bindings are registered in a One question maybe for everyone: would it make sense to have a global |
For my part that would be optimal, and I think it's pretty likely that anyone going to the trouble to configure a |
Yes, global configuration would be great as I patch multiple projects that use prompt_toolkit, eg. |
@jonathanslenders This function would be in its own standalone file? And in which file do I put the merging function from the link? |
Could anyone please explain how to get this to work? |
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: 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 |
Description
GNU read line allows to globally map
jj
key sequence toESC
for quick insert mode -> command mode transition.I'm not aware of any global configuration for
prompt_toolkit
.Your environment
Currently I've patched the
key_bindings.py
like so:The text was updated successfully, but these errors were encountered: