Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Completion for command-not-found "commands"



The very-short version of my question is: How can I dynamically get completion for `git rm` when what I've typed on the commandline so far is `grm`? ("dynamically" meaning without defining a bunch of compdef's up front.)

Longer version:

A colleague of mine finally convinced me that using his 'g___' aliases for git commands was superior to using my 'g ___' versions. E.g.:

==> in his ~/.bashrc <==
alias gst='git status'
alias gco='git checkout'
========================

==> vs. my ~/.gitconfig <==
[alias]
  st = status
  co = checkout
==> and my ~/.zshrc <==
alias g=git
=======================

However, I'm still convinced that I'd rather keep mine as git [alias]'es, rather than shell aliases (When getting slightly complex, git [alias]es can be shell functions). So, I wrote the following:

valid_git_alias () {
  git config -l | grep -qF "alias.$1="
}

valid_git_command () {
  # my `awk` is better than my `zsh` for this kind of string manipulation:
  git help --all | awk '/---/ { ok=1 ; OFS="\n" ; ORS="" } /^ / { NF=NF+1 ; if (ok) print $0 }' | grep -qF $1
}

command_not_found_handler () {
 [[ $1 = g* ]] || return 1
 local al=${1#g}
 shift
 valid_git_alias $al || valid_git_command $al || return 1
 git $al "$@"
 return 0
}

This lets me do:

`gst` -- gets converted to `git st` (which is one of my git [alias]es)
`grm` -- gets converted to `git rm` (which is a built-in git command)

The problem is that I've lost shell-completion (which I had with the `g ___` version). E.g. `g checkout <Tab>` would complete branches and tags. And, with:

_git-co () { _git-checkout "$@" }

`g co <Tab>` would behave the same.

--
Best,
Ben



Messages sorted by: Reverse Date, Date, Thread, Author