Vi: Remove Ctrl-M characters from a document
Thursday, June 30th, 2011I regularly have to move scripts between Windows and Linux systems. The line endings from
Windows can make running my scripts on Linux fail. I have been on a few systems where the
dos2unix command was not available, so how do I get rid of the Ctrl-M at the end of the lines?
Command: vi -b <filename>
The -b sets vi into binary mode. This displays the extra control characters. In vi, I remove the
ctrl-Ms.
In vi, I use the following key sequence in command mode:
:%s/<ctrl-v><enter>//g
The ctrl-v tells vi to take the next character and display it as a ctrl char sequence. And hitting the
enter key displays the ctrl-M. The :%s///g is a command to replace the characters in the first // and replace
it with the chars in the second // and the g says to do it to every line.