2
I'm trying to set up a zsh function that will take me back to the top level git directory with an optional argument to move relative to that directory. I've currently got this which works:
alias gitdir='git rev-parse --show-toplevel'
cdgit() { cd $(gitdir)/$1 }
The issue is, tab completion doesn't work properly, it will autocomplete from whatever directory I'm in when I run cdgit
, but I want it to complete from $(gitdir)
. If I type the following line before running cdgit
, the completion will work correctly (from $(gitdir)
):
compctl -W $(gitdir) -/ cdgit
However, I don't want to type that command every time before I type cdgit
just to get tab completion. Is there any way that I can make a completion function for cdgit
that will somehow run that command so my completion is correct?
Why the downvote? – Josh Sherick – 2016-06-13T14:42:29.807
Why don't you want to stick with the
compctl
solution? This can go to the rc file where you define the cdgit function, can't it?! – mpy – 2016-06-14T18:58:16.8831No, because I don't want it to run when my shell starts. That would change the directory that
cdgit <path>
completes to to whatever$(gitdir)
evaluated to at the time that my .zshrc was run. However, I want it to autocomplete to whatever$(gitdir)
evaluates to at the time that I typecdgit <path>
. So I want to typecdgit
and press tab and get completion from where it wouldcd
me to, which might not be the same place as when the shell was started. – Josh Sherick – 2016-06-14T20:00:27.060It could potentially run on every directory change, I guess. Not the cleanest solution but it's a quick command. – Josh Sherick – 2016-06-14T20:01:26.800
Of course, my bad not realizing that
$(gitdir)
will be static then. See my answer for a suggestion. – mpy – 2016-06-15T16:46:55.420