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

Re: Alter completion for mv



On Oct 2,  1:27pm, Daniel wrote:
}
} I would like completion of the destination argument for mv(1) to first
} complete directories. Is this possible by toggling an option, or could
} somebody help me out?

Start by telling completion that it should default to grouping matches
by tag:

zstyle ':completion:*' group-name ''

Check that you don't already have a conflicting zstyle for group-name.

Then, if mv is GNU mv (e.g. on linux), the best way is probably this:

compdef _gnu_generic mv
zstyle -e :completion::complete:mv:argument-rest: list-dirs-first \
	'(( ! ${words[(I)--target-directory=*]} )) &&
	 compset -N "-*"; reply=( $((CURRENT > 1)) )'

Otherwise this will be mostly correct:

zstyle ':completion:*' group-name ''
zstyle -e :completion::complete:mv:: list-dirs-first \
	'compset -N "-*"; reply=( $((CURRENT > 1)) )'

The "compset" in both examples discards options (-i, -v, etc.) that
might appear, so that you can tell which non-option word position you
are in.  That also discards the command name itself, so you are left
with nothing but the non-option arguments, of which any after the
first one might be the destination argument.

There isn't a "list-files-first" style, but list-dirs-first splits
the completions into directories and other-files, so to get "prefer
files when -f is given" you can add

zstyle -e ':completion::complete:mv::' group-order \
	'(( ${words[(I)(-f|--force)]} )) && \
	 reply=( other-files directories )'

Note you use ':completion::complete:mv::' for both the GNU and other
examples, otherwise this style is checked too late and compset will
have already removed the -f option from $words.

This overrides the default order (directories other-files) that the
list-dirs-first style would use.  Improve the pattern in the $words
subscript if you want to also match -f when it is combined with some
other flag in the same word (-uf, maybe).



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