Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle all hostaliases in /etc/hosts #3495

Merged
merged 2 commits into from
Oct 29, 2023
Merged

Handle all hostaliases in /etc/hosts #3495

merged 2 commits into from
Oct 29, 2023

Conversation

akdevservices
Copy link
Contributor

Fixes #3488

@akdevservices akdevservices marked this pull request as ready for review October 27, 2023 20:10
@akdevservices akdevservices changed the title Fix #3488 Handle all hostaliases in /etc/hosts Oct 27, 2023
@junegunn
Copy link
Owner

Thanks, it looks like we should handle in-line comments.

127.0.0.1	localhost home # homey

What do you think?

diff --git a/shell/completion.bash b/shell/completion.bash
index c42909b..db84e06 100644
--- a/shell/completion.bash
+++ b/shell/completion.bash
@@ -423,7 +423,7 @@ if ! declare -F __fzf_list_hosts > /dev/null; then
   __fzf_list_hosts() {
     command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | command awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \
       <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts 2> /dev/null | command tr ',' '\n' | command tr -d '[' | command awk '{ print $1 " " $1 }') \
-      <(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0') |
+      <(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0' | command sed 's/#.*//') |
       command awk '{for (i = 2; i <= NF; i++) print $i}' | command sort -u
   }
 fi
diff --git a/shell/completion.zsh b/shell/completion.zsh
index 1189c26..c4ba2c7 100644
--- a/shell/completion.zsh
+++ b/shell/completion.zsh
@@ -226,7 +226,7 @@ if ! declare -f __fzf_list_hosts > /dev/null; then
     setopt localoptions nonomatch
     command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \
       <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts 2> /dev/null | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \
-      <(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0') |
+      <(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0' | command sed 's/#.*//') |
       awk '{for (i = 2; i <= NF; i++) print $i}' | sort -u
   }
 fi

@akdevservices
Copy link
Contributor Author

akdevservices commented Oct 28, 2023

What do you think?

It's a fix for another issue :) I've included it in this pull request.

@junegunn junegunn merged commit a62fe3d into junegunn:master Oct 29, 2023
5 checks passed
@junegunn
Copy link
Owner

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Host completion does not see all hostaliases in /etc/hosts
2 participants