tags:

views:

56

answers:

1

I'm trying to write a simple Vim function that takes the name of a file as an argument and reads the contents of that file into the current document (related to this question).

Here's my first stab at it:

fun! Tpl(tplfile)
  r c:\tpl\a:tplfile
endfun

That just gives me the following error:

E484: Can't open file c:\tpl\a:tplfile

How do I make the function actually use the value of the tplfile argument?

+4  A: 

Replace the line with:

exe 'r c:\tpl\' . a:tplfile

The a:tplfile is a string variable, so to include it in a command, you have to combine the whole lot into one string (with the '.' operator) and then use exe to run the command

:help exe

On a related note (and a shameless plug), if you're trying to add templates (as implied by the post you linked to), my file templates plugin has a command AddTemplate to add a template from your vimfiles/templates directory at the current cursor location. However, the documentation is currently rather poor, so if you decide to use it and have any difficulties, feel free to drop me an email at the address on my website.

Al
Perfect, thanks!
Mark Biek