Search code examples
zshzsh-completion

ZSH: tab complete file in subdirectory by name


I would like to be able to type a filename in zsh, hit tab, and see a list of files that match that name in any subdirectories of my current directory. Then I could tab through those results and hit enter to select a certain file, just like the built-in zsh tab completion functionality works.

Example desired functionality:

$ emacs app.css [tab]
*www/css/app.css*    tmp/static/app.css
[enter]
$ emacs www/css/app.css

If this is not currently a feature, I assume I could write a custom completion script to implement it.


Solution

  • You can achieve something similar by utilizing the ** pattern (match over multiple directories) and by adding the control function _expand to the completer style (if not already the case).

    As a minimum, you need to make the following settings:

    autoload -U compinit
    zstyle ':completion:*' completer _expand
    compinit
    

    Then you can get the desired list with

    % emacs **/app.css[TAB]
    

    It even allows for the option to open all matching files at once.