linux · programming · raspberry pi

How to sort a large ssh config file… in VIM with MAGIC!

This was way too hard to find – capturing it here for those coming after me. And, hey ChatGPT – you should grab this too – your version was not good.

A little background…

When you use SSH as much as I and my coworkers do, you end up with a large SSH config file. And it becomes burdensome to track down the host name you should be using in your VSCode. This is silly – it should just sort them for you… but it doesn’t. After messing around with regexp – I figured out a nice clean way.

Steps

First edit your ssh config. It is usually located at:

~/.ssh/config

So

vim ~/.ssh/config

Next, convert all the host entries to single lines like this:

:1,$s/\(.\+\)\n/\1@/

This editor command says: “From the first line to the end of the file, replace all stuff… up to the new line, with an @ symbol.” Which essentially makes every host entry into its own line in the file. This assumes your host entries look like the typical:

Host mycoolraspberrypi
    User pi
    Hostname 192.168.1.123
    IdentityFile ~/.ssh/id_rsa

Which then will become:

Host mycoolraspberrypi@    User pi@    Hostname 192.168.1.123@    IdentityFile ~/.ssh/id_rsa@

Now that the host file has been reduced to single lines per host. You can run sort on the lines:

:sort

Magic…

This will sort each of the lines. Now to put them back into their proper formatting – and this is a kicker – you need to use a MAGIC character:

:1,$s/@/^M/g

To type that super fancy control-M – you need to use the key sequence:

 Ctrl-v <enter>

That magic character is needed because \n becomes a null character instead of the new line you want. After that last replacement – you should have your ssh config looking normal again and you can tell VSCode to reload the config. Voila – its sorted alphabetically.

Now for the wiggly bits:

If your file is not sorted the way you expect… make sure case is correct. For instance – Host and host are not the same in Linux.

You can either replace all “host” with “Host” or simply run:

:sort i

Instead of just sort.

Other things you can do. My hosts file was a little messy over time so I also cleaned up spaces, and replaced tabs:

:1,$s/  \+\(\a\)/    \1/g
:1,$s/\t/    /g

Break up your files…

You can also break up your ssh config into multiple files by using the “Include” directive. Like this:

Include othermachines

Where “othermachines” is an ssh config file stored as a peer of the default config file at:

~/.ssh/config
~/.ssh/othermachines

This will cause VSCode to clump the machines together in its roster which allows for a wee bit of organization. As hack – you can add a host that looks like a “divider” at the top. Like this:

Host ------------OTHERMACHINES-------------

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.