hat tip Matthew Turland
Have you ever opened up a file using VI and found the following?
<?php echo '<html>'^M echo '<head>'^M echo '</head>'^M echo '<body>'^M ...
That stupid control character from some other horrid text editor is trailing every single line. And you are annoyed enough to Google for an answer. ^M
Now, hopefully you are aware of the powerful regex tools built right into VI. But in this case, the fix is pretty simple, and we won’t have to craft any regFu. After you open your file using VI, we will then use the search/replace functionality. And we will eventually end up with a line that looks like this.
But there is a gotcha to this solution, so you may need to read on to find out more. And before proceeding, press esc to make sure you are not in insert mode.
First type the following.
Shift + :
Which should just print the colon at the bottom of your screen.
Then type this exactly how it looks.
Now we need to type the control character. You may be thinking “Jared, I will just hit shift + 6 and get that character onto my screen in no time.”. And you are more than welcome to do that, but you would be wrong.
You will need to press the following keys.
Control + V
Which will get you this printed out.
Then type the following.
Control + M
Which should have you ending up with this.
At this point you can type the rest of the characters exactly how they look.
Which leaves you with a finished line that looks like so.
Press enter, and you will now replace the ^M character with nothing.
Part 2(kinda): Small search and replace example.
This would replace Dog with Cat.
Cat with Mouse.
Mouse with Trap.
So our line to replace the ^M character is literally saying “Replace the ^M character with nothing”.