On Sun, 14 May 2017, Joel Maxuel wrote:
> I am writing a function to change to a directory below the current working
> directory, and I am hitting a brick wall with fish from Jessie backports
> (2.2.0-3~bpo8+1) and Stretch (2.4.0-1).  The function is as follows (saved
> as ~/.config/fish/functions/cdb.fish):
> 
> function cdb
>         complete -c cd -r -a "(find -type d -name "$argv" -not -path
> '*/\.*' -prune)";
> end
> 
> So after "funcsave cdb", I try it with a same directory name and it
> silently exits with no action taken.

The complete builtin is for describing how a command should be completed - 
that is, what should happen when you type the command and press Tab. If 
you are trying to write a function, the function should perform the action 
directly. Instead, try:

function cdb
   cd (find -type d -name "$argv" -not -path '*/\.*' -prune)[1]
end

The array subscript (`[1]`) ensures that only the first result is used.

> I debugged to the point that the find command by itself works (returns the
> directory).  After consulting the fish documentation[1], I found out you
> can test complete commands at the terminal.  So I tried again with:
> 
> complete -c cd -r -a "(find -type d -name "github" -not -path '*/\.*'
> -prune)"
> 
> ...still, no action taken.

This example defines a completion for the cd builtin, rather than 
defining a function. It also has incorrect quoting - in general, the 
argument to the `-a` option should be surrounded by single quotes.
 
> While I was in the documentation, I tried a stock example:
> 
> complete -x -c su -d "Username" -a "(cat /etc/passwd | cut -d : -f 1)"
> 
> Even with that, no choices, nothing executed.  Running complete without
> parameters provides a list of saved examples.

Likewise, this example defines a completion for the su builtin.

David Adam
fish committer
zanc...@ucc.gu.uwa.edu.au

Reply via email to