Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

^M at the end of every line in vim

Tags:

linux

vim

windows

People also ask

How do I get rid of M in vim?

How can I remove ^M characters from text files? A. ^M are nothing more than carriage return, so it is easy to use the search and replace function of vim to remove them. That will do the job.

What does M mean in vim?

0xD is the carriage return character. ^M happens to be the way vim displays 0xD (0x0D = 13, M is the 13th letter in the English alphabet). You can remove all the ^M characters by running the following: :%s/^M//g. Where ^M is entered by holding down Ctrl and typing v followed by m , and then releasing Ctrl .

What is M in bash?

Show activity on this post. ^M is a carriage return, and is commonly seen when files are copied from Windows.


As a command, type

:%s/^M$//

(To get ^M, press ^V ^M, where ^ is CTRL on most keyboards)


One easy way to strip out the DOS line endings is to use the ff option:

:set ff=unix
:wq

Now your file is back to the good-old-Unix-way.

If you want to add the DOS line-endings (to keep a printer happy, or transfer files with Windows friends who don't have nice tools) you can go the opposite direction easily:

:set ff=dos
:wq

You can do this:

:set fileformats=dos

It will hide the ^M's, without touching the file.


There's a program called dos2unix that should strip those for you. Windows uses different line-ending characters which is why that happens.


This worked for me in a file that had everything on one line:

First find all matches

:%s/^M//

(To get ^M, press ^V ^M, where ^ is Ctrl on most keyboards)

Then replace with newlines

:%s//\r/g

Combined command would be:

:%s/^M/\r/g