I wrote a function to get my hosts from ssh config without getting wildcard hosts:
sshConfAutoComplete() {
cat ~/.ssh/config | \
grep 'host ' | \
sed '
s#.*\*##g;
s#host ##g
'
}
Output:
pi
lynx
iridium
batchelorantor
rasp
The output is correct, so I added this function to:
/usr/local/etc/bash_completion.d/ssh
Like this:
sshConfAutoComplete() {
cat ~/.ssh/config | \
grep 'host ' | \
sed '
s#.*\*##g;
s#host ##g
'
}
complete -F sshConfAutoComplete ssh
Then I added this source: . /usr/local/etc/bash_completion.d/ssh
to ~/.bash_profile
Sourced ~/bash_profile
, then when I type ssh <tab>
the following comes up:
pi
lynx
iridium
batchelorantor
rasp
If I type ssh ly <tab>
it doesn't autocomplete to lynx
, it just outputs the above.
How do I fix this?
;
, and so on. – meuh Nov 15 '23 at 17:23sed
cmd instead of;
? – Nickotine Nov 15 '23 at 18:16sed -n
can you explain the curly brace syntax please, and why you need the p? I see people saying it's to tellsed
to print once but I've never had the problem of it printing twice @meuh, also confused as to why you give seem to pass in/^host/
as a parameter if that's what's going on? – Nickotine Nov 16 '23 at 18:42sed
reads an input line, applies the commands in the script, and outputs the resulting line with any changes made. The-n
option suppresses this default output, so instead you must use thep
command to produce output of the current changes. The alternative is to used
to delete all the unwanted lines so they don't print.{}
are just used to join many commands into one, as a sed script consists principally of a pattern to match a line against, and a single command to apply if it matches. The alternative would be to repeat the match for each command. – meuh Nov 17 '23 at 14:30p
and the-n
option. Thanks, much appreciated. – Nickotine Nov 18 '23 at 14:57