René Nyffenegger
René Nyffenegger

Reputation: 40553

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)

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

endfu

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?

Upvotes: 0

Views: 125

Answers (3)

Luc Hermitte
Luc Hermitte

Reputation: 32966

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

Upvotes: 1

Maxim Sloyko
Maxim Sloyko

Reputation: 15896

Instead of

execute "w! " + a:name

do this:

exec "save! " . a:name

Worked for me

Upvotes: 1

René Nyffenegger
René Nyffenegger

Reputation: 40553

Never mind... I found it.

I should have written

execute "w! " . a:name

instead of

execute "w! " + a:name

Upvotes: 2

Related Questions