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

Auto-complete xxh host name from ssh config #82

Open
athityakumar opened this issue May 16, 2020 · 12 comments
Open

Auto-complete xxh host name from ssh config #82

athityakumar opened this issue May 16, 2020 · 12 comments

Comments

@athityakumar
Copy link

athityakumar commented May 16, 2020

xxh has been awesome so far with it's core functionality - but I need to type in the exact host names from ssh config as of now. Is it possible to auto-complete this with TAB-key support?

xxh host-prefix[TAB]

host-prefix-1
host-prefix-2
host-prefix-3

🏅 Solutions:

➡️ Bash and Xonsh on Ubuntu - #82 (comment)

➡️ Zsh - #82 (comment)

➡️ Fish - #82 (comment)


For community:
⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

@anki-code
Copy link
Member

anki-code commented May 16, 2020

The auto-complete is depend on the shell and OS you use. But some shells like xonsh can get the completions from bash as well.

In Ubuntu 20.04 I just added the same completion as ssh to bash and it works in xonsh the same way:

$ sudo bash
# cat > /etc/bash_completion.d/xxh_complete <<'COMPLETION'
_xxh() {
    local cur prev words cword
    _init_completion || return
   _known_hosts_real -a "$cur"
} && complete -F _xxh xxh
COMPLETION

After relogin:

bash$ complete | grep xxh
complete -F _xxh xxh

bash$ xxh host-prefix[TAB]
host-prefix-1
host-prefix-2
host-prefix-3

bash$ xonsh
xonsh$ xxh host-prefix[TAB]
host-prefix-1
host-prefix-2
host-prefix-3

The best way to solve it is to understand how your shell and OS works with auto-complete, get the ssh default auto-complete and post here the solution for xxh. As I did for Ubuntu.

@athityakumar
Copy link
Author

My current setup is Mac OS & zsh shell - any idea how I can add custom completion of hosts for xxh locally?

@athityakumar
Copy link
Author

Also, can you guide me on how I can contribute this auto-completion as something that works out-of-the-box automatically upon installing xxh?

@anki-code
Copy link
Member

anki-code commented May 16, 2020

My current setup is Mac OS & zsh shell - any idea how I can add custom completion of hosts for xxh locally?

Sorry I'm not the user of zsh and MacOS.
This could help:

Also, can you guide me on how I can contribute this auto-completion as something that works out-of-the-box automatically upon installing xxh?

Good question. I have no answer now.

@chitoku-k
Copy link
Contributor

This could be easily achieved by adding the following config:

#compdef xxh

_xxh() {
    service=ssh _ssh $@
}

_xxh

Put this file to somewhere your environment autoloads, which you can check $fpath for the exact location. Probably I can add one to this repo?

@anki-code
Copy link
Member

@chitoku-k for what OS and shell type your solution?

@chitoku-k
Copy link
Contributor

@anki-code This solution only works for Zsh as it has built-in support for SSH completion (probably for any OS, at least it works in macOS and Linux).
https://github.com/zsh-users/zsh/blob/zsh-5.8/Completion/Unix/Command/_ssh

@anki-code anki-code changed the title Auto-complete host name from ssh config Auto-complete xxh host name from ssh config Jun 30, 2020
@anki-code
Copy link
Member

anki-code commented Jun 30, 2020

@chitoku-k If you can write the complete script to install xxh autocomplete for zsh it will be great! Something like this on bash or zsh:

#!/bin/bash
mkdir -p ~/.config/xxh/
echo ' ... compef xxh ... ' > ~/.config/xxh/autocomplete-zsh
echo 'source ~/.config/xxh/autocomplete-zsh' >> ~/.zshrc
echo 'Please restart zsh'

And place it to the repo to autocomplete/install-xxh-autocomplete-zsh.sh.

@chitoku-k
Copy link
Contributor

@anki-code For homebrew, it has built-in support for installing package-specific completion script described in Homebrew Shell Completion, which is quite handy if we can enable it in installation steps. I can somehow contribute to this.

@anki-code
Copy link
Member

@athityakumar @chitoku-k Are you using argcomplete or alternatives? One of the way is to add it to xxh.

@chitoku-k
Copy link
Contributor

@anki-code I am not quite familiar with that. Presumably we got to integrate with the existing completion for ssh command at least.

@Gorthog
Copy link

Gorthog commented Nov 17, 2021

If you want hosts completion from ssh config file when using fish add this to your ~/.config/fish/config.fish file:

read -alz -d \n contents <~/.ssh/config
set hosts (string replace -rfi '^\s*Host\s+(\S.*?)\s*$' '$1' -- $contents | string split " " | string match -rv '[\*\?]' | string join ' ')
complete -c xxh -f -a $hosts

this is a simplified version of what fish completions for ssh. you can see the source here

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

No branches or pull requests

4 participants