Home > OS >  How to bind function fish shell plugin
How to bind function fish shell plugin

Time:03-29

I'm trying to write a plugin for the fish shell but can't get it to work. I have the following in a file called functions/codex.fish:

function create_completion
    commandline -a test
end

bind \cx create_completion

I installed the plugin using fisher:

tom@desktop-20-3 ~/g/b/z/update_insert (main)> fisher install ~/git/codex.fish/
fisher install version 4.3.1
Installing /home/tom/git/codex.fish
           /home/tom/.config/fish/functions/codex.fish
Updated 1 plugin/s

However when I try to run the function using Ctrl x nothing happens.

What am I doing wrong?

CodePudding user response:

I have the following in a file called functions/codex.fish:

There's your problem. Fish functions are lazily loaded. A file called "codex.fish" will be loaded once a function called "codex" is executed for the first time.

So those bindings will only be defined after you've run "codex" once in that session (unless there's another codex.fish that has precedence, in which case they won't be defined at all).

Simply add the binding to config.fish, or one of the eagerly loaded files in ~/.config/fish/conf.d.

CodePudding user response:

You don't need plugins to set up key bindings. Here is one way to do it:

  1. Create your function:
function create_completion
     commandline -a test
end
  1. Save it to disk:
funcsave create_completion

(This creates the file ~/.config/fish/functions/create_completion.fish; you can also do that manually.)

  1. Run funced fish_user_key_bindings and add your binding:
function fish_user_key_bindings
    bind \cx create_completion
end

Now control-X should execute the function.

  • Related