Tip #379: 1,$ s/^M//g gets rid of control-Ms (windows carriage returns)
tip karma |
Rating 162/74, Viewed by 6560
|
Read and edit this tip on the
Vim tip wiki.
The wiki may have a more recent version of this tip.
created: |
|
November 27, 2002 23:11 |
|
complexity: |
|
basic |
author: |
|
bothered by control M |
|
as of Vim: |
|
5.7 |
This has got to be in the tips somewhere else, but darned if I could find it. I had been bothered by the pesky ^M characters that appeared at the end of lines in files that were generated in MS Windows -- particulary appserver log files for me. My new best friend showed me this regex substitution that gets rid of them:
:1,$ s/^M//g
Note - If I don't have this in the command buffer, I usually wind up copying and pasting the ^M into the regex if I'm in windows, since I'm not sure how to type it from the keyboard (shift 6 followed by capital M doesn't work). In unix, I can ususally type ctrl-V followed by Enter to get the ^M.
<< Auto insert Java class template when editing a new Java file |
Using gvim as frontend for dbx >>
Additional Notes
[email protected],
November 27, 2002 23:46
|
I think a proper way to do this is :
:set fileformat=unix
or in short terms:
:set ff=unix
Of course, you can also use it from unix to ms-dos fileformat :
:set ff=dos
And even to mac format :
:set ff=mac
Have a look at :help fileformat
By the way, ":1,$s/..." can be shortened in ":%s/..."
|
[email protected],
November 28, 2002 0:07
|
The Vim way is
:%s/\r//g
|
xburgerNOSPAMhout AT freeler DOT nl,
November 28, 2002 3:14
|
You can also add a modeline to the file; this way vim will keep it in the desired file format no matter what.
Just add this line to the file:
/* vim: set ff=dos: */
|
[email protected],
November 28, 2002 13:15
|
I did not see using $ as part of the pattern so I'll mention that...
I like to specify a little more exact pattern so I get rid of only what I think I am getting rid of, so I append the end of line marker to the end of the pattern.
I.e.
:%s/^M$//
I.e. only ^Ms at EOL.
:h /$
|
[email protected],
November 28, 2002 13:21
|
Ways to get ^M:
\r
<c-v>013
<c-v><c-m>
<c-v><enter> (at least on this win2kbox)
I believe as Zzapper mentioned, \r is the best for a :s pattern; though I often do one of the <c-v> varients :)
|
[email protected],
November 29, 2002 11:10
|
While tweaking the fileformat may work, it should be noted that Vim typically figures out the fileformat for you automatically. A file with \r\n linefeeds only becomes dos and a file with \n only becomes unix (\r only becomes mac). However, when a file contains BOTH \r\n and \n linefeeds, Vim declares it as a UNIX file and displays the ^M (\r) characters therein in the normal fashion as they are just another character inside a Unix file.
|
[email protected],
November 29, 2002 11:12
|
As an addendum, these mixed-eol files come about from stuff edited in different editors: some editors only save the lines actually changed so convert only part of the file to DOS-style line endings, for example. This also happens with files generated by user-written programs if a consistent mechanism for line termination isn't used.
|
[email protected],
December 3, 2002 8:15
|
on my windows 2000 box ctrl-v is mapped to paste, so I have to do a ctrl-q ctrl-m to get a ^M.
|
[email protected],
March 12, 2003 13:09
|
My 2-cents worth.
You are on Windows(2K), You get a file from someone that was created/edited on UNIX.
You open it with gVim and its covered with the dreaded CTL-M ^M control-M chars all over the place.
SOLUTION:
:%s/<ctl-q><ctl-m>/\r/g
WHERE:
<ctl-q> = Press Crtl and Q keys at the same time
<clt-m> = Press Crtl and M keys at the same time
Explained:
CTL-V is mapped to the Windows PASTE function, so, you can't invoke [ESC]escape sequence [CTL-V] like you do in UNIX.
In WINDOWS, the [ESC] is actived by CTL-Q.
The above command will change all the UNIX style <CR><LF> to the Windows normal behavior.
Hope this helps, it took me about 2 hours of trial and error and on-line searching to find the answer.
YOU'RE WELCOME! ;-}
|
Josh Pritt<[email protected]>,
November 29, 2004 9:11
|
If you use:
:%s/\r//g
it will get rid of the ^M's but still leaves your file unreadable.
Try using:
:%s/\r/\r/g
to replace the other platform's carriage-return characters with the current platform's.
Happy VIMing!
|
|