How to replace crlf with lf in a single file
How do I convert a single file that has crlf line returns to have lf line returns?
git is already correctly handling crlf to lf conversion automatically for files when I push them to a remote repository, but in this particular case I am not doing a push to a repository. Rather, I am uploading a file, using a file field on an HTML form, to a website that requires that the file have lf line returns. So I need to be able to convert this file individually.
My available potentially useful tools available on this computer would be git and Dreamweaver CC 2014.1. (I’m guessing Word, Wordpad and Notepad are not viable options but I’m open to being corrected.)
I am on Windows 7 and using git line commands.
7 Answers 7
The git installation on windows usually includes the dos2unix tool.
But in your case you should use .gitattributes to prevent the file from being converted on windows.
A .gitattributes file can look like this
Set to string value «lf»
This setting forces Git to normalize line endings to LF on checkin and prevents conversion to CRLF when the file is checked out.
Just commit the .gitattributes file and your file will be checkout out on every system with LF line ending.
Replace Carriage Return and Line Feed in Notepad++
I have a file with a mix of CRLF and LF, and wish to remove the LF. I am new to Notepad++ and just downloaded V6.23.
I’m using the search/replace dialogue box in extended mode. It will find and count the CR LF (using \r\n) but if I do a replace (to e.g. a blank or \r) nothing happens, and a replace all says it has replaced 0 occurrences. There are around 1000 CRLF in file and may be 100 lone LF.
17 Answers 17
Task: File has a mix of CRLF and LF, and you want to end up with only CRLF and delete all standalone instances of LF.
File:
File (Show All Characters): 
Now if you delete only the standalone LF at the end of Line 3, you should end up with Lines 3 and 4 joined together, with the rest of the line endings unaffected.
Replace #1 ( \n with (blank) ): 
File after Replace #1: 
Replace #2 ( \r with \r\n ): 
File after Replace #2: 
Final result: 
Conclusion: Notepad++ seems to be able to do the job just fine!
In the Notepad++ menubar click Edit → EOL Conversion → Old Mac format to format for entire files.

![]()
![]()
Here’s a SUPER SIMPLE WAY.
Select all the text you want to change, and then Click Edit Menu —> Line Operations —> Join Lines.
Simply, press CONTROL + A, and then CONTROL + J to do the same.
You can just copy everything to wordpad, create a new text file with notepad++ and paste it back to the new file from wordpad. Every line should have CR now.
you might have accidentally enabled the «show all character» option enabled in the editor

May I just suggest that the 2 most usual line endings are either CRLF or LF.
If you remove the LF, you will end up with line endings of just CR. This is non-standard.
This (depending on your usage) can break a bunch of things. For example windows hosts file will break if you do this (just spent a bunch of time on a support call about this issue).
View -> Show Symbol -> uncheck Show End of Line.
Make sure the Show all characters button is selected. Use Alt and drag the mouse over only the text you want to copy and paste. DO NOT SELECT CR|LF. CR|LF will get pushed aside. Do the same when you paste and use Alt and do not select CR|LF and hit Ctrl->v. This is to append a list of text to a separate file with a list of text.
Negative lookbehind allows you to find and replace just the LF that are not preceded by a CR:
Before:
After: 
In the find dialog there are three Search Modes, namely Normal, Extended (\n, \r, \O, \x. ) and Regular expression. Mine defaulted to Normal but you need Extended for the escape characters.
I faced similar issue. I followed the below steps to remove LF with CRLF
- Open file with notepad++
- Click Edit -> EOL Conversion -> Windows Format (This will append replace LF with CRLF)
- Save the file
Open the file in Notepad++.
Step 1) put in word warp ( view wordwrap)
Step 2) go to search.. click on : find the character in range tab
In the search window, you have to the range from 10 TO 10. then you able to find all the occurrence of CR/LF and LF.
Delete manually if the file is very small file.
![]()
Copy the whole text to a new tab, this will automatically replace the special characters. The main challenge with the find replace method is in case the text format/Alignment is distorted.
Based on the answers above, I’ve come up with a single substitution to solve this problem, which I had myself.
Instead of replacing «\r\n» for «\n» and doing it again, I replaced «\r\n» for «» (nothing), then it was done. I also marked «Wrap around» and the box next to it, extending the possible regular expressions.
For some reason, it is necessary to replace double spaces with a single space afterwards, but the «\r\n» is solved after the first replacement.

![]()
![]()
The EOL conversion feature under the Edit menu does the job just fine, as someone has already mentioned — very quick, very easy, very accurate. I did notice something a little odd, though, which is why I’m throwing in my two cents worth.
I was aiming for Windows encoding. In some of my files, I didn’t have the option of choosing ‘Windows Format’ because it was greyed out, I’m guessing because most of the file was using Windows End Of Line characters. The simple solution was to choose one of the other two options (‘UNIX/OSX Format’ or ‘Old Mac Format’) and then, once again, go ‘Edit —> EOL Conversion’ and Windows Format was now available to choose.
In order to remove the CRLF you have to select the icon at the top that says «Show all characters», and they will go away. Just scan with your mouse and find the one that looks like a backwards P.
Just Use replace [^\r]\n with » » and select regular expression it will replace all \n with » » but doesn’t do anything to \r\n
-
The Overflow Blog
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.3.11.43304
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How to convert newlines between Unix(LF) and DOS/Windows(CRLF)
The term CRLF refers to Carriage Return (ASCII 13, \r) Line Feed(ASCII 10, \n).
They’re used to note the termination of a line, however, dealt with differently in today’s popular Operation Systems.
For example: in Windows, both a CR and LF are required to note the end of a line, whereas in Linux/UNIX a LF is only required.
In the HTTP protocol, the CR-LF sequence is always used to terminate a line.
sed command
SED command in UNIX stands for stream editor and it can perform lots of functions on file, like searching, find and replace, insertion or deletion.
- Replace or substitute string:
The most common use is for substitute or find and replace. The commands below replace “cat” with “dog” in the pet.txt :
Here the “s” specifies substitute, there are some useful flags for this operation:
Replace newline
If you know how to enter the carriage return character in bash( Ctrl-V then Ctrl-M ):
Notice that “^M” represents a carriage return character, is not just “^” + character “M”.
Configuring Git to handle line endings
To avoid problems in your diffs, you can configure Git to properly handle line endings.
About line endings
Every time you press return on your keyboard you insert an invisible character called a line ending. Different operating systems handle line endings differently.
When you’re collaborating on projects with Git and GitHub, Git might produce unexpected results if, for example, you’re working on a Windows machine, and your collaborator has made a change in macOS.
You can configure Git to handle line endings automatically so you can collaborate effectively with people who use different operating systems.
Global settings for line endings
The git config core.autocrlf command is used to change how Git handles line endings. It takes a single argument.
On macOS, you simply pass input to the configuration. For example:
On Windows, you simply pass true to the configuration. For example:
On Linux, you simply pass input to the configuration. For example:
Optionally, you can configure a .gitattributes file to manage how Git reads line endings in a specific repository. When you commit this file to a repository, it overrides the core.autocrlf setting for all repository contributors. This ensures consistent behavior for all users, regardless of their Git settings and environment.
The .gitattributes file must be created in the root of the repository and committed like any other file.
A .gitattributes file looks like a table with two columns:
- On the left is the file name for Git to match.
- On the right is the line ending configuration that Git should use for those files.
Here’s an example .gitattributes file. You can use it as a template for your repositories:
You’ll notice that files are matched— *.c , *.sln , *.png —, separated by a space, then given a setting— text , text eol=crlf , binary . We’ll go over some possible settings below.
text=auto Git will handle the files in whatever way it thinks is best. This is a good default option.
text eol=crlf Git will always convert line endings to CRLF on checkout. You should use this for files that must keep CRLF endings, even on OSX or Linux.
text eol=lf Git will always convert line endings to LF on checkout. You should use this for files that must keep LF endings, even on Windows.
binary Git will understand that the files specified are not text, and it should not try to change them. The binary setting is also an alias for -text -diff .
Refreshing a repository after changing line endings
When you set the core.autocrlf option or commit a .gitattributes file, you may find that Git reports changes to files that you have not modified. Git has changed line endings to match your new configuration.
To ensure that all the line endings in your repository match your new configuration, backup your files with Git, delete all files in your repository (except the .git directory), then restore the files all at once.