I have a command, that can connect to remote machines and so the completion should be based on which machine the user provided. That means that if the user pass -D DEVICE_ID I want the completion to provide items for that specific machine.

I couldn't find a way to access the existing arguments in the argument function. For example I have a completion like so:

 complete --command xxx --force-files --arguments "(__fish_complete_pids) (get_process_names)"

And I would like the get_process_names to get the processes from the correct machine using the provided flags.

1 Answer 1


You can achieve this by parsing the command line text to find tokens of interest. The commandline builtin has a convenience to walk the tokens of the current process (so we won't pick up -D tokens in other parts of the pipeline).

Here's an example which offers completions for previous tokens. If you complete example -D alpha -D beta it will offer "ALPHA" and "BETA" as completions:

function example_completer
    # Tokenize the current process, up to the cursor.
    # Find the indices of the "-D" tokens.
    # Offer the next tokens as completions (but uppercase).
    # -o means tokenize, -p means current process only, -c means stop at cursor.
    set tokens (commandline -opc)
    for idx in (seq (count $tokens))
        if test $tokens[$idx] = '-D'
            set next_idx (math $idx + 1)
            string upper -- $tokens[$next_idx]          

complete -c example --no-files --arguments '(example_completer)'

From this you should be able to find the DEVICE_ID and query from the corresponding machine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .