Remove carriage returns ^M

To remove carriage returns (\r), often seen as ^M at the end of lines in files from Windows,

:%s/^M//g
  • How to type: ^M:
    You cannot simply type a caret and an “^M”.
    You must enter the literal control character by pressing:
    • Ctrl + V then Ctrl + M
    • Ctrl + V then return
  • Note for Windows users: If you are using vi on Windows, use Ctrl + Q then Ctrl + M.

Modern Vim alternative: 
In many versions of Vim, you can simply use \r

<code>:%s/\r//g

Clear entire file:

Shift+ H then Shift + V then Shift G then dd

Leave a Reply