I am using includeexpr and substitute() in a ftplugin to remove a portion of an include string for gf functionality (in twig template files, for the curious), but I get an error instead.

Twig includes look like this:

{% include "@mynamespace/footer/footer.twig" %}

I already have my path setup to include the twig template directory, and I want to remove the @mynamespace/ piece to properly map to the filesystem.

I used this expression, adapted from the helpdocs example for Java.

setlocal includeexpr=substitute(v:fname,'@mynamespace/','','')

However, when I try to gf an include, I get this error:

E447: Can't find file "mynamespace/footer/footer.twig" in path

I would have expected substitute() to remove the @mynamespace/ piece. I looked at the documentation, and couldn't see anything I was doing wrong. I tried messing around with different invocations, using \ to escape the @ or /, but always got the same error.

Any help would be greatly appreciated. Thanks in advance.


Vim is separating out the @ when extracting the filename. Why? Per :h gf...

[Vim] uses the 'isfname' option to find out which characters are supposed to be in a file name.

You can demonstrate this another way by putting your cursor on the filename and running

:echo expand('<cfile>')

The response is "mynamespace/footer/footer.twig". <cfile> is determined the same way as for v:fname. That means you can proceed with your substitution with the same assumption....

:setlocal includeexpr=substitute(v:fname,'mynamespace/','','')

...and gf will look for footer/footer.twig.

  • Wow, I never would have figured that out, but such a simple solution. Thanks so much! – bronzehedwick May 19 '20 at 19:51
  • You bet. Happy to help. Cheers. – B Layer May 19 '20 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.