How can I provide tab completions to fish shell from my own script?

后端 未结 2 857
[愿得一人]
[愿得一人] 2021-02-05 07:51

I am running Ubuntu 13.10 and fish 2.1.0. I want to write myself a Python script to do some tasks from the command line. The script will require command line arguments.

相关标签:
2条回答
  • 2021-02-05 08:14

    Adapted from zanchey's comment on GitHub:

    If you have a program myprog which takes the --_completion option, you can write a single completion stub for myprog that looks like this:

    complete --command myprog --arguments '(myprog --_completion (commandline -cp)'
    

    Your program will then get invoked as myprog --_completion myprog some arguments here, and you can respond with the appropriate completions. It should return only the current token that is being completed (you could also pass this to the program with (commandline -ct), or tokenise it yourself), followed optionally by a tab and a short description. Multiple completions are separated with new lines.

    Notes:

    • --_completion is a convention suggested by the python-selfcompletion library, but you can use anything you want, and this answer is not Python-specific
    • There is no way to specify the default completion as described in dbarnett/python-selfcompletion#2 (GitHub comment). You would definitely have to make a short stub for each command.

    For Python scripts specifically, the following libraries may support fish completions at some point in the future (but they don't yet):

    • argcomplete
    • python-selfcompletion
    0 讨论(0)
  • 2021-02-05 08:15

    You should create a fish autocomplete function for your script and source it or put it in ~/.config/fish/completions/myprog.fish folder.

    reference: fish docs

    0 讨论(0)
提交回复
热议问题