How to Clean Up a Mac File in VI with ^M Line Endings

Replacing ^M with new line in VI

Have you ever edited a file in VI that was created with a Mac text editor only to find you have one very long line with ^M characters where you are expecting line endings? Historically Mac OS used Carriage Return characters to indicate the end of a line. Most parts of OSX are more UNIX-like these days but there are still many tools that write text files with CR line endings. Those line endings display in a normally configured VIM installation as ^M

I’ve seen a number of strange search and replace solutions floating around, basically involving searching for a strange string of control characters. But there is a better way.

Permanently Changing the Line Endings

VIM (which is what you are running on most modern *nix systems when you type vi these days.) is able to convert the file for you very easily. Once you have a file open that you would like to change type the following commands.

1
2
3
:set ff=mac
:w
:e

The first line changes the “file format” to mac. The second saves the file. The third reloads the file and it should look clean. If you want to see what type of file vim thinks the file is you can type “:set ff?”.

1
2
:set ff?
fileformat=unix

Of course it’s a unix file now, but it thought it was a unix file to start with. (to confirm that you can load another unaltered Mac file and type “:set ff?” to see.) And therein lies the problem. Maybe it would be better to get VIM to understand mac files.

Configuring VIM to speak Mac

If you’re dealing with a lot of files created on a Mac it might be better to just reconfigure VIM. Which is not as hard as it may sound. The setting we want to alter is ffs, which is what tells VIM what kind of line endings it should be looking for. To see what your setting currently is type “:set ffs?”.

1
2
:set ffs?
fileformats=unix,dos

If you already have the file open and you want to reload it as a mac file WITHOUT actually changing the file you can do this…

1
2
:set ffs=unix,dos,mac
:e

Now you can edit and save the file without changing the line endings, which will probably be more convient for the original editor if they still need to use a tool that would prefer the original line endings. The first item in the file formats list is your default format. So if you want new files saved as mac, you can put it first in the list. When a file is loaded, the order makes no difference.

Of course that change only affects the current buffer. So unless you want to do that every time you probably want to edit your .vimrc file in your home directory. You don’t need the colon in the config file. From the command line just type…

1
echo 'set ffs=unix,dos,mac' >> ~/.vimrc

Now you should not have to worry about the dreaded ^M any more!

More VIM file format details can be found at http://vim.wikia.com/wiki/File_format

Category(s): Command Line, Linux
Tags: , , , , , ,

Leave a Reply

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

*

 

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free