Skip to content

Instantly share code, notes, and snippets.

@dblevins
Created March 2, 2010 23:14
Show Gist options
  • Save dblevins/320089 to your computer and use it in GitHub Desktop.
Save dblevins/320089 to your computer and use it in GitHub Desktop.
#!/bin/bash
_opts () {
perl -ne '
print if s/^.IP .\\fB\\-/-/ and s/\\(f(P|I|B|R)|)|".*//g;
print if s/^.IX Item .\-/-/ and s/"//g;
print if s/^.It Fl /-/;'
}
_man_generic() {
local IFS=$'\t\n' cmd cur opts
cur="${COMP_WORDS[COMP_CWORD]}"
cmd=$(basename "$1")
opts=$( for n in $(manpath | tr ':' '\n'); do \
n="$n/man1/${cmd}.1"; \
[ -f "$n" ] && cat "$n" | _opts; \
[ -f "$n".gz ] && gunzip -c "$n".gz | _opts; \
done | perl -pe 's/, | (--)/\n$1/' | sort | uniq )
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
}
# just add a line like the following
# for any command you wish completed
complete -o default -F _man_generic wget
complete -o default -F _man_generic curl
complete -o default -F _man_generic rsync
# or alternatively, use a slicker syntax
for cmd in ls wget curl rsync; do
complete -o default -F _man_generic $cmd
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment