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

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny