Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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-

like image 442
Mykle Hansen Avatar asked Mar 23 '13 00:03

Mykle Hansen


2 Answers

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/*
like image 145
Jim Stewart Avatar answered Oct 20 '22 22:10

Jim Stewart


Vim doesn't have such a feature by default. The closest buil-in feature is the wildmenu/wildmode combo but it's still very different.

A quick look at the script section of vim.org didn't return anything but I didn't look too far: you should dig further. Maybe it's there, somewhere.

Did you try Command-T, LustyExplorer, FuzzyFinder, CtrlP or one of the many similar plugins?

I use CtrlP and fuzzy matching can be done on filepath or filename. When done on filepath, I can use the keysequence below to open src/wp-contents/themes/us/functions.php (assuming functions.php is the only file under us that starts with a f):

,f " my custom mapping for the :CtrlP command
swtuf<CR>

edit

In thinking about a possible solution I'm afraid I was a little myopic. I was focused on your exact requirements but Vim has cool tricks when it comes to opening files!

The :e[dit] command accepts two types of wildcards: * is like the * you would use in your shell and ** means "any subdirectory".

So it's entirely possible to do:

:e s*/w*/t*/u*/f*<Tab>

or something like:

:e **/us/f<Tab>

or even:

:e **/fun<Tab>

Combined with the wildmode settings in Jim's answer, I think you have got a pretty powerful file navigation tool, here.

like image 34
romainl Avatar answered Oct 20 '22 21:10

romainl