Search code examples
fish

Trying to write completion, then cd


This seems so basic to me, but I've been unable to figure it out. So I wrote a completion that auto completes my code directories like the following:

complete --command dev --exclusive --arguments '(__fish_complete_directories (~/Code/))'

Now I'm trying to write a function that cd's into the chosen directory from the dev completion above.

This doesn't work but I hope you get what I'm trying to do:

function c
  cd dev
end

So when typing c, I want to get all my directories in the ~/Code/ directory as tab completion options, then when I select one, I want my current path to be taken to the chosen directory.

Btw - fish needs more docs :)


Solution

  • So you're trying to do two things:

    1. Create a function c that cd's to its argument
    2. Make tab-completing c show paths in ~/Code

    This is straightforward:

    function c
        cd $argv
    end
    
    complete --command c --exclusive --arguments '(__fish_complete_directories ~/Code/)'
    

    I'm not sure what dev is meant to be, but to be clear, the --command option to complete adds completions to an existing command. It does not define a new command.

    Regarding documentation, there's lots at http://fishshell.com/docs/current/index.html. If there's areas you think need more coverage please open an issue at https://github.com/fish-shell/fish-shell/issues.