Garbled characters in Windows and Mac? Difference of Line Break Code and Conversion Method
Text

Garbled characters in Windows and Mac? Difference of Line Break Code and Conversion Method

Have you ever experienced that line breaks disappeared when you opened text file? We explain difference of line break codes such as CRLF, LF and simple conversion method using tool.

Mystery of "No Line Break!" Text File

Have you ever experienced that line breaks all disappeared and connected to one line when opening text file created in Windows on Mac, or opening log file of server with Notepad on Windows? Or, when engineer investigated because program did not work for some reason, invisible "Line Break Code" was the cause... it is common story for engineers.

Actually, in the world of computers, there are multiple codes to represent "Line Break", and standard differs depending on OS. This time, we explain difference of this troublesome "Line Break Code" and conversion method to avoid trouble in detail.

Main Types of Line Break Codes

There are roughly 3 types of line break codes. Let's keep features of each in mind.

1. CRLF (\r\n) — Windows Standard

It represents "Line Break" by combining two control characters CR (Carriage Return) and LF (Line Feed). Since it is derived from operation of typewriter (return carriage to beginning and send paper by 1 line), 2 characters become 1 line break. This format is standard in text editors and applications of Windows.

2. LF (\n) — Unix/Linux/Mac Standard

Often regarded as "Line Break" only by LF (Line Feed). It is standardly used in macOS (OS X or later), Linux, Unix-like systems. In modern programming and Web development, this LF is de facto standard. GitHub and most Web servers also assume LF.

3. CR (\r) — Classic Mac

Regarded as "Line Break" only by CR (Carriage Return). It was used in very old Mac OS (version 9 or earlier), but is rarely seen now. It is to the extent that you encounter it very rarely when handling legacy files.

Common Troubles and Concrete Scenarios

Trouble caused by difference of line break code occurs in various scenes of daily work. Typical scenarios are introduced below.

Scenario 1: Script Written in Windows Does Not Work on Linux Server

Most classic trouble. If you create shell script (.sh) with Notepad or editor of Windows and upload it to Linux server, error such as /bin/bash\r: bad interpreter or \r: command not found occurs because CRLF is mixed.

Solution: Convert to LF before upload, or set line break code at saving to LF with editor such as VS Code.

Scenario 2: Sharing CSV File Between Mac and Windows

When opening CSV file created on Mac with old "Notepad" of Windows, line break is not recognized and it is displayed connected to 1 line. Conversely, if you save CSV with Excel, it becomes CRLF, so if you try to process with terminal of Mac, data may be broken because extra CR (\r) is mixed.

Solution: If you convert collectively on Mac side before exchanging, you can prevent compatibility problem. If you use Jenee's line break code conversion tool, you can convert instantly on browser.

Line Break ConverterConvert line breaks for any OS.

Scenario 3: Large Amount of Diff of Line Break Code Occurs in Git

When team members are using Windows and Mac mixed, since line break code is different even in same file, all lines may be treated as change even though there is no substantial change. It is troublesome at code review.

Solution: Create .gitattributes file in root of project and set automatic conversion rule of line break code. Generally, by describing * text=auto, it automatically converts to line break code suitable for OS at checkout.

Scenario 4: Text from Web Form Is Not Processed Correctly

There are cases where text pasted by user to Web form is not processed correctly in backend. Especially when counting number of characters, 2 bytes of CRLF may affect count. If you unify line break code and then count, accurate value is obtained.

Letter & Word CounterReal-time character and word count for your essays, articles, and social media posts. Instant counts for any text.

Solve Instantly with Jenee's "Line Break Code Conversion" Tool

Such trouble of line break code can be solved instantly if you use tool. In Jenee's Line Break Code Conversion Tool, simple conversion is possible on browser.

Line Break ConverterConvert line breaks for any OS.

Usage (3 Steps)

  1. Paste text or copy from file.
  2. Choose destination format (LF or CRLF).
  3. Copy and use converted text.

Just by this, you can solve invisible line break code problem quickly. Since installation of software is also unnecessary, it is big merit that you can use it immediately even when you are in a hurry.

💡 Hint: If text contains HTML tags and is obstructive, it is smoother if you clean up with HTML tag removal tool first and then convert line break code.

HTML Tag RemoverRemove HTML tags and extract plain text from HTML source.

For Developers: Settings to Prevent Line Break Code

It is also important to organize environment so that trouble does not occur in the first place, not just fixing it afterwards.

Editor Settings

In editor such as VS Code, you can set default line break code.

  • VS Code: Set files.eol to \n (LF) in setting screen
  • Status Bar: Line break code for each file is displayed, so click to switch

Team Unification by .gitattributes

To manage line break code on project basis, .gitattributes is most reliable.

* text=auto
*.sh text eol=lf
*.bat text eol=crlf

If you set like this, you can do team development without being conscious of difference of OS.

In processing of text data, various conversions and formatting besides line break code may be necessary. Jenee also provides following tools.

Letter & Word CounterReal-time character and word count for your essays, articles, and social media posts. Instant counts for any text.

Text DiffHighlight differences between two texts or code

Line DeduplicationRemove duplicate lines from text.

HTML Tag RemoverRemove HTML tags and extract plain text from HTML source.

Frequently Asked Questions (FAQ)

Q. I cannot see difference of line break code by appearance?

Yes, in general text editor, since both CRLF and LF are displayed as mere "Line Break", there is no difference in appearance. Since "CRLF" or "LF" is displayed at bottom right of status bar in VS Code, you can check there. Also, if you execute cat -A filename in terminal, ^M is displayed at end of each line in case of CRLF.

Q. Garbled characters are not fixed even if I convert line break code?

"Line Break Code" and "Character Code (Encoding)" are different problems. What can be fixed by conversion of line break code is only trouble related to line delimiter. Garbled characters of Japanese (difference between UTF-8 and Shift_JIS etc.) require conversion of character code. First, let's isolate which is the cause.

Q. Which line break code should be unified?

In field of Web development and programming, Unifying to LF is general. This is because GitHub and most server environments assume LF. It is best practice to use CRLF only for purposes specialized for Windows environment such as Windows-specific batch files (.bat, .cmd).

Q. Is there a way to batch convert large amount of files?

If you are familiar with command line, batch conversion is possible using dos2unix command on Linux/Mac and unix2dos command on Windows. If it is small amount of text, Jenee's Line Break Code Conversion Tool is easy and recommended.

Summary

"Line break is line break, but line break". Invisible difference of line break code becomes cause of surprisingly big trouble in file sharing and program execution.

Organization of Points:

  • CRLF is standard for Windows, LF is standard for Mac/Linux
  • Be careful of line break code when exchanging files between different OS
  • Unifying with .gitattributes is best in team development
  • Jenee's online tool is convenient for urgent conversion

When in trouble, please utilize Jenee's conversion tool.

Line Break ConverterConvert line breaks for any OS.

Related Articles