Remove control characters (^M) by search and replace in vi

If you edit a text file in a UNIX or Linux environment that has it’s origin in the DOS or Windows world, you will see that every line ends with the control character ^M. The reason is that the UNIX or Linux world only use one control character to mark an end of line and in the DOS or Windows world this is done by two control characters.

The control characters will be automatically converted if you use ASCII mode when transferring the file by FTP between the UNIX or Linux world and the DOS or Windows world.

However, if you end up with a text file in the UNIX or Linux environment that for some reason didn’t get its end of line control characters converted, you will see that every line ends with a ^M. Those ^Ms can easily be stripped away by using the search-and-replace function in the vi editor. This requires that you have a basic knowledge on how to edit files in vi.

To search and replace the ^M you use the search and replace command %s. The format for this command is:

(press ESC key once)
:%s/TEXT/REPLACE/g

where all occurences of the TEXT will be replaced by the word REPLACE. Now we want to replace a control character, so instead of TEXT we should enter ^M. If we just type the ^ character and then the M character on our keyboard, the search and replace function will not match the ^M seen in our text file. This is because they are control characters, i.e. control-M (press and hold the CTRL-key while pressing M). If we try to just press CTRL-M after the :%s/ you will get an error message.

This is because the CTRL-M is the same as pressing the Enter key. To tell vi not to interpret the CTRL-M you need to escape it. This is done by pressing and holding CTRL in the same time as the V-character once before pressing CTRL-M. In the example below, pressing and holding CTRL while pressing the V-character is shown as a ^V and pressing and holding the the CTRL while pressing the M-character is shown as a ^M.

So to search and replace all ^M in the entire file in vi you should do:

(press ESC key once)
:%s/^V^M//g
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

thirteen − 8 =