helix-editor / helix

A post-modern modal text editor.
https://helix-editor.com
Mozilla Public License 2.0
34.07k stars 2.52k forks source link

Issue with `[keys.insert]` mappings such as `j = { f = "normal_mode" }` #2612

Open nitish-17 opened 2 years ago

nitish-17 commented 2 years ago

Summary

I'm using jf to exit insert mode. When I insert anything that ends with j (not jj) and try to exit insert mode with jf I end up inserting jf and remain in insert mode. To overcome this I usually end up pressing escape key twice to exit insert mode which is not ideal.

Reproduction Steps

context: I'm using jf to exit insert mode.

I tried this:

I expected this to happen:

Instead, this happened:

Helix log

No response

Platform

Linux

Terminal Emulator

gnome terminal

Helix Version

helix 22.03 (d4e45fd4)

n0s4 commented 2 years ago

I guess the solution would be when the next key doesn't match the keybind, instead of inserting it you check again to see if it's the start of another key combination?

the-mikedavis commented 2 years ago

That might work but I like the current system where a key not found in a pending key sequence cancels the sequence (see here).

Kakoune has an interesting way of approaching this: https://github.com/mawww/kakoune/wiki/Avoid-the-escape-key#jk-to-escape. It maps k to a hook that deletes the preceeding jk sequence if it exists, and if it existed it goes to normal mode. I don't think we can create a similar keybinding without scripting support for config though.

kverb commented 2 years ago

In vim, this type of exiting insert mode has a timeout. So, while there's a short (but configurable) inconvenience if you want to end your insertion on 'j', it's at least possible. I was a bit surprised to find that helix's helpful little keymap sequence hint window just persists indefinitely, waiting for the next key.

fwiw, this is my first day with helix and I am truly delighted so far.

aldanor commented 2 years ago

This was the first (and only major) problem I've hit with Helix so far.

Wonder if anyone has viable solutions for it? If the text your editing in insert mode ends with the first letter of your jj/jk/fd/kj/whatever, what do you do?

E.g., I have f-d as exit-to-normal-mode sequence.

zeeklop commented 2 years ago

Glad to not be the only one with this issue. I use jj to escape insert mode. On VIM there is a timeout implemented for these kind of commands. If I press j once, it waits for a couple of seconds. After the timeout is done, j is inserted in the document. If I press another key (not bound to j) then j and the new key are inserted.

The key bound to the command get inserted in the document but if the next key executes a command then that key is removed and the command executed

vim_escape_insert

geometryolife commented 2 years ago

Previously, I used Neovim to use kj to exit insert mode, but in the past few days with Helix, I have become very accustomed to using C-[ to exit insert mode.

eugenesvk commented 1 year ago

Have you tried nested binding like this?

[keys.insert] #ā“˜
  'j'   = {f='normal_mode','j'={'f'=[':insert-output /bin/echo -n j','move_char_right','normal_mode']}}
  # 'hij' + 'jf' cleanly exits after typing 'hij' instead of typing an extra jf
  # the char movement was needed because otherwise you end up 1 char left with insert and with append-output at the end of the line you get an insert at the wrong line

(though this is missing a proper text insertion API, check https://github.com/helix-editor/helix/issues/2797)

@zeeklop

it waits for a couple of seconds

that's also bad

The proper way to do this type of switching is like this:

(as implemented in https://github.com/NeoVintageous/NeoVintageous)

See this short demo

https://github.com/helix-editor/helix/assets/12956286/d07fc374-9d5f-47ec-ac6d-3f663351f11d

Although an even better solution would be to have an on-hold functionality since you'd not use that in regular typing, so you don't have this conflict in the first place, then you could have a clean

just like you can have the same with other modes

(though this suggestion was unfortunately shot down https://github.com/helix-editor/helix/issues/1494)

eugenesvk commented 1 year ago

Another workaround is to use a good keyboard remapping tool (like Karabiner on a Mac) to implement the workaround I use for the lack of on-hold functionality: basically, make some key like i

However, this is limited by:

(you can also do it for a key chord like jf, but I think it's more bug-prone due to the aforementioned limitations)

Mac example: Helix config

[keys.insert] #ā“˜
  'A-}' = 'normal_mode' # some unused inconvenient shortcut, ideally free in any mode

Goku Karabiner config snippet

:applications {:wezterm ["^com\\.github\\.wez\\.wezterm$"]}
:main [
 {:des "  šŸ æiāƒ£  āŸ¶ Normal Mode ā‡§āŽ‡] (@WezTerm instead of @Helix)" :rules [:wezterm
  [:i         nil nil
   {:alone {:key :i :halt true} :delayed{:canceled[:i]} :held{:key :ā‡§āŽ‡ć€‘ :repeat false}
    :params{:alone 254 :held 255 :delay 255} }]
  ]}
]
kmicklas commented 1 year ago

Is there any opposition from the maintainers to having proper support for this? It's a dealbreaker for me to use Helix, but I'm happy to try to implement it if it's desired.