0

I have two "identical" files that I compare in diff, and it shows that every line is different, after looking at the file I found that one of them has ^M at the end of every line. How do I remove this? I tried this sed command:

sed -i '' -E 's/^M//g'

this tr command: (which even if it worked I wouldn't want since I want to do in place editing)

tr -d '^M'

and this perl command:

perl -pi -e 's/^M//g'

But none of those changed the contents.

  • 1
    Make sure you are using ctrl-V, ctrl-M, not simply typing ^M. – David Betz Jan 27 '16 at 23:23
  • @DavidBetz typing literally ctrl + M just makes a new line in my terminal. – DisplayName Jan 28 '16 at 0:00
0

Some time ago I've found these functions in kenrob's .vimrc in his dotfiles repository:

" Removes the ^M character from the end of every line
function! RemoveM()
    :%s/^M$//ge
endfunction

" Replaces the ^M character with a carraige return native to the system
function! ReplaceM()
    :%s/^M/\r/ge
endfunction

So you can easily adopt these functions and use Vim to deal with those ^M characters.

Not the answer you're looking for? Browse other questions tagged or ask your own question.