Bash – Don’t suggest current directory in autocomplete

autocompletebashdirectoryzsh

I am a long time user of ZSH. It seems to me that I used to have this functionality a long time ago but then I rewrote my .zshrc (a long time ago) and now I miss this functionality every now and then. (Of course, being a long time ago, my memory may be faulty.)

What I want is that when I am autocompleting (such as pressing tab etc.) then I don't want suggestions from my current directory if I am starting from 1 level up (i.e., '..') or more.

So if I do

/usr/share$ cd ../<tab>

then I don't want my current directory as a suggestion

/usr/share$ cd ../share/

The question is for ZSH but I would love to know if it can be done in Bash too.

Best Answer

From http://zshwiki.org/home/examples/compsys/ignore#ignore_parent_directory

zstyle ':completion:*:(cd|mv|cp):*' ignore-parents parent pwd

See also info zsh ignore-parents for details. Reproduced below for zsh 5.4.2:

ignore-parents

 The style is tested without a tag by the function completing
 pathnames in order to determine whether to ignore the names of
 directories already mentioned in the current word, or the name of
 the current working directory.  The value must include one or both
 of the following strings:

 parent
      The name of any directory whose path is already contained in
      the word on the line is ignored.  For example, when completing
      after foo/../, the directory foo will not be considered a
      valid completion.

 pwd
      The name of the current working directory will not be
      completed; hence, for example, completion after ../ will not
      use the name of the current directory.

 In addition, the value may include one or both of:

 ..
      Ignore the specified directories only when the word on the
      line contains the substring '../'.

 directory
      Ignore the specified directories only when names of
      directories are completed, not when completing names of files.

 Excluded values act in a similar fashion to values of the
 ignored-patterns style, so they can be restored to consideration by
 the _ignored completer.