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

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

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

M Kelly
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

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/abac3a43-a328-402d-bdca-635f430d665c%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

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

John Little-4
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

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/9e8284d8-136e-4dff-8154-d9b24b693c94%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

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

M Kelly
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

On Thursday, February 6, 2020 at 6:35:57 PM UTC-5, John Little wrote:
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

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/b5908d0a-4191-4347-a55c-b95266e78945%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

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

John Little-4

> 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

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/2a046725-c7f3-4d9d-b571-0cd9242984ae%40googlegroups.com.