I keep getting the ^M
character in my .vimrc
and it breaks my
configuration.
-
57BTW [this](http://vimdoc.sourceforge.net/htmldoc/digraph.html#digraph-table) is a great resource. – Lightness Races in Orbit Apr 30 '11 at 18:05
-
@LightnessRacesinOrbit - Hm, it works forms now, too. I have retracted my comment saying the link was dead. – ArtOfWarfare Jul 23 '15 at 11:27
-
3Mirror for @LightnessRacesinOrbit link, which is down at the moment: http://www.fifi.org/doc/vim/html/digraph.html#digraph-table – alexpls Sep 04 '15 at 03:23
-
7Typing `:digraphs` within vim shows the digraph-table that @LightnessRacesinOrbit linked to. – askewchan Sep 21 '15 at 19:18
-
1When trying to replace this kind of character using php, try this `preg_replace('/[\x01]/', ' ' ,$str); `Hope it helps. – Rachel Geller Mar 10 '16 at 10:21
-
This "question" sucks. Just saying. It isn't even a question, and noone seems to have noticed. There are at least 3 different things that may be going on with the issue -- your file encoding is as it should be but your Vim is misconfigured to show you `^M` because it expects different line endings than the file so consistently uses. Or it may be the other way around. Or you may have mix of line endings in your file. Yet all the answers at the top here tell you how to patch your actual file. All of the answers that explain the issue are at the bottom. Today is a good day to quit SO :P – Armen Michaeli May 26 '18 at 12:49
-
Compare with [Convert ^M (Windows) line breaks to normal line breaks](https://stackoverflow.com/q/811193) and [Convert DOS/Windows line endings to Linux line endings in Vim](https://stackoverflow.com/q/82726). – Henke Oct 26 '22 at 16:38
15 Answers
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).

- 942
- 10
- 17

- 12,796
- 1
- 30
- 32
-
12Just in case you're on Windows, Ctrl+V will probably be mapped to paste. If that's the case the defaults remap the "special character escape" key it to Ctrl+Q. – R. Martinho Fernandes May 01 '11 at 17:45
-
13The real question is why does Vim sometimes automatically detect the line endings and display correctly, and sometimes not. – Scott Feb 24 '13 at 17:30
-
12You will see the `^M` character if the file has mismatched line endings. Use the command provided by Tomasz, then type `:fileformat={unix|dos|mac}` depending on which OS you're targeting. If there are only a few lines without the `^M` character, you probably want `:fileformat=dos`. If you're editing files in an editor other than vim, be sure that it's configured to match line endings (Notepad won't do this, but any reputable editor like Notepad++ or TextMate will). – Mikkel Paulson May 27 '13 at 19:17
-
10Note for beginners to `vi`, that command needs to be typed in manually. Just pasting it into an open terminal with `vi` running will result in a "pattern not found" error. – IQAndreas Oct 09 '13 at 13:14
-
61@bluesm I actually needed to use `:%s/\r//g` to replace the carriage returns in my file. The ^V^M pattern couldn't be found in the file I had a bunch of ^M characters in. – jonnybot Jan 13 '14 at 23:00
-
What if I dont want to modify the file but just hide for better reading experience? Does vi has any option for that? – BTR Naidu Jun 27 '14 at 12:03
-
5Both `:%s/^M//g` and `:%s/\r//g` don't work for me. Return "Pattern not found" – A-letubby May 27 '15 at 06:57
-
-
1@Killercam In the end, I just use sed. Still don't have a clue why it didn't work on vim. – A-letubby Jun 02 '15 at 03:29
-
-
3@MikkelPaulson In GVIM I needed to type `:set fileformat={unix|dos|mac}`. – PerryC Jan 07 '16 at 16:04
-
For me, in Windows & VIM, typed **Ctrl + Shift + V**, **Ctrl + M** to present `^M`. *Ctrl + V* means **PASTE**. – youngzy Dec 10 '16 at 05:28
-
2@jonnybot Note that you should not type `^V^M`, but rather hold down `Ctrl` and press `v` followed by `m`, and then release `Ctrl`. – joelostblom Apr 04 '17 at 14:27
-
3Using `set ff=unix` then replacing the linebreaks manually in vim (deleting then re-adding the linebreaks) worked fabulously for me :D – StephanieQ Apr 05 '19 at 19:44
-
1
:%s/\r//g
worked for me today. But my situation may have been slightly different.
-
1the g option is not required, there is only one new line per line. `:%s/\r//` would suffice. – frogcoder Dec 17 '15 at 03:39
-
7The g option may very well be required, because a carriage return is not necessarily a newline. When opened in Unix mode, carriage return is just another character, and when opened in Windows mode, carriage return is only special immediately followed by line feed. – Jonathan Baldwin Apr 14 '16 at 02:48
To translate the new line instead of removing it:
:%s/\r/\r/g

- 27,279
- 10
- 68
- 73
-
4What is this actually supposed to do? To me it looks like it's doing a substitution of like for like, which I can't imagine would change anything. Am I missing something special about how the syntax works here? – Platinum Azure Oct 20 '14 at 14:02
-
20@PlatinumAzure I don't know why no one ever thought to answer your question. `\r` has different behavior in the search part vs. the replace part. See http://stackoverflow.com/a/3834303/ for instance. – Nov 17 '15 at 22:09
-
Or `:%s/^V^M/^V^M/g` see [here](http://stackoverflow.com/a/24542882/499167) – 681234 Jan 27 '16 at 14:13
-
-
For me this replaced the annoying characters with an additional newline. @dlk5730 answer worked better for me. ie `:%s/\r//g` – marcusshep Aug 16 '17 at 13:22
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

- 178,505
- 25
- 365
- 392
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.

- 403
- 1
- 5
- 11
I removed them all with sed:
sed -i -e 's/\r//g' <filename>
Could also replace with a different string or character. If there aren't line breaks already for example you can turn \r
into \n
:
sed -i -e 's/\r/\n/g' <filename>
Those sed
commands work on the GNU/Linux version of sed
but may need tweaking on BSDs (including macOS).

- 11,247
- 10
- 69
- 89
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.

- 944
- 10
- 15
In Unix it is probably easier to use 'tr' command.
cat file1.txt | tr "\r" "\n" > file2.txt

- 11,060
- 15
- 46
- 72

- 89
- 1
- 1
This is the only thing that worked in my case:
:e ++ff=dos
:wq

- 309
- 4
- 15
-
Excellent! This keeps the DOS line endings for those who need them, and fixes errors that prevent Vim from correctly identifying the DOS line endings (I had a file that was missing one CR out of several thousand lines). – Roger Dueck May 18 '21 at 17:38
If you didn't specify a different fileformat
intentionally (say, :e ++ff=unix
for a Windows file), it's likely that the target file has mixed EOLs.
For example, if a file has some lines with <CR><NL>
endings and others with
<NL>
endings, and fileformat
is set to unix
automatically by Vim when reading it, ^M (<CR>)
will appear.
In such cases, fileformats
(note: there's an extra s
) comes into play. See :help ffs
for the details.

- 586
- 5
- 11
You can fix this in vim using
:1,$s/^V^M//g
where ^ is the control character.

- 19,102
- 10
- 61
- 83
-
3
-
3He's not really asking what it is, he's asking how to fix it. What it is is a carriage return character from DOS. – Scott C Wilson Apr 30 '11 at 18:03
-
5_I_ know what it is. The title of the question is "What does ^M character mean in Vim". – Lightness Races in Orbit Apr 30 '11 at 18:04
In FreeBSD, you can clear the ^M
manually by typing the following:
:%s/
Ctrl+V, then Ctrl+M, then Ctrl+M again.

- 6,661
- 7
- 48
- 63

- 21
- 1
I've discovered that I've been polluting files for weeks due to the fact that my Homebrew Mvim instance was set to use filetype=dos. Made the required change in .vimrc....

- 51
- 2
If it breaks your configuration, and the ^M characters are required in mappings, you can simply replace the ^M characters by <Enter>
or even <C-m>
(both typed as simple character sequences, so 7 and 5 characters, respectively).
This is the single recommended, portable way of storing special keycodes in mappings

- 374,641
- 47
- 450
- 633