How to remove ^M characters using VI
This is a quick tip for Linux/UNIX users who are familiar with the vi editor.
Here’s how to remove those annoying ^M characters, that show up at the end of lines, in files that were created or modified in DOS/Windows.
Open the file using vi, and type:
:%s/[CTRL+v][CTRL+m]//g
The [CTRL+v][CTRL+m] means holding down the CTRL key and then typing vm. The text displayed will look like this:
:%s/^M//g
Explanation
In Linux or UNIX, typing [CTRL+v] allows you to escape a control character.
The :%s is the substitute (search and replace) command for vi. It tells vi to replace the regular expression between the first and second slashes (^M), with the text between the second and third slashes (nothing in this case).
The g at the end tells vi to search and replace globally (i.e. all occurrences).
Tags: dos, editor, Linux, text file, UNIX, vi, vim, windows, ^M
October 23rd, 2008 at 9:16 pm
Concise and to the point.
Thanks
December 23rd, 2008 at 12:22 pm
Thanks,
It works for me
January 28th, 2009 at 3:35 pm
Awesome post!!!!
I wish other people would be as clear and thorough as you!