sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #877: Replace all commas with new lines

 tip karma   Rating 10/28, Viewed by 2234 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   February 17, 2005 14:18      complexity:   intermediate
author:   Roumen Semov      as of Vim:   6.0

If you wanted to turn a comma-separated list of items into a list where each item is on its own line do this in vim:
do not type the "^M" literally but hit CTRL-ENTER to get it.

 rate this tip  Life Changing Helpful Unfulfilling 

<< selecting blocks of paragraphs. | Remove trailing spaces >>

Additional Notes

Anonymous, February 17, 2005 14:21
I meant to say you get "^M" by hitting CTRL-V and then ENTER.
Anonymous, February 18, 2005 4:06
What about


using the magic of Vim
zzapper, February 18, 2005 4:59
Panchali Das Gupta, February 18, 2005 10:45
What does this mean:


I get '^@' as replacements in gvim6.3 on windows.

What is the use of '\=', some kind of eval mechanism?
I would like to thank you.
Anonymous, February 18, 2005 23:44
ctrl-v should paste right?
i am not clear about how to get "^M"

BTW, two others look better, tough I prefer \r

Thank you for sharing the tip :)

Anonymous, February 20, 2005 17:43
Type ctrl-v and then type ctrl-m to get the "^M" character. This works with control characters also.
Anonymous, February 20, 2005 23:49
You cannot use CTRL-V on Windows (it pastes...)
You should instead type CTRL-Q + ENTER to get that ^M.

Rohan Kailasam, February 21, 2005 11:34
I hate ^M, why doesn't this work in vim :%s,\n,\r,

Emacs is more consistent, I can replace *ANY* char by *ANY* other
char, no special cases - the power of elegance is this:

(query-replace "
" "
" nil nil nil)

NdrOchip@ScampbellPfamily.AbizM - NOSPAM, February 23, 2005 20:42
* ctrl-m can be had by (unix) ctrl-v + enter
  Windows users often use, wittingly or not, <mswin.vim>.
  Because of that, they need to type ctrl-q + enter.
  If <mswin.vim> was not being sourced, then the ctrl-v has not been remapped.

* Zzapper's substitute is what I would use

* For Panchali: (looks best in a fixed font)
  :             command mode,
   %            all lines
     s/,/       substitute commas,
     \=     with the following expression
           "\n" with the string which contains one newline

* for Rohan K:
  :%s,\n,\r,  means to replace all ctrl-m's with newlines, not what the
  tip was talking about.  When I have lines terminating with ctrl-m's,
  the substitute takes place as expected, leaving an extra (blank) line
  after each original line.  Emac's "elegant" solution looks pretty ugly
  to me, but I've always much preferred vi, let alone vim, over emacs.
  I prefer to leave my chords to guitar playing.
marzvix@gmail.com, February 24, 2005 16:00
I'm newbie in Vim and I was following the thread. So I was wondering how (in windows environment) reverse this tip... I've tryied to replace :%s/^M/\,/ or even better :%s/\r/\,/ or :%s/\r\n/\,/ or :%s/\n\r/\,/ and the messages was the same in all cases: E486: Pattern not found.
The solution (really a workaround) :%s/^/\;/  3J and replace extra spaces ( likely leaved by one of \r or \n)
These stuff are really interesting tools to .csv analysis on windoze environment. Any idea how to reverse that ?? TIA
Rohan K, February 25, 2005 16:02
Well you did manage a simpler substitution with vim, and claimed it does
what emacs does.

Use 'od' or 'xxd' dump to the file before and after editing and
understand my claim.

With Emacs, the hex dump will show every single '\n' got replace by single '\r',
without change in file size, which is what I am driving it.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge.net Logo