Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I get autocomplete of hosts to work for an alias or function? #4101

Closed
devondragon opened this issue Jun 29, 2015 · 5 comments
Closed

Comments

@devondragon
Copy link

I have a custom function which takes in a hostname argument. I would like to be able to tab-complete the hostname just like I can with ssh/rsync/scp/etc... I've tried adding the short alias to my function "wl" to the the line in the common-aliases.plugin.zsh:

zstyle -e ':completion::(ssh|scp|sftp|rsh|rsync|wl):hosts' hosts 'reply=(${=${${(f)"$(cat {/etc/ssh,~/.ssh/known_}hosts(|2)(N) /dev/null)"}%%[# ]_}//,/ })'

And tried doing the same in my .zshrc. But neither seems to work.

How can I apply the hosts completion to an arbitrary function or alias?

@apjanke
Copy link
Contributor

apjanke commented Jun 29, 2015

Could you stick that code sample in triple-backquotes so we can see the exact string and it doesn't get interpreted as Markdown formatting? Some stars in there are turning things in to italics.

Like this.

@devondragon
Copy link
Author

zstyle -e ':completion:*:(ssh|scp|sftp|rsh|rsync|wl):hosts' hosts 'reply=(${=${${(f)"$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) /dev/null)"}%%[# ]*}//,/ })'

@devondragon
Copy link
Author

I just took the line from the common-aliases.plugin.zsh which I believe provides tab completion for ssh, and added my alias "wl" to the list of commands. But that doesn't seem to actually work.

@apjanke
Copy link
Contributor

apjanke commented Jun 29, 2015

Short answer: that zstyle line just customizes the manner in which zsh looks up hosts when it knows it needs them for completion. (Also, it looks like there's a typo in it, so it might not work as intended.)

To tell zsh that your function takes hosts as an argument, do this.

compdef _hosts wl

If only one of your function's arguments takes hosts, then you may want to define your own _wl() completion widget function to go along with the main wl() function that can do more context-sensitive completing.

Long answer: see the Completion System section of the Zsh Reference Manual for all the details of defining completion.

@devondragon
Copy link
Author

Perfect! Thank you very much!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants