14 Ağustos 2012 Salı

Removing carriage returns from MS-DOS (^M) in VIM

To contact us Click HERE


If you ever try to edit a MS-DOS file, you'll notice that each line ends with a ^M character. This is caused by the funny way that MS-DOS treats the end-of-line. (For some background on this problem take a look at The EOL Story.

Remove the ^M in Vim

To remove the ^M characters from a MS-DOS file, enter the command:

:%/{Ctrl+V}{Ctrl+M}//{Enter}

This command starts with a colon (:) to tell Vim to enter ex mode. All ex start with a line number range, in this case its from the first line (1) to the last ($). The slash indicates the start of the "from text". The {Ctrl+V} tells Vim to treat the next character as a regular character even if it's a special one. The next character is {Ctrl+M}. (This would be treated as {Enter} without the {Ctrl+V}.) The next slash ends the "from text". What follows is the "to text" enclosed by slashes. In this case it's nothing (//).

Not display the ^M

You also can
:set fileformats=dos
It will hide the ^M's, without touching the file.

Using dos2unix

There's a program called dos2unix that should strip those for you. Windows uses different line-ending characters which is why that happens.
dos2unix

Hiç yorum yok:

Yorum Gönder