Trust me. We’ve all been there. The first battle a newcomer has to go through is already a horrific experience. “Unable to quit Vim“. Sounds familiar? Great. But hold on. It doesn’t end there.
Ever realized trying to copy a string of text from the browser with Ctrl+c and pasting it with Ctrl+v on a regular text editor to be the nicest thing to have? Sorry to interrupt, Vim is too good to call that a nice thing. It has its own ways of doing things, which in turn, means it won’t function like the generic code editor you’ve been using all this time. Try pasting a string of code in Vim insert mode and see nothing appear, only to then press Enter in hope of some Magic to happen and get the thing done.
Behold! You do see Magic, but not the one you expected. This is the “^M” magic. It doesn’t paste the text, but rather fills your screen with unprecedented Ms the more you try pressing Ctrl+v and Enter in horror. As Vim is designed around UNIX Based Systems, it handles digraphs (more on that later) differently than other operating systems do, one of which is ^M, also known as the “Carriage Return“.
What on Earth is a Carriage Return?
A Carriage return is basically a type of input to tell the computer (or a typewriter, which is where it all started) that you want to reposition the Text Input Cursor and bring it back to the start of the line. UNIX systems pair this Carriage Return with the newline character (\n) to place the cursor at the start of a new line after the one you’re currently on. This is done when you press the Enter key in conventional operating systems. And that is where things get a bit tricky.
Inside of Vim insert mode, if you try pressing Ctrl+m, you’ll see that it functions just like the Enter key, taking the cursor to the start of a new line. Functions that make use of Ctrl+[another character] are mapped as “^[that another character]” in traditional consoles (Ctrl+c is mapped as “^C“, for instance).
In traditional UNIX terminals (or consoles), Ctrl+v is mapped as "Verbatim Insert", whose task is to print the digraph pressed right after Ctrl+v on the console without executing its function. Sounds tough huh?. Hear me out. What it essentially does, is that as you press Enter or Ctrl+m after pressing Ctrl+v, the "Verbatim Insert" thing prints ^M on the screen rather than executing its function, which is to reposition the cursor at the start of a newline. Therefore, Ctrl+m means ^M, and ^M means "Carriage Return", while "Carriage Return'' is the thing that happens when you press Enter in a Text Editor. But in Vim, stuff happens when you try pressing either Ctrl+m or Enter right after pressing Ctrl+v. And hold on, there's a reason for that.
In a few cases, Ctrl+v may not be the only reason for getting ^M. You may get these printed in your files unintentionally, for some random bug caused by the interaction of modern kernel functionalities with the Vim source code. But the most common appearance of ^M is when you try paste stuff in Vim.
Another common reason for encountering ^M
Another common appearance of ^M is when you save a file from the Vim Editor in Windows, and then bring the file to a Linux Machine. Pretty complex stuff, but just to let you know, this happens because UNIX systems use “0xA” to map a newline character, while Windows uses two characters – “0xD” and “0xA”. When you save a vim file in Windows with a bunch of newlines, and open it up from a Linux Machine, you happen to get the mighty “^M”s as Vim is just trying to display the characters directly.
Finally understand what this whole “^M” thing is about? It’s about time you know how to get rid of it.
How to get rid of ^M?
The easiest workaround is to forget that Ctrl+v doesn’t paste text in Vim like it does elsewhere, so simply stop using it in Vim. Vim pastes the text by only pressing “p” in Vim Command Mode, which is off to a later topic. But if you wish to not get Ms anymore, stop pressing Ctrl+v inside of Vim!
There’s another small trick though, at least get rid of all the ^M present in your current Vim instance or file.
To get rid of all the “^M“, type the following in Vim Command Mode:
Not working, right? I know it wouldn’t. Because this is where most people get stuck. Regularly expressing “^M” in the above code will never work. What you need to do instead, is this:
Make sure that you actually press Ctrl+v and Ctrl+m while typing the above code. Expressing the above one regularly wouldn’t work either. Mark that.
This will hopefully remove all the unwanted ^Ms.
What are digraphs, though?
In plain English terms, digraphs are pairs of two characters joined together to represent a specific sound. In UNIX Systems, digraphs are used to map pairs of other commands or characters together to execute a specified function, which in our case, is Ctrl+m, shortened as the “^M” digraph for easier interpretation of system handlers.
So there you have it! The next time you see this Mighty “^M” appear after trying to paste stuff with Ctrl+v, remember to execute the given trick. Moreover, if you frequently happen to transfer files saved by Vim in Windows and transfer them to a Linux Machine, use this trick, as there is no permanent fix to that. Try asking the developers of Windows, if you really wish! Or else, simply use Linux when editing Vim files.