To give an example everyone knows:
Can you provide a csh complete command which allows to autocomplete any argument to ls to be either an option or a filename?
Useful documentation here.
The typical solution to this is to provide two completions: One for all -arguments and a separate one for the pathes. Here is an example I just contrived to demo how to tab complete empty args to files & dirs, arguments beginning with - to short version arguments, and arguments beginning with -- to long version arguments:
complete ls \
'c/--/(all almost-all author escape block-size= ignore-backup color color= directory dired classify file-type format= full-time no-group h
uman-readable si dereference-command-line dereference-command-line-symlink-to-dir hide= indicator-style= inode ignore= dereference numeric-uid-gid lit
eral indicator-style=slash hide-control-chars show-control-chars quote-name quoting-style= reverse recursive size sort= time= time-style tabsize width
help version)/' \
'c/-/(a A b B c C d D f F g G H i I k l L m n N o p q Q r R s S t T u U v w x X 1)/' \
'p/*/f/'
Note that the order of the specifications for -- and - is important.
This can be used like this:
> ls --[TAB]
all format= quote-name
almost-all full-time quoting-style=
author help recursive
block-size= hide-control-chars reverse
classify hide= show-control-chars
color human-readable si
color= ignore-backup size
dereference ignore= sort=
dereference-command-line indicator-style= tabsize
dereference-command-line-symlink-to-dir indicator-style=slash time-style
directory inode time=
dired literal version
escape no-group width
file-type numeric-uid-gid
> ls -[TAB]
1 A B C D F G H I L N Q R S T U X a b c d f g i k l m n o p q r s t u v w x
> ls [TAB]dir/
..where [TAB] denotes pressing the TAB key.
How can this be rewritten to get all three completions listed in one step by just doing:
> ls[TAB]
It should list all -opt, --opt, and dir/
The problem seems to be combining the p/*/f/ into any fixed word list.
One trial and actually nice trick but not workable is something like this:
complete ls 'p/*/`ls -1F | xargs echo --all --almost-all --author --escape --block-size= --ignore-backup --color --color= --directory --dired --classify --file-type --format= --full-time --no-group --human-readable --si --dereference-command-line --dereference-command-line-symlink-to-dir --hide= --indicator-style= --inode --ignore= --dereference --numeric-uid-gid --literal --indicator-style=slash --hide-control-chars --show-control-chars --quote-name --quoting-style= --reverse --recursive --size --sort= --time= --time-style --tabsize --width --help --version -a -A -b -B -c -C -d -D -f -F -g -G -H -i -I -k -l -L -m -n -N -o -p -q -Q -r -R -s -S -t -T -u -U -v -w -x -X -1`/'
It does offer the first tab completion correctly, but then you can no longer tab complete going down the directory hierarchies.
bashvscsh. – cfi Apr 4 '12 at 10:47