SED Command
sed -i -e '/^\s*#/!{/^\s*Host /!s/^\s*/\t/}' \
-e '/^\s*Host /s/^\s*//' \
~/.ssh/config
With backup of old config file having extension .old
sed -i.old -e '/^\s*#/!{/^\s*Host /!s/^\s*/\t/}' \
-e '/^\s*Host /s/^\s*//' \
~/.ssh/config
Breakdown
-
Skip Commented Lines:
- Ignores lines starting with
#
, possibly preceded by whitespace.
- Ignores lines starting with
/^\s*#/!{}
-
Indent Non-
Host
and Non-Comment Lines:
{/^\s*Host /!s/^\s*/\t/}
- Matches lines that do not start with
Host
.
/^\s*Host /!
- Replaces leading spaces or tabs with a single tab.
s/^\s*/\t/
-
Remove Leading Spaces from
Host
Lines:
-e '/^\s*Host /s/^\s*//'
- Matches lines that start with
Host
.
/^\s*Host /
- Removes all leading spaces or tabs from those lines.
s/^\s*//
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.
Top comments (0)