Home > Linux > Getting rid of the ^M characters in vi

Getting rid of the ^M characters in vi

January 1st, 2009

If you’re a regular vi user, you may have noticed that some files, when being edited in vi, contain ^M characters at line ends.

This usually happens when you edit a file using certain windows-editors, then transfer it to your *nix machine.
Luckily, it is easily to get rid of this control character. While in vi, execute the following command:

:1,$s/^M//g

Important note: do not manually type a caret then the capital M character. Actually, in order to type ^M, press CTRL+V followed by CTRL+M.

A quick note:  the above command will look for the ^M character starting on line 1, replacing it ($s) with nothing (thus having the two consecutive forward slashes / with nothing in between). And this replacement is done globally (g).

Linux

  1. No comments yet.
  1. No trackbacks yet.