4

I have many programs which take filename arguments as one(or more) of

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

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

In bash you can do this by adding : 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 have odd if/else clause 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. I want to apply this to all commands if possible. It is also okay to apply this to all commands if it is easier

--

For those who may need, now I found slightly simpler way. Insert following in your .zshrc file

function aftercolon() {
  compset -P '*:'                   # strip stuff up to last :
  compset -S ':*'                   # strip stuff after next colon
  _default -r '\-\n\t /:' "$@"      # do default completion on this
}

compdef aftercolon cmd1 cmd2 cmd3 cmd4 
# to apply to all commands
compdef aftercolon -first-  # https://superuser.com/questions/1080452/add-strings-to-zsh-tab-completion-for-all-commands-and-arguments


10
  • 2
    That's a repeat of your SO question which I've already answered, there's no point in repeating that question here. The follow-up questions you asked in comments are a completely different question. If you want to get answers to those question, please edit this question to formulate what you want clearly. – Gilles 'SO- stop being evil' May 25 '18 at 8:41
  • @Gilles The question here clearly formulates all I want. I just want to know the zsh equivalent of COMP_WORDBREAKS which works well with 'MANY programs'. – auditory May 25 '18 at 11:57
  • anyway I copied follow-up question from SO. – auditory May 25 '18 at 12:07
  • Many commands is completely different from all commands. Ignoring the prefix is also different from completing the prefix and the suffix separately. And having an optional prefix is also different from having a mandatory prefix. The bash feature you mention doesn't even do the same thing, although it occasionally can have the same effect (it it works for you, you're really not picky about getting good completions), so if you were asking the zsh equivalent of bash's COMP_WORDBREAKS, that would be again a completely different question. – Gilles 'SO- stop being evil' May 25 '18 at 21:30
  • Indeed I am not really picky about getting good completion, but just wanted to accomplish what I want in the simplest way. It's always hard to ask good question. Thanks anyway. – auditory May 26 '18 at 5:03
1

This is old posting but as a summary, in .zshrc

function aftercolon() {
  compset -P '*:'                   # strip stuff up to last :
  compset -S ':*'                   # strip stuff after next colon
  _default -r '\-\n\t /:' "$@"      # do default completion on this
}
compdef aftercolon -first-

Now I could complete filename after colon as well in zsh,

$ scp:/path/to/somef<TAB>  # now filename completion works after colon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.