Search code examples
fish

`bind` does not run user-defined function


The fish docs say you can run a list of commands using the syntax

bind <char> cmd1 cmd2 ...

including some speciall commands listed at https://fishshell.com/docs/current/commands.html#bind.

Writing a plugin that has key bindings,

I added

bind '&' `backward-delete-char` on_ampersand

to fish_user_key_bindings.fish, but it resulted in no behavior - the on_ampersand function was not called, and neither was backward_delete_char. Without on_ampersand, it works.

The docs don't suggest anything about why this behavior could occur.


Solution

  • This is a bug in fish - you can't combine the input-buffer editing commands with your own commands. See https://github.com/fish-shell/fish-shell/issues/3683.

    The workaround is to use the commandline -f [function] syntax in a user defined function to access those functions specially available to fish_user_key_bindings :

    function on_ampersand
        commandline -f backward-delete-char # or whatever
        [your code]
    end