How to make writefile() keep the new line characters

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to make writefile() keep the new line characters

Jacobo de Vera-2
Hi,

I am trying to write a list of strings to a file, I have used
writefile() successfully in the past, but this time I'm having a
problem derived from the fact that some of the items contain new line
characters.

The docs for writefile say:

> All NL characters are replaced with a NUL character.

And this seems to be the cause of my problem, since the file ends up
having ^@ where new lines should start. I don't know if the next
sentence in the docs is hinting something similar, I don't really
understand what it means:

> Inserting CR characters needs to be done before passing {list}
> to writefile().

I originally thought it meant I had to replace NL with CR before
calling writefile, but don't know exactly what the difference is
between those two.

For an example of what I am seeing, try this
:call writefile([system('ls -1 /tmp')], '/tmp/myfile.txt') and then :e
/tmp/myfile.txt
It is different than this (insert mode on an empty buffer):
<C-R>=system('ls -1 /tmp')

So my question is, is there a way to keep those new line characters in
the output file?

Thanks and regards,

--
Jacobo de Vera

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to make writefile() keep the new line characters

Jean-Rene David-2
* Jacobo de Vera [2011.11.11 17:50]:
> So my question is, is there a way to keep those
> new line characters in the output file?

Split each string which contains a newline in two
list items.

--
JR

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to make writefile() keep the new line characters

Jacobo de Vera
On Fri, Nov 11, 2011 at 23:30, Jean-Rene David <[hidden email]> wrote:
> * Jacobo de Vera [2011.11.11 17:50]:
>> So my question is, is there a way to keep those
>> new line characters in the output file?
>
> Split each string which contains a newline in two
> list items.
>

Haha, brilliant, everything I thought of doing was more complex than
this. Thanks.

I, however, still wonder what the rationale behind such behaviour of
writefile might be. and about that second sentence I quoted from the
docs:
> Inserting CR characters needs to be done before passing {list}
> to writefile().

Does it actually mean to split and insert each part in the list? Not
clear at all, or is it?


Regards,
--
Jacobo de Vera

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php