Advertisement
Advertisement


What does ^M character mean in Vim?


Question

I keep getting ^M character in my vimrc and it breaks my configuration.

2013/07/14
1
513
7/14/2013 1:13:21 AM

Accepted Answer

Unix uses 0xA for a newline character. Windows uses a combination of two characters: 0xD 0xA. 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. This is sometimes abbreviated as ^V^M, but note that you must enter it as described in the previous sentence, rather than typing it out literally.

This expression will replace all occurrences of ^M with the empty string (i.e. nothing). I use this to get rid of ^M in files copied from Windows to Unix (Solaris, Linux, OSX).

2017/10/04
575
10/4/2017 4:54:47 PM

:%s/\r//g 

worked for me today. But my situation may have been slightly different.

2015/08/31

To translate the new line instead of removing it:

:%s/\r/\r/g
2014/07/02

It probably means you've got carriage returns (different operating systems use different ways of signaling the end of line).

Use dos2unix to fix the files or set the fileformats in vim:

set ffs=unix,dos
2011/04/30

Let's say your text file is - file.txt, then run this command -

dos2unix file.txt 

It converts the text file from dos to unix format.

2015/06/22

I got a text file originally generated on a Windows Machine by way of a Mac user and needed to import it into a Linux MySQL DB using the load data command.

Although VIM displayed the '^M' character, none of the above worked for my particular problem, the data would import but was always corrupted in some way. The solution was pretty easy in the end (after much frustration).

Solution: Executing dos2unix TWICE on the same file did the trick! Using the file command shows what is happening along the way.

$ file 'file.txt'
file.txt: ASCII text, with CRLF, CR line terminators

$ dos2unix 'file.txt'
dos2unix: converting file file.txt to UNIX format ...
$ file 'file.txt'
file.txt: ASCII text, with CRLF line terminators

$ dos2unix 'file.txt'
dos2unix: converting file file.txt to UNIX format ...
$ file 'file.txt'
file.txt: ASCII text

And the final version of the file imported perfectly into the database.

2014/11/21

Source: https://stackoverflow.com/questions/5843495
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]