Repository: junegunn/fzf (language: go)

Issue:
Host completion does not see all hostaliases in /etc/hosts
<!-- ISSUES NOT FOLLOWING THIS TEMPLATE WILL BE CLOSED AND DELETED -->

<!-- Check all that apply [x] -->

- [x] I have read through the manual page (`man fzf`)
- [x] I have the latest version of fzf
- [x] I have searched through the existing issues

## Info

- OS
    - [x] Linux
    - [ ] Mac OS X
    - [ ] Windows
    - [ ] Etc.
- Shell
    - [x] bash
    - [ ] zsh
    - [ ] fish

## Problem / Steps to reproduce

Host completion does not see all hostaliases in /etc/hosts, only the first hostname is available.
`/etc/hosts`:
```
127.0.0.1 localhost name1 name2
```
`name1` and `name2` are not available, only `localhost` is displayed by fzf.

I took a look at `_fzf_host_completion` function, awk script needs enhancement, the current version of the script handles only the second field: `awk '{if (length($2) > 0) {print $2}}'`
Host completion does not see all hostaliases in /etc/hosts
<!-- ISSUES NOT FOLLOWING THIS TEMPLATE WILL BE CLOSED AND DELETED -->

<!-- Check all that apply [x] -->

- [x] I have read through the manual page (`man fzf`)
- [x] I have the latest version of fzf
- [x] I have searched through the existing issues

## Info

- OS
    - [x] Linux
    - [ ] Mac OS X
    - [ ] Windows
    - [ ] Etc.
- Shell
    - [x] bash
    - [ ] zsh
    - [ ] fish

## Problem / Steps to reproduce

Host completion does not see all hostaliases in /etc/hosts, only the first hostname is available.
`/etc/hosts`:
```
127.0.0.1 localhost name1 name2
```
`name1` and `name2` are not available, only `localhost` is displayed by fzf.

I took a look at `_fzf_host_completion` function, awk script needs enhancement, the current version of the script handles only the second field: `awk '{if (length($2) > 0) {print $2}}'`


Discussion / hints:
That's because the code that collects the hostnames is uhm... limited.
You may use something like [this](https://github.com/junegunn/fzf/commit/d718747c5b115838af7ceaf0d4a7f1c84ed7d98b) as an alternative.
That's because the code that collects the hostnames is uhm... limited.
You may use something like [this](https://github.com/junegunn/fzf/commit/d718747c5b115838af7ceaf0d4a7f1c84ed7d98b) as an alternative.

Fix patch:
diff --git a/shell/completion.bash b/shell/completion.bash
index 37a37948251..db84e06a1ee 100644
--- a/shell/completion.bash
+++ b/shell/completion.bash
@@ -423,8 +423,8 @@ 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 awk '{if (length($2) > 0) {print $2}}' | command sort -u
+      <(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 c01bbb825f8..c4ba2c758b3 100644
--- a/shell/completion.zsh
+++ b/shell/completion.zsh
@@ -226,8 +226,8 @@ 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') |
-      awk '{if (length($2) > 0) {print $2}}' | sort -u
+      <(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
 


Test patch: