Windows and UNIX-like Systems Use Different Line Break Characters

The difference between CRLF and LF line endings and how to convert them with WordPad or Vim

Line Break Characters by OS

Windows and UNIX-like operating systems handle line breaks differently. The difference is in the newline characters: Windows uses CR+LF, while UNIX-like operating systems such as Linux use LF. It may be easier to understand this as the difference between \r\n and \n.

The symbols mean the following.

  • \r: CR (Carriage Return). Move to the beginning of the current line.
  • \n: LF (Line Feed). Move to the next line.

Line break characters by OS are as follows.

  • DOS (Windows): \r\n
  • UNIX or Linux: \n
  • Mac: \r, which by itself means CR+LF.

How to Solve Line Break Character Issues by OS

Because of this issue, text files written on Linux often do not display line breaks correctly when opened on Windows. Let’s look at ways to solve this problem.

Windows - WordPad

On Windows systems, there is a program called WordPad under Start - Accessories.

If you open a text file written on Linux with WordPad, the line breaks are displayed normally.

In this state, use the “Save As” feature. A window appears where you can specify the save path and file name. In the lower “Save as type” section, you can select “Text Document - MS-DOS format”.

If you select this and save the file, the line breaks are processed and saved as Windows CR+LF, creating a text document that can be used normally on Windows.

If this method still does not process the file correctly, handling it on Linux, where the problem started, is probably the fastest method.

UNIX-like OS - VIM

Open the target file with VIM.

In Linux VIM, you can freely convert Windows files to UNIX format and UNIX files to Windows format. First, convert a Windows file to UNIX format as follows.

:set ff=unix

Now just save the file. It is very simple.

Converting a UNIX file to Windows format is also simple.

:set ff=dos

Summary

If you cannot use this kind of editor, one option is to download the file from FTP in ASCII mode from the beginning so it is converted automatically. Finally, as a very simple tip for handling \r\n and \n, you can unconditionally replace \r\n with \n and then process only \n.