What's the problem with 'execute “w! ” + a:name' in a vim script/function?

I have the following Vim-function:

fu! Create_file_and_write_to_it(name, text)

  execute "normal i" . a:text
  execute "w! " + a:name


I call this function like so:

:call Create_file_and_write_to_it('c:\temp\foo.txt', "here is some text")

While it creates a new buffer and writes the desired text (ie: here is some text) into the buffer, it doesn't write the buffer to a file named c:\temp\foo.txt or any other name I could see. Neither do I get an error message.

Is there a reason for this behaviour, or am I doing something wrong, and how would I go about getting the desired functionality?


Never mind... I found it.

I should have written

execute "w! " . a:name

instead of

execute "w! " + a:name

Instead of

execute "w! " + a:name

do this:

exec "save! " . a:name

Worked for me

Unless a:text has some special sequences/mappings to be triggered, you could also have used put=a:text

Need Your Help

Difference between ISODates in each document?

python mongodb datetime pymongo timedelta

Is it possible to calculate the difference between two dates in a document server-side, and query on it? (Like SQL's DATEDIFF function)

CoreData - Modeling mapping table

ios cocoa-touch core-data iphonecoredatarecipes

Im planning on using CoreData to model a “News” iOS app. This’ll have many sections (read a UITabBarItem) which will contain many articles (in a UITableView). The articles might appear in other sec...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.