`exec A` in hook doesn't seem to work


I have a prepare-commit-msg git hook that automatically sets part of the commit message.

Now, when using Kakoune to edit that commit message, I want the editor to automatically enter insert mode at the end of the first line.

The following doesn’t seem to work (it does nothing):

hook global BufOpenFile '.*/COMMIT_EDITMSG$' %{
  # Automatically enter insert-mode at end of line.
  exec A

I’ve tried adding debug messages, to make sure that the hook is actually invoked, but the exec A is not doing anything…

Could someone help me?

I think your issue comes from the fact that git creates a new temporary COMMIT_EDITMSG file each time, but you are using BufOpenFile, which triggers when the file already exists.
Taken from :doc hooks :

BufOpenFile filename
a buffer for an existing file has been created

Either try using BufNewFile or BufCreate :blush:

I think the Buf* hooks execute in buffer context, while things like “cursors” and “insert mode” only exist in window context. Instead of trying to enter insert mode immediately, you might like to tell Kakoune to try it once everything’s initialised and it’s ready for editing to begin:

hook global BufOpenFile '.*/COMMIT_EDITMSG$' %{
  hook -once buffer NormalIdle .* %{
    # Automatically enter insert-mode at end of line.
    exec A

(also, the pattern for the BufOpenFile hook could just be .*/COMMIT_EDITMSG without the $, since Kakoune implicitly wraps every hook pattern with ^ and $)

1 Like

This works well!

Only problem I could detect was that it broke my keychord (lh) for exiting insert mode:

# Make it possible to exit insert mode using "lh".
hook global InsertChar l %{
  hook -group lh global InsertChar h %{
    try %{
       exec -draft hH <a-k>lh<ret> d
       exec <esc>
  $ sh -c %{
    sleep 0.2
    :send rmhooks global lh

Any idea how I can fix that as well?

The keychord works whenever I run Kakoune normally, it just doesn’t work when the hook you provided has been run…

I’m afraid I have no idea why those hooks would be interfering with one another. Anything in the *debug* buffer?

Unfortunately no.

Well, I thought the BufCreate trick worked because it changed my cursor color (I switch to purple when in insert) and did not bother checking more.
This means that exec A does execute, and I’m in insert mode while not being in insert mode ?
Schrodinger insert
I can freely move around while my cursor is clearly indicating insert mode :thinking: