Using execute-keys in hooks

I’m trying to automatically move the cursor to the bottom of the file when it’s opened, but I’m having trouble getting execute-keys to work in a hook.

This works:

hook global WinDisplay .* %{ execute-keys "gj" }

But I only want to go to the bottom when a file is first opened, not when I’m switching back to it.

This doesn’t work:

hook global BufCreate .* %{ execute-keys "gj" }

Is there something obvious I’m missing?


When a buffer is created, it’s not necessarily displayed anywhere - for example, if you run kak filea.txt fileb.txt then fileb.txt will have a buffer but it won’t be visible.

If a buffer isn’t visible at all, it doesn’t make sense to view the bottom or the top or any other part of it, so trying to use gj in a buffer hook won’t do you much good.

If you only want to trigger the WinDisplay hook once, instead of every time the buffer is displayed in a window, you probably want to use the -once option:

hook -once global WinDisplay .* %{ execute-keys gj }
1 Like

Thanks @Screwtapello!

Just using -once means it doesn’t get executed when editing a new file, but combining the hooks works perfectly:

hook global BufCreate .* %{
    hook -once buffer WinDisplay %val{buffile} %{ execute-keys gj }
1 Like