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!