I added loads of entries to my /etc/hosts file to aid in adblocking and tracking on the internet (a list I got from github https://github.com/StevenBlack/hosts).

However, zsh now incorporates all the entries in the hosts file when searching for autocompletes. This means I can be moving a simple file with a long name and I type mv w<TAB> and I get hundreds of autocomplete entries with the filename I want buried in there somewhere.

Is there a way to restrict the sources zsh uses for autocompletion?

Edit: here are what I think are the relevant parts of my zshrc:

zstyle ':completion:*' completer _complete _ignored _approximate
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' matcher-list ''
zstyle ':completion:*' menu select=1
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
autoload -Uz compinit
autoload -U zutil
autoload -U complist
  • Let me guess... you are using d'oh-my-zsh or something similar? – mpy Jul 11 '16 at 17:08
  • @mpy Nope, no zsh plugins. I can post my .zshrc if it would help. – thosphor Jul 12 '16 at 15:23
  • Yes, post your .zshrc if it's not too big. If it's more than 50 lines please try to narrow the behavior down to a specific part of it before. – mpy Jul 12 '16 at 17:08
  • @mpy Edited the question to include the relevant bits. – thosphor Jul 12 '16 at 17:27
  • 1
    Check out nion.modprobe.de/blog/archives/… for a starting point. – mpy Jul 13 '16 at 18:06

By default, zsh populates its hosts completion list from the command getent hosts if it's available, or from certain files, including /ect/hosts.

If we want to skip this part, we need to provide our own completion logic. You can do so by adding the following to your ~/.zshrc:

zstyle -e ':completion:*:hosts' hosts 'reply=(
  ${=${${(f)"$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) 2>/dev/null)"}%%[#| ]*}//,/ }
  ${=${${${${(@M)${(f)"$(cat ~/.ssh/config 2>/dev/null)"}:#Host *}#Host }:#*\**}:#*\?*}}

Inspiration: Voku's solution in their dotfiles, by leaving out the /ect/hosts part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.