Remove ^M Newline Character Using Vim

| Comments

Sometimes when opening a file in vim there are a lot of ^M characters at the end of everyline. This is probably because the file was saved in Windows which uses a slightly different newline, linebreak, carriage return, and line feed characters. It may look something like this:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod^M
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,^M
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo^M
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse^M
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non^M
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.^M

Using Vim to remove the ^M characters

The fasttest way to remove these characters in VIM is to do the following:


This command cannot be pasted directly into Vim. You must press Control+V then Control+M to get the special character.

The :%s is the substitution command. If you had :%s/this/that/ it would read substitute the word this with the word that.

The Control+V Control+M is using a special code to tell Vim to create the character we are looking for.

The /<Cntrl+V><Cntrl+M>/ means we are substituting it with a carriage return and line break that our system recognizes.

The g indicates to execute this substitution globally on every occurance.

misc, vim