How do I append text to a file with vim script?

vim

Solution

According to `:help writefile()`:

When {flags} contains "a" then append mode is used, lines are
appended to the file: >
    :call writefile(["foo"], "event.log", "a")
    :call writefile(["bar"], "event.log", "a")

Problem

I want a function to append text to a file (not a buffer) in vim. As far as I can see, there is no `appendfile()`. But the desired functionality can be emulated with `readfile()` and `writefile()`: ``` fu! TQ84_log (S) let l:f = readfile('my.log') call add(l:f, a:S) call writefile(l:f, 'my.log') endfu ``` Since `my.log` can grow quite large, I'd rather not read and write the entire file when I want to add a line. So, I came up with another "solution": ``` fu! TQ84_log (S) silent execute "!echo " . a:S . ">> my.log" endfu ``` This works (on windows, that is) as expected. Yet, when I invoke `TQ84_log()`, that `cmd.exe` window pops up for a short time. This is a bit distracting. Is there a better solution for my problem?

Original source