Use colon as filename separator in zsh tab completion












3















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 '-nt /:' "$@" # 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











share|improve this question




















  • 1





    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
    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
    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
















3















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 '-nt /:' "$@" # 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











share|improve this question




















  • 1





    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
    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
    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














3












3








3


1






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 '-nt /:' "$@" # 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











share|improve this question
















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 '-nt /:' "$@" # 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








zsh autocomplete






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago







auditory

















asked May 25 '18 at 0:34









auditoryauditory

162




162








  • 1





    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
    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
    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





    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
    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
    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




1





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
May 25 '18 at 8:41





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
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





@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





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
May 25 '18 at 21:30





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
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





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










0






active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f445889%2fuse-colon-as-filename-separator-in-zsh-tab-completion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f445889%2fuse-colon-as-filename-separator-in-zsh-tab-completion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

サソリ

広島県道265号伴広島線

Setup Asymptote in Texstudio