2

I have programs which take filename arguments as

scp:path/to/file.ext ark:/abs/path/to/file.ext 

Is it possible make zsh complete filenames after some keywords followed by colon?

So far, I found how to do it in bash: add : to the COMP_WORDBREAKS variable.


Thanks to Gilles, I manage to work like this.

$ cat ~/.zshrc ... function aftercolon() { if compset -P 1 '*:'; then _files "$expl[@]" else _files "$expl[@]" fi } autoload -Uz compinit compinit compdef aftercolon hello olleh 

Now in commands hello and olleh, completion after : works as expected.

I think there might be better way since:

  1. I had to odd if/else since the commands also take filename without prefix.

  2. And since I have many commands take this kind of argument, I need to add names of every commands

1 Answer 1

1

Call compset -P 1 '*:' to remove everything up to the first colon, then _files to complete what comes after the colon. For example, if the file name completions don't depend on the part before the colon:

if compset -P 1 '*:'; then _files "$expl[@]" else compadd "$expl[@]" -S : ark scp fi 

If the file name completions depend on the part before the colon, save that from $PREFIX first.

if [[ $PREFIX = *:* ]]; then local domain=${PREFIX%%:*} compset -P 1 '*:' case $domain in ark) _files "$expl[@]" -g "*.((tar|cpio)(|.gz|.xz|.bz2)|tgz|zip|rar|7z)";; *) _files "$expl[@]";; esac else compadd "$expl[@]" -S : ark scp fi 
Sign up to request clarification or add additional context in comments.

2 Comments

thank you. but I am quiet newbie to zsh completion system. How do you apply this to shell? I added the snippet to .zshrc but failed. I want this completion works for all commands and no need to depend on prefix part.
@auditory This is very different from your original question. Please ask a new question. Ask on Unix & Linux, because your new question is off-topic here: it's about user configuration, not about programming.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.