Convert ^M (Windows) line breaks to normal line breaks
Asked Answered
M

38

522

Vim shows ^M on every line ending.

How do I replace this with a normal line break in a file opened in Vim?

Marji answered 1/5, 2009 at 12:44 Comment(5)
possible duplicate of Convert DOS line endings to Linux line endings in vimIceland
Please change the solution to the fileformat change because that's the most clean solution.Offhand
I know this question is about vim, but some other Google searches also land in this thread. So, if you have Eclipse installed on your system you could convert the line delimiter for either a single file or a complete folder tree of files with a few clicks: https://mcmap.net/q/66101/-automatic-eol-conversion-in-eclipsePomposity
I like how you call normal line breaks "normal" -- in contrast to those ^M line breaks :-)Sandrocottus
@Sandrocottus I guess all those fancy standard Internet protocols are just abnormal then?Fermanagh
C
156

This is the only thing that worked for me:

:e ++ff=dos

Found it at: http://vim.wikia.com/wiki/File_format

Chattanooga answered 2/4, 2014 at 15:30 Comment(4)
Worked on windows7 Vim7.4Hazardous
:e ++ff=dos followed by :set ff=unix will convert the endings to a sane format.Apostatize
Solution's response didn't work for me, only @MateenUlhaq commands helped me.Napolitano
Hmm, I suspect a whole host of Internet protocols must be considered "insane", if only LF line endings are deemed "sane" and it is supposed to be some sort of dichotomy?! ... personally I find the "Unix" way no more legitimate than the "DOS" (and Internet protocol) way.Fermanagh
G
617

Command

:%s/<Ctrl-V><Ctrl-M>/\r/g

Where <Ctrl-V><Ctrl-M> means type Ctrl+V then Ctrl+M.

Explanation

:%s

substitute, % = all lines

<Ctrl-V><Ctrl-M>

^M characters (the Ctrl-V is a Vim way of writing the Ctrl ^ character and Ctrl-M writes the M after the regular expression, resulting to ^M special character)

/\r/

with new line (\r)

g

And do it globally (not just the first occurrence on the line).

Garceau answered 1/5, 2009 at 12:48 Comment(23)
@luckytaxi - what does it do for you?Garceau
it removed the ^M characters but doesn't insert the carriage return.Thornhill
This doesn't work, at least on Ubuntu. I get the error E486: Pattern not found: <Ctrl-V><Ctrl-M>Outlive
I actually typed the string '<Ctrl-V><Ctrl-M>'. Did you mean type <kbd>Ctrl</kbd>+<kbd>V</kbd>, then <kbd>Ctrl</kbd>+<kbd>M</kbd>?Outlive
@Outlive - You need to push the key chords Ctrl-V/Ctrl-M, not enter that as verbatim text.Garceau
This was the correct solution when I had this problem. It's not quite as simple as that though. This works if the file has CRLF endings, while the :%s/^V^M/^V^M/g approach works if converting from one to the other. The settings when the file is opened and when it is updated by Git in the background can affect this, because Vim may guess the mode when opening the file, but will read in everything if the file changes while open.Bricklayer
fail, now I have a one-line fileFlare
Works for me in ArchLinux :-), Vim 7.3Rhadamanthus
This is definitely the right answer for the question, if you have a ^M followed by a new line, you want to keep the newline but remove the ^M. Doing the other substitution below double-spaces your file.Poohpooh
If you're still missing a carriage return (i.e. the ^M has been removed but everything is wrapped on a single line), do: :%s/<Ctrl-V><Ctrl-M>/\r/g (where \r replaces the ^M with a newline).Guarino
If you are running Vim on Windows, then you'll need to use <Ctrl-Q> instead of <Ctrl-V>.Silden
For me, using MacVim @netpoetica's solution way down below was the only one here that worked.Slover
The OP said replace, not remove.Numismatist
Make sure you actually press Ctrl-V and Ctrl-M as opposed to just copy and pasting the string.Threesome
I always forget how to do this, and always end up back on this question when I go looking. Thanks for the 18th time.Territorialism
This answer should be my homepageComfortable
When dealing with a text file that was mangled by Windows, I only needed to replace the ^M with "" (the empty string).Jacquijacquie
Further to @Silden : GVim on windows could not find ^M but it found ^M$ !?!? example: :%s/^M$//g worked for meBrokenhearted
if you only want to remove the ^M: :%s/<Ctrl-V><Ctrl-M>/g. Also I find out that /g is not necessary(gvim, windows)Natica
Note that this does not work if the file is loaded as a dos file.Anaximenes
only this worked for me. If you don't want new line, don't add \r. :%s/<Ctrl-V><Ctrl-M>//gKirakiran
This answer added extra line breaks to my file ... the accepted answer worked fine. :e ++ff=dosPhilous
On a Mac with iTerm2 one really has to use the 'control' key, not the 'command'.Monad
I
427

On Linux and Mac OS, the following works,

:%s/^V^M/^V^M/g

where ^V^M means type Ctrl+V, then Ctrl+M.

Note: on Windows you probably want to use ^Q instead of ^V, since by default ^V is mapped to paste text.

Isbell answered 1/5, 2009 at 12:47 Comment(26)
Why did this get a downvote? It works, even when your file is mashed onto one line because it's got the wrong line end.Isbell
It really works and is exactly what you need when all your text is on one line.Telethermometer
Shouldn't %s/^V^M/^V^M/g be %s/^V^M//g ?Tchad
No. My way replaces whatever is the line end in the file with the correct line end.Isbell
This doesn't work, at least on Ubuntu. I get the error E486: Pattern not found: ^V^M.Outlive
@jumpnett, you didn't type it right. colon, then percent, then s, then slash, then control v, then control m, then slash, then control v, then control m, then slash, then g, then return.Isbell
I didn't type it right. It does replace the ^M's, but it also adds an extra newline. What I want is :%s/^V^M//g, but that's neither here no there, since I didn't ask the question.Outlive
This is the correct solution if your file appears to have no newlines at all (i.e. it is one long line). If your file appears to have line endings and the ^M character, then this will add an extra line break between every line.Bricklayer
@Leo, the question specifically asks to convert ^M to a linebreak, which is what my answer does (unlike the accepted answer, which removes ^M).Isbell
@PaulTomblin yes, you're quite right. Apologies. The problem I had was subtly different - ^M appearing in git diff but not in Vim because of conversion when the file is openend. I had to use Jonathan Leffler's answer in combination with both yours and LeopardSkinPillBoxHat's to wrangle the various files. Perhaps I should have asked a new question, but last time I did so for a slight variation I was told it was a dupe!Bricklayer
@PaulTomblin -- your answer works for one long line with ^M's in it. If there are lots of lines, each ending with a ^M, then the accepted answer works since those are really carriage-return/line-feed pairs.Epistasis
@LavaSlider, my experience is that it works for any file with any number of lines.Isbell
But if the line ends with "<CR><LF>", ":%s/^V^M/^V^M/g" will change it to "<LF><LF>", inserting a blank line. Maybe it's different versions of vim but I typed in: hello^V^Mhow^V^Mare^V^Myou^V^M[return]hello^V^M[return]how^V^M[return]are^V^M[return]you^V^M[esc] (where [return] is hitting the return/enter key and [esc] is the escape key). Before executing the command the file has 5 lines, the first with "hello^Mhow^Mare^Myou^M", the next four with one word followed by "^M". After ":%s/^V^M/^V^M/g" there are 13 lines, 1 word per line except blank lines at lines 5,7,9,11 & 13 (on a mac). Try it.Epistasis
That's not my experience with real files moved from Windows to Linux or Windows to Mac.Isbell
This one works for me, files from windows, edited on osx, the question is, why this works, it's a bit odd isn't it, replacing with the same character sequence.Skidmore
This is for a different problem when your file has NO new lines in it, which I'll admit is more common. FYI ^M can be matched by \rPoohpooh
This is a deeply mysterious command, but it works for me on a mac.Raddled
deeply mysterious indeed and works for me on every server I tried it on from redhat, to ubuntu, to os x.Wilscam
Yep! Works on cygwin too. I had a file created on Ubuntu/scp'ed on to Windows/opened with vim under Cygwin. It had one long line with 1660 ^M's in it. Running this command gave: 1660 substitutions on 1 line and the file now has 1661 lines. Yay!Bloomer
This is the correct answer and not the one that has been accepted.Isologous
I found the equivalent %s/\r/\r/g worked well on MacVim for a file showing only ^M at end of lines (one long line in MacVim terms). Clearly \r means different things in the pattern vs replacement parts of this command.Svend
This works like charm! on OSX+vim. :) Thank you so much. Highly annoying to have ^M in file.Imide
Your tip on using ^Q on Windows should get an additional +1000 points! Thanks.Mcneill
This didn't worked for me, but RichVel comment %s/\r/\r/g worked for me. Would love to see an answer who explain why this answer didn't worked, but \r does (and another solutions people put). There are a lot of different solutions to this problem, and don't understand what is happening resolves to try everything until something works.Aquiline
@Tchad yes, replacing it with nothing worked for me.Blunge
The CTRL-Q tip fixed my muscle memory in WSL. I could not figure out why it wasn't working as it always does but it makes sense after you explained it. :)Ablaze
I
194

Within vim, look at the file format — DOS or Unix:

:set filetype=unix

:set fileformat=unix

The file will be written back without carriage return (CR, ^M) characters.

Inductor answered 1/5, 2009 at 12:57 Comment(7)
use :set fileformat=unix For most configurations filetype only changes the syntax type being used.Barbate
This fixed the bug I was having... Vim started thinking my UNIX formatted file was windows and newly changed lines were showing ^M in the git diff.Programmer
Must be system dependent. Today, this one worked. The set command is done within vim, btw.Whish
Small gotcha I found: if Git updates a file in the background (due to a checkout) and you have set this to something other than what Vim guessed when opening the file, Vim does not appear to reconvert when reloading the buffer. You'll end up with a bunch of extraneous characters. You can then use one of the %s/^V^M/.../g approaches above. Which one depends on exactly what formats you're converting to and from.Bricklayer
great!because the fileformat is dos, set it to be unix, and it's okConjectural
This also works on vi BTW, and you can do set ff=unix as a little shortcut in the same way ft is a shortcut for filetypeLilly
I like this solution because I don't have to do what I used to do (a) leave vi, (b) vi -b <filename>, (c) :s/^M//. Thanks.Madera
C
156

This is the only thing that worked for me:

:e ++ff=dos

Found it at: http://vim.wikia.com/wiki/File_format

Chattanooga answered 2/4, 2014 at 15:30 Comment(4)
Worked on windows7 Vim7.4Hazardous
:e ++ff=dos followed by :set ff=unix will convert the endings to a sane format.Apostatize
Solution's response didn't work for me, only @MateenUlhaq commands helped me.Napolitano
Hmm, I suspect a whole host of Internet protocols must be considered "insane", if only LF line endings are deemed "sane" and it is supposed to be some sort of dichotomy?! ... personally I find the "Unix" way no more legitimate than the "DOS" (and Internet protocol) way.Fermanagh
A
126

A file I had created with BBEdit seen in MacVim was displaying a bunch of ^M line returns instead of regular ones. The following string replace solved the issue:

:%s/\r/\r/g

It's interesting because I'm replacing line breaks with the same character, but I suppose Vim just needs to get a fresh \r to display correctly. I'd be interested to know the underlying mechanics of why this works.

Ancon answered 27/9, 2012 at 4:46 Comment(6)
There's a tendency for search functions to accept broader rules for recognizing end-of-line sequences. But \r has a specific meaning when it's being written as data.Whish
This worked for me in Windows gVim to sort out a file that had no line breaks, just lots of ^M instead.Prosenchyma
From all the solutions offered on this page, this was the only pattern that worked for me in removing ^M from a csv file. Using MacVim.Disentwine
+1 Worked for me too while SSHing to Ubuntu Trusty from Windows using MobatermConcertino
So, this seemed to replace all of the \r characters with \n despite specifying \r as the replacement. Not complaining, that's exactly what I wanted. Just weird.Sanderson
This answer cleared up for me why the search and replace patterns are the same. I would think that has no effect, but they are interpreted differently in each context - I haven't seen that mentioned yet. Short answer seems to be that "\n was already taken".Longwise
R
35

First, use :set ff? to figure out the file format your file is.

I guess it could be unix, then the problem is your file was created with fileformat=dos adding "^M^J" to the line end but read with flieformat=unix only removing the "^J" from the line end, leaving the "^M" there.

Just input :e ++ff=dos in Vim command line to change your file's format from unix to dos. It should solve the problem. If not, :%s/\r//g should help you out.

Restivo answered 7/3, 2012 at 4:1 Comment(1)
I had to do both. :e ++ff=dos then :set ff=unix and :wExpiation
K
29

in order to get the ^M character to match I had to visually select it and then use the OS copy to clipboard command to retrieve it. You can test it by doing a search for the character before trying the replace command.

/^M

should select the first bad line

:%s/^M/\r/g

will replace all the errant ^M with carriage returns.

This is as functions in MacVim, which is based on gvim 7.

EDIT:

Having this problem again on my Windows 10 machine, which has Ubuntu for Windows, and I think this is causing fileformat issues for vim. In this case changing the ff to unix, mac, or dos did nothing other than to change the ^M to ^J and back again.

The solution in this case:

:%s/\r$/ /g
:%s/ $//g

The reason I went this route is because I wanted to ensure I was being non-destructive with my file. I could have :%s/\r$//g but that would have deleted the carriage returns right out, and could have had unexpected results. Instead we convert the singular CR character, here a ^M character, into a space, and then remove all spaces at the end of lines (which for me is a desirable result regardless)

Sorry for reviving an old question that has long since been answered, but there seemed to be some confusion afoot and I thought I'd help clear some of that up since this is coming up high in google searches.

Kimon answered 25/4, 2011 at 19:36 Comment(3)
pressing ctrl-v ctrl-m may work to insert the character, as well, fwiw. but the \r is what inserts the proper carriage return.Kimon
This is the RIGHT answer. As said above, use <ctrl-v><ctrl-m> to get the literal ^M inserted in the command.Alcoholometer
I agree, though I am certain that if that had worked for me at the time of original posting then I wouldn't have posted. ;)Kimon
T
24

None of these worked for me, so I tried this, which worked:

type :%s/

press CTRL-VCTRL-M

type //g

press Enter

So the overall command in Vim shoud look like :%s/^M//g

What this does: :%s (find and replace) /^M/ (that symbol) / (with no chars) g (globally).

Triplet answered 10/5, 2014 at 18:51 Comment(2)
Bang on!! Works on Mac.Plumate
Nice Answer. Just kills the ^M though, doesn't replace them with newlines (vim linux, ff=dos). YMMV depending on which of [mac,windows,linux] you have and what fileformat is currently set to ( see with :set ff?). Were you on a mac?Distichous
C
18

^M is retrieved by Ctrl+V and M, so do

s/^M//g
Caudex answered 1/5, 2009 at 12:47 Comment(0)
W
17

Without needing to use Ctrl: :%s/\r$//

Watershed answered 6/10, 2015 at 19:46 Comment(5)
This is the way I do it as well. I just never remember the commandDiphosgene
This is the recommended way to remove these endings.Kimon
This worked perfectly for my case as well.Ronen
Vim 8.1 gives me E486: Pattern not found: \r$ for that.Distichous
This is the best solution, works with any OS and without needing the buffer to be associated with a file.Sicilia
O
15

Simple thing that worked for me

dos2unix   filename
Occlusive answered 7/11, 2014 at 7:30 Comment(2)
That just removed the ^M characters, rather than replace them with newlines.Hertzfeld
this failed to do both for me. :%s/^M/\r/g (where the ^M was CTRL-'VM') worked.Distichous
K
14

I did this with sed:

sed -i -e 's/\r/\n/g' filename

Kansu answered 3/1, 2015 at 4:8 Comment(3)
Are you sure this works? On OSX: sed -e 's/\r/\n/g' <<< 'over and over' -> oven and ovenAuscultation
Yes it works (I use it a lot), but on OSX if you do not have the GNU version of sed installed then it may not work. The version of sed that Apple ships by default is much different (sadly) than the GNU version that ships on all Linux distros. Check out the answer to this question for more help: https://mcmap.net/q/64805/-how-to-use-gnu-sed-on-mac-os-10-10-39-brew-install-default-names-39-no-longer-supported/2062384Kansu
Thanks @Freedom_Ben. So it sounds like this works for GNU sed but not BSD sed then (as found on OSX). Anyone fixing linebreaks is probably working with a mix of operating systems, so it's useful to be clear about when the answer will work.Auscultation
C
11

What about just: :%s/\r//g That totally worked for me.

What this does is just to clean the end of line of all lines, it removes the ^M and that's it.

Cassiecassil answered 11/7, 2014 at 12:47 Comment(0)
P
10

There are many other answers to this question, but still, the following works best for me, as I needed a command line solution:

vim -u NONE -c 'e ++ff=dos' -c 'w ++ff=unix' -c q myfile

Explanation:

  • Without loading any .vimrc files, open myfile
  • Run :e ++ff=dos to force a reload of the entire file as dos line endings.
  • Run :w ++ff=unix to write the file using unix line endings
  • Quit vim
Perpetuity answered 22/4, 2017 at 15:4 Comment(1)
thank you! this fixed my issue and found it useful to use in scriptsAberdeen
R
8

Ctrl+M minimizes my window, but Ctrl+Enter actually inserts a ^M character. I also had to be sure not to lift off the Ctrl key between presses.

So the solution for me was:

:%s/<Ctrl-V><Ctrl-Enter>/\r/g

Where <Ctrl-V><Ctrl-Enter> means to press and hold Ctrl, press and release V, press and release Enter, and then release Ctrl.

If you are working on a Windows-generated file

The above solution will add an additional line between existing lines, because there is already an invisible \r after the ^M.

To prevent this, you want to delete the ^M characters without replacing them.

:%s/<Ctrl-V><Ctrl-Enter>//g

Where % means "in this buffer," s means "substitute," / means "(find) the following pattern," <Ctrl-V><Ctrl-Enter> refers to the keys to press to get the ^M character (see above), // means "with nothing" (or, "with the pattern between these two slashes, which is empty"), and g is a flag meaning "globally," as opposed to the first occurrence in a line.

Robomb answered 4/4, 2017 at 15:55 Comment(0)
L
7

This worked for me:

  1. Set file format to unix (\n line ending)
  2. save the file

So in vim:

:set ff=unix
:w
Lithea answered 30/11, 2016 at 20:30 Comment(1)
This one worked for me, while the accepted answer didn't.Cypher
M
7

In my case,

Nothing above worked, I had a CSV file copied to Linux machine from my mac and I used all the above commands but nothing helped but the below one

tr "\015" "\n" < inputfile > outputfile

I had a file in which ^M characters were sandwitched between lines something like below

Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKT6TG,TRO_WBFB_500,Trico,CARS,Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKTG0A,TRO_WB_T500,Trico,
Musicology answered 4/12, 2016 at 2:53 Comment(0)
E
6

Alternatively, there are open-source utilities called dos2unix and unix2dos available that do this very thing. On a linux system they are probably installed by default; for a windows system you can download them from http://www.bastet.com/ amongst others.

Erdmann answered 1/5, 2009 at 13:10 Comment(0)
M
5
sed s/^M//g file1.txt > file2.txt

where ^M is typed by simultaneously pressing the 3 keys, ctrl + v + m

Meneau answered 4/8, 2014 at 10:7 Comment(0)
B
4

use dos2unix utility if the file was created on windows, use mac2unix utility if the file was created on mac. :)

Blate answered 25/9, 2012 at 12:29 Comment(2)
never heard of mac2unix before, would have upvote again if I could!Detergency
Huray! this is the way to go!Stronski
R
4

Use one of these commands:

:%s/\r//g

Or

:%s/\r\(\n\)/\1/g
Reenter answered 10/6, 2015 at 10:45 Comment(1)
First command simply removes all \r, if you save it like this your file will be destroyed.Beelzebub
S
3

In command mode in VIM:

:e ++ff=dos | setl ff=unix | up

e ++ff=dos - force open file in dos format.

setl ff=unix - convert file to unix format.

up - save file only when has been modified.

Systematist answered 3/12, 2020 at 17:52 Comment(0)
A
2

To save keystrokes, you can avoid typing Ctrl+VCtrl+M by placing this in a mapping. Just open a file containing a ^M character, yank it, and paste it into a line like this in your .vimrc:

nnoremap <Leader>d :%s/^M//g<CR>
Acumen answered 6/12, 2013 at 9:18 Comment(0)
S
2

This worked for me:

:% s/\r\n/\r
Sibyls answered 18/4, 2014 at 1:9 Comment(0)
C
2

To use sed on MacOS, do this:

sed -i.bak $'s/\r//' <filename>

Explanation: The $'STRING' syntax here pertains to the bash shell. Macs don't treat \r as special character. By quoting the command string in $'' you're telling the shell to replace \r with the actual \r character specified in the ANSI-C standard.

Clavius answered 22/1, 2019 at 15:4 Comment(0)
C
1

None of these suggestions were working for me having managed to get a load of ^M line breaks while working with both vim and eclipse. I suspect that I encountered an outside case but in case it helps anyone I did.

:%s/.$//g

And it sorted out my problem

Crossfade answered 3/4, 2014 at 23:16 Comment(1)
This will remove ANY character at the end of a line. If you have a line that is not, for some reason, terminated by ^M, then you will remove that character also, and would be unexpected. This should not be the case, but in the event that it is, and you don't know this, then you could sully your file in a very hard to identify way.Kimon
M
1
:g/^M/s// /g

If you type ^M using Shift+6 Caps+M it won't accept.

You need to type ctrl+v ctrl+m.

Mudcat answered 30/4, 2015 at 5:42 Comment(0)
D
1

^M gives unwanted line breaks. To handle this we can use the sed command as follows:

sed 's/\r//g'
Dickie answered 20/7, 2016 at 11:3 Comment(1)
idk why this was downvoted to -1... because it uses sed? you can do the same substitution inside vim and it should workKimon
C
1

Just removeset binary in your .vimrc!

Campus answered 26/5, 2017 at 12:37 Comment(1)
In one other thread, "set binary" was recommended to get rid of the auto insert of new line.Aubin
S
1

On Solaris:

:%s/<CTRL+V><CTRL+M>//g

that is:

:%s/^M//g

That means:

  • % = all lines,
  • s = substitute,
  • ^M = what you desire to substitute
  • // = replace with nothing
  • g = globally (not only the first occurrance)
Salba answered 2/12, 2020 at 9:20 Comment(0)
S
0

" This function preserves the list of jumps

fun! Dos2unixFunction()
let _s=@/
let l = line(".")
let c = col(".")
try
    set ff=unix
    w!
    "%s/\%x0d$//e
catch /E32:/
    echo "Sorry, the file is not saved."
endtry
let @/=_s
call cursor(l, c)
endfun
com! Dos2Unix keepjumps call Dos2unixFunction()
Strafe answered 25/4, 2011 at 23:6 Comment(1)
In this case, what's wrong with using *NIX's native dos2unix command? linux.die.net/man/1/dos2unixKimon
P
0

I've spent an afternoon struggling with \n ctrl-v 012 (both of which supply me with null). & laboured through this thread until I reached metagrapher's.

\r worked fine for me!

/),/s/),/)\r/g

turned something like this:

blacklist-extra:i386 (0.4.1, 0.4.1+nmu1), libmount1:i386 (2.20.1-5.1, 2.20.1 -5.2), libblkid1:i386 (2.20.1-5.1, 2.20.1-5.2), libapt-pkg4.12:i386 (0.9.7.4 , 0.9.7.5), nmap:i386 (6.00-0.1, 6.00-0.2), libsane-common:i386 (1.0.22-7.3,

into something like this:

26 libwv-1.2-4:i386 (1.2.9-3, automatic)
27 openjdk-6-jre-headless:i386 (6b24-1.11.4-3, automatic)
28 jed:i386 (0.99.19-2.1)

Magic. I am profoundly grateful

Pyjamas answered 21/1, 2013 at 16:28 Comment(0)
R
0

Or instead of using vim you can just fix the line breaks using this command

fromdos <filename.txt>

Hope it helps!

Ringmaster answered 17/3, 2013 at 16:39 Comment(0)
M
0

None of the above worked for me. (substitution on \r, ^M, ctrl-v-ctrl-m ) I used copy and paste to paste my text into a new file.

If you have macros that interfere, you can try :set paste before the paste operation and :set nopaste after.

Misadvise answered 28/7, 2014 at 15:59 Comment(0)
D
0

In vim, use command:

:%s/\r\n/\r/g

Where you want to search and replace:

\r\n

into

\r

and the

/g

is for global

Note that this is the same as the answer by @ContextSwitch but with the gobal flag

Dibrin answered 14/8, 2015 at 0:54 Comment(0)
C
0

Over a serial console all the vi and sed solutions didn't work for me. I had to:

cat inputfilename | tr -d '\r' > outputfilename
Candlewick answered 16/11, 2016 at 12:37 Comment(0)
S
0

If you can see the control characters but your searches are unable to find them using the other methods, and dos2unix isn't working, here's what worked for me.

Move your cursor to the ^ character and press * to start a search for it. I had to manually add a carriage return after it (i.e., after the M) in order for the search to select what I wanted. Then you can run your substitution command on your current search by just leaving the pattern term empty.

:%s//\r/g

This will replace all of the ^M characters with newlines (\r) and perform it on every line (g=global)

Semifluid answered 22/6, 2023 at 0:38 Comment(0)
C
-1

When in windows, try :%s/<C-Q><C-M>/g

Cultural answered 17/1, 2017 at 12:45 Comment(3)
Can you add some explanation as to why this works, i.e. what are you actually doing with the various parts of that command?Jaxartes
This is just the same as the top answer, except that <C-v> is replace with <C-q> because by default the former is mapped to paste text.Floorman
^M means "0d0d0a", :%s/<C-Q><C-M>/g will change "0d0d0a" to "0d0a" (0d0a means \n\r). I dont know why, but it really works in this way (0d0d0a→0d0a i.e. ^M→\n\r) in my windows7 OS.Cultural

© 2022 - 2024 — McMap. All rights reserved.