Vim Recipes ‣ Editing ‣ Working with Different File Formats
You're editing a file created on a different operating system, so need to change its line endings. Or, you wish to convert a file to use different line endings, perhaps because a tool expects them that way.
Specify the desired file format with :set ff=format, where format is dos for Microsoft Windows/DOS files, unix for Unix/Linux, or mac for Apple Mac. As usual, you must then save the file to make the changes permanent: :up.
Different operating systems have different notions of what constitutes a line in a text file. Specifically, they disagree on the control character that terminates a line. Unix/Linux uses a line feed (LF), Microsoft Windows uses a carriage return followed by a line feed (CRLF), and the Mac uses a carriage return (CR). This would be a matter of pedantry were it not for users of different operating systems sharing files.
Luckily, Vim handles this situation transparently most of the time. When a file is loaded, its format is guessed, and &ff is set appropriately. If Vim guesses incorrectly, you can insist on a specific file format with :e ++ff=format, thus forcing the file to convert to the given format.
The above assumes that a file's line endings are at least consistent. That is, each line ends with the same character(s). If your file is mangled (some lines ending with CR, some with LF, for example) you will likely see stray control characters such as ^J or ^M peppered throughout. This can be fixed with search and replace: :%s/\r//. Lastly, if you have some Mac line endings with dos/unix or Unix/Linux line endings with mac, use :%s/\r/\r/g.