How to autocomplete file paths in Vim, just like in zsh

autocompletevimzsh

In Zsh, I can use filename completion with slashes to target a file deep in my source tree. For instance if I type:

 vim s/w/t/u/f >TAB<

zsh replaces the pattern with:

 vim src/wp-contents/themes/us/functions.php

What I'd like is to be able to target files the same way at the Vim command line, so that typing

 :vi s/w/t/u/f >TAB<

will autocomplete to:

 :vi src/wp-contents/themes/us/functions.php

I'm trying to parse the Vim docs for wildmode, but I don't see what settings would give me this. It's doing autocompletion for individual filenames, but not file paths. Does Vim support this natively? Or how can I customize the autocomplete algorithm for files?

Thanks for any advice!
-mykle-

Best Answer

I couldn't find a plugin to do this, so I wrote one. It's called vim-zsh-path-completion. It does what you're looking for, although via <C-s> rather than <Tab>. You can use it with <Tab> for even more control over what matches, though.

It's got bugs, but for basic paths without spaces/special characters, it should work. I think it's useful enough in its current state to be helpful. I hope to iron out the bugs and clean up the code, but I figured I'd start soliciting feedback now.

Thanks for the idea!

Original (wrong) answer, but with some useful information about Vim's wildmode.

Put the following in your .vimrc:

set wildmenu
set wildmode=list:longest

That will complete to the longest unique match on <Tab>, including appending a / and descending into directories where appropriate. If there are multiple matches, it will show a list of matches for what you've entered so far. Then you can type more characters and <Tab> again to complete.

I prefer the following setting, which completes to the first unique match on <Tab>, and then pops up a menu if you hit <Tab> again, which you can navigate with the arrow keys and hit enter to select from:

set wildmode=list:longest,list:full

Check out :help wildmenu and :help wildmode. You might also want to set wildignore to a list of patterns to ignore when completing. I have mine as:

set wildignore=.git,*.swp,*/tmp/*