1

My zsh has some completion features I don't understand and can't find where to change. I have two issues where I suspect they have a similar "fix" for my problem. I initialize the zsh completion system with

autoload -Uz compinit
compinit

to get advanced completion features, but I also get the following problems that I don't have without compinit.

First: I happen to have a directory called mydir in my home directory and unfortunately, there is also a user called mydir. When I want to change into my directory and then use tab completion, i.e.

cd mydir/<TAB>

I get the content directories of ~myusername/mydir/ along with all directories available for ~mydir/. I already tried to put

zstyle ':completion:*' users myusername

in my .zlogin file, but it does only change the completion of the username itself and not subsequent directories. Is there some similar switch to turn off completion of other users' home directories? Alternatively, it would already be good if the current directory completion would appear first in the completion menu.

Second: I wrote a script called setup-file-with-a-long-name.sh that resides in my home directory. When I want to execute it via

source setup-file-with-a-long-name.sh

I start with the first few characters, I press <TAB> and I get a list of lots of executable files that are probably somewhere on my $PATH installed by the system, but I don't care about all those files, I just want my file (which is the only match in the current directory) to be displayed either first in the menu and accessible via <TAB> <TAB> or better yet, be accepted after the first <TAB>. (If I select any of them, they don't work anyway because source needs the absolute path, not the filename. Therefore this is a behavior I don't understand and can't see how this is useful as a default for anybody.)

Possible workarounds:
1. Write ~/ explicitly - this is what I want to avoid, because I have to ssh into a new shell pretty often and want to start navigating without thinking about whether I am in $HOME or not.
2. Don't use compinit - well, I like the context-aware completion in principle, I just want to adapt it to my needs.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
skaphle
  • 26
  • 6

1 Answers1

0

The following works in bash,

man source -

source filename [arguments]
              Read  and  execute  commands  from filename in the current shell environment and return the exit status of the last command executed from filename.  If filename does not contain a slash, file
              names in PATH are used to find the directory containing filename.  The file searched for in PATH need not be executable.  When bash is not in posix mode, the current directory is searched  if
              no file is found in PATH.  If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched.

to disable the flag instructions are a little above the description of sourcepath

       shopt [-pqsu] [-o] [optname ...]
              Toggle the values of variables controlling optional shell behavior.  With no options, or with the -p option, a list of all settable options is displayed, with an indication of
              whether or not each is set.  The -p option causes output to be displayed in a form that may be reused as input.  Other options have the following meanings:
              -s     Enable (set) each optname.
              -u     Disable (unset) each optname.
              -q     Suppresses normal output (quiet mode); the return status indicates whether the optname is set or unset.  If multiple optname arguments are given  with  -q,  the  return
                     status is zero if all optnames are enabled; non-zero otherwise.
              -o     Restricts the values of optname to be those defined for the -o option to the set builtin.
...
             sourcepath
                      If set, the source (.) builtin uses the value of PATH to find the directory containing the file supplied as an argument.  This option is enabled by default.

so executing the following should remove path from your tab completion...

shopt -u sourcepath

Calvin Taylor
  • 664
  • 4
  • 15
  • Thank you. But when I do this, it says the `shopt` command was not found. – skaphle Jun 01 '17 at 12:35
  • I see, my answer works for bash I assumed it would for zsh as well, sorry about that. I started looking into zsh builtin's as well, and its seems to be different enough to be a bit of a pain, man zshoptions and zshbuiltins. setopt seems to be the command of interest and it may be the HASH_LIST_ALL option but I haven't proven it yet. – Calvin Taylor Jun 01 '17 at 19:30