Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VIM: Using the find command recursively

2 views
Skip to first unread message

DJK

unread,
May 26, 2005, 11:20:49 AM5/26/05
to
Is there a way for the find (or an alternative)
command to search through subdirectories in the path.

i.e., if I had a file structure like

.
./directory1
./directory1/file1
./directory1/file2
./directory2
./directory2/file1
./directory2/file2

and I typed :find file2 I would like it to present me
with a list of matching files and allow me to select
the file to edit. Is this possible today?

Is there a way I can use the find from the shell to
invoke this functionality?

Thanks,
Dennis

Preben 'Peppe' Guldberg

unread,
May 26, 2005, 1:41:26 PM5/26/05
to
DJK wrote:
> Is there a way for the find (or an alternative)
> command to search through subdirectories in the path.

> i.e., if I had a file structure like

> .
> ./directory1
> ./directory1/file1
> ./directory1/file2
> ./directory2
> ./directory2/file1
> ./directory2/file2

> and I typed :find file2 I would like it to present me
> with a list of matching files and allow me to select
> the file to edit. Is this possible today?

It's doable, if you don't mind traversing the directories yourself in a
user defined function. Approach is pretty much like below, except you'd
need to do it without extarnal commands.

> Is there a way I can use the find from the shell to
> invoke this functionality?

How about this:

:fun Find(path, ...)
return system('find "' . a:path . '" -type f')
endfun

:com -nargs=1 -complete=custom,Find Find

There are probably some very rough edges, but as a quick hack it does
the trick for me.

Peppe
--
se nocp cpo=BceFsx!$ hid bs=2 ls=2 hls ic " P. Guldberg /bin/v...@wielders.org
se scs ai isf-== fdo-=block cino=t0,:0 hi=100 ru so=4 noea lz|if has('unix')
se sh=/bin/sh|en|syn on|filetype plugin indent on|ono S V/\n^-- $\\|\%$/<CR>
cno <C-A> <C-B>|au FileType vim,mail se sw=4 sts=4 et|let&tw=72+6*(&ft=~'v')

DJK

unread,
May 28, 2005, 10:38:12 PM5/28/05
to
So would I have to use python or something to search through the
directories? I didn't see any built in functions that would give this
to me. Also, when I do have the list of files, how to I get them in an
error list or something so I can navigate them like when I use grep?

thanks,
Dennis

Preben 'Peppe' Guldberg

unread,
May 29, 2005, 12:21:00 PM5/29/05
to
DJK wrote:
> So would I have to use python or something to search through the
> directories?

[You have not given much context for your questions. When following up
on specific points, please quote enough context to identify it. Thanks.]

On systems that has find, that command should do it, as mentioned.
Did this work for you?

Or do you mean a python enabled vim? I don't think this is necessary.

> I didn't see any built in functions that would give this
> to me.

I haven't tried, but using globpath() and isdirectory() should help you
recurse down through a directory tree. globpath() and filereadable()
and/or filewritable() should help you work with individual files.

> Also, when I do have the list of files, how to I get them in an
> error list or something so I can navigate them like when I use grep?

How much trouble are you willing to go through?

I would probably put the list in a new buffer and do

:%s/$/:1:dummy/ " fake some context
:w
:cf %

It's manual work, but does the trick quickly.

DJK

unread,
Jun 2, 2005, 10:03:56 AM6/2/05
to
>On systems that has find, that command should do it, as mentioned.
>Did this work for you?

>Or do you mean a python enabled vim? I don't think this is necessary.

I am using windows which doesn't have find. I downloaded the GNU find,
but I can use the backticks to call it from VIM.

>How much trouble are you willing to go through?

As much as it takes. Besides being useful for me, it is a learning
experience.

>I would probably put the list in a new buffer and do

> :%s/$/:1:dummy/ " fake some context
> :w
> :cf %

>It's manual work, but does the trick quickly.

Can I put hyperlinks around the file path and pass it to vim, that way
I can jump straight to it somehow (like the help system)?

thanks,
Dennis

Preben 'Peppe' Guldberg

unread,
Jun 2, 2005, 2:29:19 PM6/2/05
to
DJK wrote:
> >On systems that has find, that command should do it, as mentioned.
> >Did this work for you?

> >Or do you mean a python enabled vim? I don't think this is necessary.

> I am using windows which doesn't have find. I downloaded the GNU find,
> but I can use the backticks to call it from VIM.

> >How much trouble are you willing to go through?
> As much as it takes. Besides being useful for me, it is a learning
> experience.

I think writing a function to traverse directories and finding files
based on some criteria, using only vim internal commands would be a good
learning experience :-)

I am quite convinced that it is possible using the functions I pointed
you to earlier.

> >I would probably put the list in a new buffer and do

> > :%s/$/:1:dummy/ " fake some context
> > :w
> > :cf %

> >It's manual work, but does the trick quickly.

> Can I put hyperlinks around the file path and pass it to vim, that way
> I can jump straight to it somehow (like the help system)?

As long as the files are relative to somewhere in the 'path' option,
simply using the gf and <C-W>f normal mode commands should work without
problems. Simply place the cursor on a file name and type "gf".

0 new messages