How do I change the escape key?

I’d like to use fd to escape. I have read https://github.com/mawww/kakoune/wiki/Avoid-the-escape-key, but when I stick this modified snippet in my .kakrc it doesn’t work—fd inserts those characters as normal. Any suggestions?

# escape with fd
hook global InsertChar f %{ try %{
  exec -draft hH <a-k>fd<ret> d
  exec <esc>
}}

The snippet you posted works like this:

hook global InsertChar f %{ try %{

After the user presses f in insert mode…

exec -draft hH <a-k>fd<ret> d

…select the previous two characters, and try to match them with the regex fd. If there’s no match (i.e. the previous two characters were something other than fd) the <a-k> command raises an error, which gets eaten by the try %{} block, and nothing else happens.

If the previous two characters do match fd, the selected characters are deleted…

exec <esc>
}}

…and we use <esc> to exit insert mode.

Unfortunately, this will never work, because after the user types f, the last character typed will always be f, not d. If you change the code to check for something that does end in f, like df, it should work, or if you change the hook to only trigger after the user types d, that should work too.

You can try this script to acheive what you want:

map-sequence global fd %{exec <esc>}

I’ve got it working now (using @Screwtapello’s explanation.) Thank you both!

This removes the hook after some time. So jk is only evaluated as <esc> if it is typed within 0.05 seconds. It uses connect.kak, but there is probably a simple way, to do it without it.

hook global InsertChar j %{
  hook -group jk global InsertChar k %{
    try %{
      exec -draft hH <a-k>jk<ret> d
      exec <esc>
    }
  }
  $ sh -c %{
    sleep 0.05
    :send rmhooks global jk
  }
}
1 Like
# Type <character><character> to leave insert mode.
# ["jj", "kk"]
hook global InsertChar '[jk]' %{
  try %{
    execute-keys -draft "hH<a-k>%val{hook_param}%val{hook_param}<ret>d"
    execute-keys <esc>
  }
}