SED Command
sed -i -e '/^\s*#/!{/^\s*Host /!s/^\s*/\t/}' \
-e '/^\s*Host /s/^\s*//' \
~/.ssh/config
Breakdown
Skip Commented Lines:
/^\s*#/!{}: Ignores lines starting with#, possibly preceded by whitespace.
Indent Non-
Hostand Non-Comment Lines:
{/^\s*Host /!s/^\s*/\t/}
/^\s*Host /!: Matches lines that do not start withHost.
s/^\s*/\t/: Replaces leading spaces or tabs with a single tab.
Remove Leading Spaces fromHostLines:
-e '/^\s*Host /s/^\s*//'
/^\s*Host /: Matches lines that start withHost.
s/^\s*//: Removes all leading spaces or tabs from those lines.
Example
Input:
Host myserver
HostName ssh.example.com
User myuser
Port 22
IdentityFile ~/.ssh/mykey
Host another-server
HostName another.example.com
User anotheruser
Port 2022
IdentityFile ~/.ssh/anotherkey
Output:
Host myserver
HostName ssh.example.com
User myuser
Port 22
IdentityFile ~/.ssh/mykey
Host another-server
HostName another.example.com
User anotheruser
Port 2022
IdentityFile ~/.ssh/anotherkey
This formats the file in place (-i flag), ensuring consistent indentation and removing leading spaces.
SOCIAL SHARE CARD GENERATOR