109

Is there any way in Notepad++ (or even with another tool) to change the line ending automatically on multiple files in one go?

i.e. convert a mix of windows EOL (CRLF) and UNIX EOL (LF) files to be all Windows EOL (CRLF)

1

8 Answers 8

113

The Replace dialog can handle extended characters like EOL. Just change "Search Mode" to "Extended", and you can work with EOL (\r\n in Windows or \n in Unix), tabs (\t), etc.

You can also use the Find in Files tab of the dialog to do the replace across multiple files.

Screenshot

6
  • 16
    Note that this solution will not work if all your files are not already unix-style. Since replacing \n with \r\n will also convert \r\n to \r\r\n.
    – Kirk Woll
    Jan 10, 2014 at 21:41
  • -1 without changing Edit>EOL conversion (see Dos\Windows in the status bar of your screenshot), pressing ENTER will insert the wrong EOL - and having mixed EOL is even worse than keeping to any of the standards
    – Aprillion
    Jan 28, 2014 at 12:46
  • 48
    replace \n with \r\n. then replace \r\r\n with \r\n. voila!
    – dev_row
    Apr 16, 2014 at 2:57
  • 4
    Why your screenshot example shows \t in Find what? Tabs have nothing to do with changing line endings, right?
    – trejder
    Jul 10, 2014 at 10:08
  • 1
    great...it works perfectly in notepad++ editor...it saves huge effort, that we are going to spend by manual changes
    – aniruddha
    Aug 3, 2016 at 12:14
93

I have Notepad++ 6.1.2.
In "Edit" menu you have "EOL conversion" that does exactly what you need.

12
  • 33
    that works on a file basis, cannot be applied to a whole set of files.
    – fduff
    Jul 5, 2012 at 9:50
  • 12
    Have you read the title of the question? Did you see the "change EOL on multiple files" part?
    – Sk8erPeter
    Sep 4, 2013 at 20:46
  • 2
    You would open more than one file to do it to more than one file, sequentially. Like he said, "in one go" wasn't originally there.
    – gparent
    Oct 31, 2013 at 14:26
  • 7
    In fact the answer was very useful to me, since I needed to change EOL in single files, and I didn't know how.
    – DiegoDD
    Nov 20, 2013 at 18:09
  • 5
    Useful answer as this comes up first in Google Jun 23, 2014 at 5:41
83

Use the 'Find In Files' feature (Ctrl + Shift + F). Change the search mode at the bottom left to 'Regular Expression'.

In the 'Find what' box, use this pattern:

(?<!\r)\n

Replace with:

\r\n

Choose your directory and specify any file type filters. Check 'In all sub-folders' if you want. Click 'Replace in Files'.

What this does is replace any newline characters (\n) that are not currently preceded by a carriage return (\r) with \r\n. So it won't match line endings that are already Windows style.

enter image description here

5
  • 1
    Could you explain what (?<!\r)\n means, especially the first ?<! part, thanks!
    – WSBT
    Jan 8, 2016 at 20:29
  • 2
    the (?<! ) is a negative lookbehind. It means match if prefix is absent. In this case it's checking for \r and will only match if \n doesnt have an \r before it.
    – rtpHarry
    Jan 9, 2016 at 15:02
  • Yep. I agree with rtpHarry. This is this part from my answer: "any newline characters (\n) that are not currently preceded by a carriage return (\r)".
    – Sean
    Jan 12, 2016 at 1:04
  • 1
    most sophisticated solution! Sep 30, 2016 at 14:01
  • 6
    Technically speaking, this is the most precise and complete answer on this page.
    – dotNET
    Mar 23, 2017 at 5:56
14

Use replace all with regular expression

(\r?\n)|(\r\n?)

to

\r\n

This will match every possible line ending pattern (single \r, \n or \r\n) back to \r\n (Windows).

To operate on multiple files, either:

  • Use "Replace All in all opened document" in "Replace" tab. You will have to drag and drop all files into Notepad++ first. It's good that you will have control over which file to operate on but can be slow if there several hundreds or thousands files.
  • "Replace in files" in "Find in files" tab, by file filter of you choice, e.g., *.cpp *.cs under one specified directory.
2
  • Best answer, as it works with all flavours of line endings. Thanks!
    – yoyo
    Jul 5, 2019 at 16:45
  • This is the best and easiest answer! Jul 10, 2020 at 23:57
7

The only WORKING solution i found for multiple files/folders, after googling for 1 hour is this:

  • install PyCham trial mode,
  • open and select your Project Folder/Folders and follow the screenshot

enter image description here

2
  • 2
    Also works in IntelliJ IDEA Community Edition, which is free and does not require trial. IntelliJ IDEA.
    – omalyutin
    Apr 26, 2019 at 9:13
  • Pity for the downvote. I came here as I thought that "Rider (PyCharm for C#) won't do that, let's start with notepad++". Thanks. Aug 18 at 14:46
1

Found this solution via this discussion:

You can also set the default EOL in notepad++ via "Settings" -> "Preferences" -> "New Document/Default Directory" then select "Unix/OSX" under the Format box.

Note: One can always use an out-of-band option using the command line:

unix2dos *.cmd
dos2unix *.sh
1

To convert multiple files into one directory and recursively. Just install PythonScript on Notepad ++, then use the script below

https://gist.github.com/bjverde/583c2ee8b386994f3a1f8acdea3b7ed2

1

This did the work for me

git config core.autocrlf false 
git rm --cached -r . 
git reset --hard

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.