Search code examples
fish

How to feed autocomplete data to a fish alias?


I am starting with fish and one of the things I could not find in the extensive documentation was autocomplete feeds.

There is mention of Tab Completions in the tutorial but it addresses existing the existence of the mechanism itself, not its configuration.

I have a bunch of virtual machines I connect to via

machinectl shell <name of machine> /bin/bash

I could make alises for all my machines via

function cm
    machinectl $argv shell /bin/bash; 
end

but this requires to remember and type the machine name.

How could I use the output of machinectl list | tail -n +2 | head -n -2 | cut -f1 -d' ' as a feed/hint to my cm command so that it shows them when using Tab?


EDIT: I somehow missed this right at the top of the documentation: Tab completion (I found it after reviewing the answers)


Solution

  • This should get you off to a good start:

    complete --command cm --no-files \
    --arguments '(machinectl list | tail -n +2 | head -n -2 | cut -f1 -d" ")'
    

    Entering that at the command line will activate it for the current session; to make it permanent add the line to a completions file as Kurtis describes (~/.config/fish/completions/cm.fish).