From 9b6bf4a7cd0f05600fd5ea9c60d330136aebc666 Mon Sep 17 00:00:00 2001 From: Indrajit Raychaudhuri Date: Wed, 26 Jul 2017 15:16:40 -0500 Subject: [PATCH] [completion] Support ignoring '/etc/hosts' entries in host completion Entries from static '/etc/hosts' can now be ignored via 'zstyle' based configuration. Both IP address and corresponding hostname will be ignored during host completion. However, some of the entries ignored from '/etc/hosts' still might appear during completion because of their presence in 'ssh' configuration ('~/.ssh/config') or history ('~/.ssh/ssh_hosts', '~/.ssh/known_hosts' etc.). --- modules/completion/README.md | 17 +++++++++++++++++ modules/completion/init.zsh | 9 +++++++-- runcoms/zpreztorc | 8 ++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/modules/completion/README.md b/modules/completion/README.md index aaa86fdd..7efb3d96 100644 --- a/modules/completion/README.md +++ b/modules/completion/README.md @@ -6,6 +6,23 @@ the [zsh-completions][1] project. This module must be loaded **after** the *utility* module. +Settings +-------- + +### Ignore */etc/hosts* Entries + +To ignore certain entries from static */etc/hosts* for host completion, add the +following lines in *zpreztorc* with the IP addresses of the hosts as they +appear in */etc/hosts*. Both IP address and the corresponding hostname will be +ignored during host completion. However, some of the entries ignored from +*/etc/hosts* still might appear during completion because of their presence in +*ssh* configuration or history). + +```sh +zstyle ':prezto:module:completion:*:hosts' etc-host-ignores \ + '0.0.0.0' '127.0.0.1' +``` + Contributors ------------ diff --git a/modules/completion/init.zsh b/modules/completion/init.zsh index ca2117b4..e4f6a337 100644 --- a/modules/completion/init.zsh +++ b/modules/completion/init.zsh @@ -102,12 +102,17 @@ zstyle ':completion:*:history-words' menu yes # Environmental Variables zstyle ':completion::*:(-command-|export):*' fake-parameters ${${${_comps[(I)-value-*]#*,}%%,*}:#-*-} -# Populate hostname completion. +# Populate hostname completion. But allow ignoring custom entries from static +# */etc/hosts* which might be uninteresting. +zstyle -a ':prezto:module:completion:*:hosts' etc-host-ignores 'etc_host_ignores' +etc_host_ignores=('\#' $etc_host_ignores) + zstyle -e ':completion:*:hosts' hosts 'reply=( ${=${=${=${${(f)"$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) 2>/dev/null)"}%%[#| ]*}//\]:[0-9]*/ }//,/ }//\[/ } - ${=${(f)"$(cat /etc/hosts(|)(N) <<(ypcat hosts 2>/dev/null))"}%%\#*} + ${=${(f)"$(cat /etc/hosts(|)(N) <<(ypcat hosts 2>/dev/null))"}%%${(j:*|:)~etc_host_ignores}*} ${=${${${${(@M)${(f)"$(cat ~/.ssh/config 2>/dev/null)"}:#Host *}#Host }:#*\**}:#*\?*}} )' +unset etc_host_ignores # Don't complete uninteresting users... zstyle ':completion:*:*:*:users' ignored-patterns \ diff --git a/runcoms/zpreztorc b/runcoms/zpreztorc index 9a768df7..0b7a8376 100644 --- a/runcoms/zpreztorc +++ b/runcoms/zpreztorc @@ -41,6 +41,14 @@ zstyle ':prezto:load' pmodule \ # Set the query found color. # zstyle ':prezto:module:autosuggestions:color' found '' +# +# Completions +# + +# Set the entries to ignore in static */etc/hosts* for host completion. +# zstyle ':prezto:module:completion:*:hosts' etc-host-ignores \ +# '0.0.0.0' '127.0.0.1' + # # Editor #