Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim find command: how to list all matched files

Tags:

file

find

vim

The :find command of vim just matches only one file. If more files are matched, it gives message "Too many file names" . Is there any command of vim which can find files by using wildcards or regular expression, and allow user to navigate between these files matched?

like image 551
river Avatar asked Jun 01 '14 03:06

river


1 Answers

You seem to be confusing Vim's command-line completion and the :find command itself.

:find only accepts one single filename (or something that resolves to a single filename) as argument but the command-line completion that lets you complete that single argument allows you to go through all the files in your path matching your current pattern.

That confusion makes what you actually want non-obvious:

  • do you want :find to open for editing every file matching your pattern?
  • or do you want a list of matches from which to choose from?

The former is impossible by design but you can use the :new command (:help :new):

:new *.foo

The latter is possible, of course. For that, you'll need to set at least a few options in your ~/.vimrc:

set wildmenu
set wildmode=list:full

See :help wildmode for how to customize the wildmenu's behavior. Those settings will of course work for other commands: :edit, :split, :buffer

A few suggestions:

set path+=**

lets Vim find files recursively under the other directories in path. Note that ** is very greedy, which means that Vim will really look everywhere under those directories, which can be very expensive and slow (think node_modules). As with lots of things in Vim, the path value should be context-dependant.

set wildignorecase

tells Vim to ignore case for completion: :find foo will match foo.txt and Foo.txt.

set wildignore=*.foo,*.bar

tells Vim to ignore those files (it can be directories) when doing completion.

Finally, here are a bunch of mappings that make my life (in Vim) so easy:

" regex completion instead of whole word completion
nnoremap <leader>f :find *
" restrict the matching to files under the directory
" of the current file, recursively
nnoremap <leader>F :find <C-R>=expand('%:p:h').'/**/*'<CR>

" same as the two above but opens the file in an horizontal window
nnoremap <leader>s :sfind *
nnoremap <leader>S :sfind <C-R>=expand('%:p:h').'/**/*'<CR>

" same as the two above but with a vertical window
nnoremap <leader>v :vert sfind *
nnoremap <leader>V :vert sfind <C-R>=expand('%:p:h').'/**/*'<CR>

Here is how it looks:

:find

like image 62
romainl Avatar answered Sep 20 '22 15:09

romainl