On Tue, 6 Jun 2006, Max Dyckhoff wrote:

-----Original Message-----
From: Gerald Lai [mailto:[EMAIL PROTECTED]
Sent: Tuesday, June 06, 2006 2:40 PM
To: Max Dyckhoff
Cc: vim org
Subject: Re: Tab complete filenames

On Tue, 6 Jun 2006, Max Dyckhoff wrote:

I am wondering if there is any way to get the tab completion of a
command to open a new file to complete any file in the path, not just
those in the current working directory. Basically, my path variable is:

path=.,c:\...\source,c:\...\source**

and I would like to be able to enter

:sf behavior_fi<tab><enter>

For it to open the file ...\source\ai\behaviour\behavior_fight.inl,
which it won't currently do. Any bright ideas?
[snip]

This was recently discussed on the list (I don't remember when). You can
place something like this in your vimrc:

   command! -nargs=? -complete=custom,PathFileComplete -bang -bar Sfind sfind<bang> 
<args>
   function! PathFileComplete(ArgLead, CmdLine, CursorPos)
     return substitute(globpath(&path, a:ArgLead."*"), "[^\n]\\+/", "",
"g")
   endfunction

Instead of using :sfind, use :Sfind (note the capital S). However, if
you have duplicate filenames in the path, you may want to use this
instead so you can specify a count for which duplicate found file is
opened:

   command! -nargs=? -complete=custom,PathFileComplete -count=1 -bang -bar Find 
<count>find<bang> <args>

So if there are 2 "behavior_fight.inl" files, :2Find behavior_fight.inl
will open the second one.

Kudos to Hari for the completion function!

HTH :)
--
Gerald

Thanks Gerald. I'm not familiar with globpath although a quick :help
taught me all I needed to know. A quick poke shows that it works rather
nicely; if a bit slowly, we have around 100 first level directories in
source, each of which has 20-100 files and sub directories (although
thankfully not many more than two levels of directories!).

My only question is whether globpath has some unwritten limit on the
number of files it can deal with. If I try to do :sf a<tab> then it is
likely to turn up a couple of hundred files.

AFAIK, there's no way to limit the path search. Also, I'm not sure if
globpath() will break with an extremely large number of files.

You're working on a large project, so I would advise caution when doing
tab completion. If you happened to be waiting on an accidental (slow)
completion like a<Tab>, then hit Ctrl-c to stop it.

And yes I respect that the answer might just be "don't do that, you
fool".

:)

I use tab completion for just about anything. Yes, that includes things
like a<Tab>. Someday, I hope to find that elusive tab completion that
would complete the entire job currently being worked on ;)

Also, is there any (easy) way to have user defined commands starting
with a lowercase letter? In all honesty I will probably not make use of
Sfind and instead put up with typing the full name myself, just because
I won't be able to train myself out of typing :sf.

Nope, no (easy) way. That's just how Vim was implemented. You could hack
the source code though.

If you don't have any other commands besides :Sfind beginning with "Sf",
you can just do :Sf instead of the full :Sfind. It's an extra Shift
keystroke. To see a list of defined commands, do

  :com

An (untested) alternative I just thought of is to do something like this
(Vim 7):

  cnoremap <silent><expr>: getcmdpos()==1?toupper(nr2char(getchar())):':'

Then when you want the first character to be uppercase, hit ':' twice
instead when you want to get into the :cmdline. For example, '::sf' will
produce ':Sf'.

HTH :)
--
Gerald

Reply via email to