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:


The [CTRL+v][CTRL+m] means holding down the CTRL key and then typing vm. The text displayed will look like this:



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: , , , , , , , ,

  1. Kanishq

    Concise and to the point.

  2. Sunny

    It works for me

  3. Kev

    Awesome post!!!!

    I wish other people would be as clear and thorough as you!

Add a Comment