Skip to main content

SSH - Autosuggestions with ZSH

ยท 3 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

SSH - Autosuggestions with ZSH

A few weeks ago, I've posted a SSH - Launch a terminal on your session without having to authenticate yourself article about the ssh command in Linux.

I'm pretty sure, like me, you are tired to use command line like ssh christophe@1.2.3.4 to start a ssh connection because ... you know, you don't need to connect on a server; no, you need to connect to the server where the application is running; you know the name of the MyAmazingApp application but certainly not the name of the server or its IP.

That's the case for me anyway.

So, I need to connect to my vault, in which I list all the information about applications, server names, credentials to be used, ...

It would be pretty cool to run ssh MyAmazingApp no?

It should be really nice to be able to run ssh AmazingApp and hop, I'm connected on the server.

This is where the ~/.ssh/config is so helpful (please refer to this article) but we can go one step further: it would be so great to type ssh and by some magic, Linux will show you the list of aliases defined in the ~/.ssh/config file.

Installation of the zsh-ssh-config-suggestions pluginโ€‹

If you've a ZSH user, please surf to https://github.com/yngc0der/zsh-ssh-config-suggestions and follow installation's instructions.

In short:

  1. First run this command git clone https://github.com/yngc0der/zsh-ssh-config-suggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-ssh-config-suggestions to get a local copy of the plugin in your Oh-my-zsh plugin folder.
  2. Edit your ~/.zshrc file, search the line with the plugins=( declaration and just add zsh-ssh-config-suggestions in the list.
  3. Close and save your changes.
  4. Run source ~/.zshrc to reload that file so your change can be taken into account.

Use itโ€‹

Imagine I've this content in my ~/.ssh/config:

Host MyAmazingApp_PROD
Hostname 1.2.3.4
User usr_app_prod

Host MyAmazingApp_TEST
Hostname 6.7.8.9
User usr_app_test

Host YourAmazingApp
Hostname 1.2.3.4
User you

Host LegacyApp
Hostname 1.2.3.4
User legacy

Now, I really don't need to remember anything and I don't even need to know the names of the aliases any more!

I just need to type (it's important): ssh followed by TAB

Using ssh-config-suggestions

You should add a space character after ssh

To make it working, please note: you should add a space character after having typed ssh and before pressing tab.

As you can see, the system will display the list of hosts defined in my configuration file! I can then edit the file, add a new application and hop, next time, I'll get his name in the list. I don't need anymore to connect to my vault. Nice no?