tags:

views:

155

answers:

2

Hi,

Let's say I have the following file tree:

/
  include/
    library/
      a.hpp
      b.hpp
  src/
    a.cpp
    b.cpp

And the following /src/a.cpp file:

#include "a.hpp"
#include "b.hpp"

I always open Vim at the root directory. So when I want to load a.hpp I do :

:tabnew include/library/a.hpp

or:

:tabnew **/a.hpp

I'd like to map <F4> to open the file under the cursor in a new tab, using a recursive search.

I tried the following mapping command:

:map <F4> :tabnew **/expand("<cfile>")<cr>

But obviously, this can't work, as it tries to open the file "/expand(" instead.

Any clue on how I could do that ?

Thanks.

+1  A: 

Have a look here.

Bozhidar Batsov
I've read Vim help and found this page already, but can't see how to write my custom mapping. Could you be more accurate ?
ereOn
+2  A: 

:help gf gives a hint at how to accomplish this.

:nmap <F4> :tabe **/<cfile><cr>

It seems <cfile> is automatically expanded in mappings.

Randy Morris
This can't work (and doesn't work) because `<cfile>` is evaluated by the `:execute` command and not when the mapping is used.
ereOn
Bah, you are right. It works fine when you define the map while the cursor is on your test case ;)
Randy Morris
I just found it out and then saw your edit ! Thanks anyway ;) I'll accept that.
ereOn
I've cleaned up this answer to remove the incorrect info for future searches.
Randy Morris