join() a list with a newline char for an echo

25 views
Skip to first unread message

M Kelly

unread,
Feb 6, 2020, 1:53:02 PM2/6/20
to vim_use
Hi,

If I have a list [ one, two, three ] is there a way to turn it into a single string with newlines after each element ?
Such that an echo mylist would show it as mulitple lines, ie:
one
two
three

I have tried join( mylist, "\n") but it uses ^@ and "\r" uses ^M

thx for everything vim,
-m

John Little

unread,
Feb 6, 2020, 6:35:57 PM2/6/20
to vim_use
On Friday, February 7, 2020 at 7:53:02 AM UTC+13, M Kelly wrote:

> I have tried join( mylist, "\n") but it uses ^@ and "\r" uses ^M

works fine for me, vim, gvim, and vim --clean.

:let l = ["one", "two", "three"]
:echo join(l, "\n")
one
two
three
Press ENTER or type command to continue

Maybe you're on MS Windows?  Used single quotes?  Have some settings causing trouble? (try with vim --clean).

HTH, John Little

M Kelly

unread,
Feb 6, 2020, 7:12:57 PM2/6/20
to vim_use
Hi,

I agree that works - but my list is from a ch_read() and has ^@ (NL) at the end of each element
and I just cannot seem to get this work.  The join("\n") just puts the ^@ back everywhere.

thx tho,
-m

John Little

unread,
Feb 6, 2020, 11:41:43 PM2/6/20
to vim_use

> I agree that works - but my list is from a ch_read() and has ^@ (NL) at the end of each element
and I just cannot seem to get this work. The join("\n") just puts the ^@ back everywhere.

I don't know what you've got there.  Presumably, you are familiar with the way vim handles NUL
characters (they are stored in memory as newlines, 0x0a, and show as ^@). 

Have you tried chopping off the ends of the elements? Say,

:let my_list = ["alice", "bob", "carol"]
:call map(my_list, {k,v -> v[0:-2]})
:echo my_list
['alic', 'bo', 'caro']

HTH, John Little
Reply all
Reply to author
Forward
0 new messages