2

Here's an example snippet from my current zsh session:

llama@llama:...Code/cpp/KingOfTheCode$ make
clang++ -std=c++11 -Iinclude src/*.cpp kothsrc/*.cpp -pthread -o KingOfTheCode
llama@llama:...Code/cpp/KingOfTheCode$ ./
Completing executable file or directory
include/        KingOfTheCode*  kothsrc/        src/

(I typed a ., a /, and then a Tab character for autocompletion.)

Why does zsh suggest directories when I type ./<tab>? I clearly want to execute a file, and if I wanted to execute something in a subdirectory the ./ part would be useless.

How can I prevent this annoying behavior from occurring? Just to be clear, my desired behavior is that autocompletion for ./<...> excludes directories and only looks for executable files.

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • I thought the `./` would just refer to the current directory. Why would a sub-directory be useless then? – tiguchi Oct 28 '14 at 21:37
  • @NobuGames When I type `./`, it's usually because I want to execute something in the current directory (for example, `./something.py`). If I wanted to execute, say, `foo/bar/baz/something.py`, the `./` would be useless because it's implied. – tckmn Oct 28 '14 at 21:38
  • I guess it would be fine if `setout autocd` is in effect, so we can type `./`, `Tab`, `Tab`... for `cd`ing subdirectory. But I personally am not using `autocd` a lot anyway. – hchbaw Dec 25 '14 at 03:21
  • Ugh… I'm very sorry for above my comment, `s/setout/setopt/`. – hchbaw Dec 25 '14 at 05:18

1 Answers1

4

I assume that there is compinit somewhere in the ~/.zshrc.

You could limit the completion results by zstylelike this:

zstyle -e ':completion::complete:-command-::executables' ignored-patterns '
  [[ "$PREFIX" == ./* ]] && { reply=(./*(/)) }
'

This adds the ignored-patterns zstyle entries when typing ./<...> then hitting the Tab. Abobe ./*(/) part indicates directories in the current directory. As a result, any subdirectories will be removed from completion results when completing the “executable file or directory”.


If you want to reinstate completion results without ignored-patterns effects if there are no matching completions, you could add the _ignored completer:

# this is same as the zsh default behavior though.
zstyle ':completion:*' completer _complete _ignored

It should complete the ignored subdirectories in this case with typing ./koTab./koshsrc/.


Update: Mon, 29 Dec 2014 21:30:37 +0900 (Thank you for your suggestion, @Francisco)
Teach to skip directories of which namestring has some spaces in it.

zstyle -e ':completion::complete:-command-::executables' ignored-patterns '
  [[ "$PREFIX" == ./* ]] && {
    local -a tmp; set -A tmp ./*(/)
    : ${(A)tmp::=${tmp// /\\ }}
    reply=(${(j:|:)tmp})
  }
'
hchbaw
  • 4,894
  • 18
  • 16
  • 1
    Why did this answer get a down vote? It works. (Ok, it does not work for directories with a space in it, but other than that it 'works for me'). – Francisco Dec 29 '14 at 10:36
  • 1
    I've updated to try to handle spaces correctly. Thank you very much for your report, @Francisco! – hchbaw Dec 29 '14 at 13:14