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)

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
fduff
  • 3,671
  • 2
  • 30
  • 39
  • 1
    In case notepad++ isn't a firm requirement, the info here might be relevant: http://stackoverflow.com/questions/3110031/batch-file-convert-lf-to-crlf – reuben Jul 05 '12 at 09:43

8 Answers8

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

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • 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 '14 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 '14 at 12:46
  • 48
    replace \n with \r\n. then replace \r\r\n with \r\n. voila! – dev_row Apr 16 '14 at 02:57
  • 4
    Why your screenshot example shows `\t` in `Find what`? Tabs have nothing to do with changing line endings, right? – trejder Jul 10 '14 at 10:08
  • Great tip! This helped me convert Windows line endings to Unix style in a pile of files where I wanted to also see which ones had the wrong endings (rather than using a tool to do this on everything automatically). – BuvinJ Aug 04 '15 at 22:57
  • 1
    great...it works perfectly in notepad++ editor...it saves huge effort, that we are going to spend by manual changes – aniruddha Aug 03 '16 at 12:14
93

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

Marco
  • 56,740
  • 14
  • 129
  • 152
  • 33
    that works on a file basis, cannot be applied to a whole set of files. – fduff Jul 05 '12 at 09:50
  • 12
    Have you read the title of the question? Did you see the "change EOL on **multiple files**" part? – Sk8erPeter Sep 04 '13 at 20:46
  • @Sk8erPeter: if you take a look at the question, you can see its title has been edited after my question was posted... – Marco Oct 08 '13 at 15:37
  • 1
    @Marco: NO, that's not true. I looked at the revision thread before commenting, and as you can see, the ORIGINAL title AND body of the question also contained ***"on multiple files"***: http://i.imgur.com/kSYdJA9.png... – Sk8erPeter Oct 08 '13 at 19:12
  • @Sk8erPeter: but not "in one go" :D – Marco Oct 08 '13 at 20:48
  • @Marco: oh yeah, but unfortunately it doesn't change the fact that you didn't answer the question, you just wrote down how to change EOL for one single file... :D – Sk8erPeter Oct 08 '13 at 20:52
  • 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 '13 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 '13 at 18:09
  • 5
    Useful answer as this comes up first in Google – Matthew Lock Jun 23 '14 at 05:41
  • I'm sorry but don't you guys read the question? "in one go" **was originally** in the question **as also the example** given by the OP. I assume that for answering a question, you have to actually *read* the question, not only the title. Builtin "EOL conversion" feature is largely known, but how to reliable convert a set of files is a bit hard to find. – Andre Figueiredo May 14 '15 at 18:33
  • This is a completely incorrect answer to the question. The "in one go" part wasn't added in with an edit, it was there in the original post – Andy Jul 01 '15 at 10:25
  • 1
    Yes, useful answer... *to a different question* – p91paul May 25 '21 at 09:00
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

Sean
  • 8,407
  • 3
  • 31
  • 33
  • 1
    Could you explain what `(?<!\r)\n` means, especially the first `?<!` part, thanks! – WSBT Jan 08 '16 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 09 '16 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 '16 at 01:04
  • 1
    most sophisticated solution! – Philipp Michalski Sep 30 '16 at 14:01
  • 6
    Technically speaking, this is the most precise and complete answer on this page. – dotNET Mar 23 '17 at 05: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.
Wappenull
  • 1,181
  • 13
  • 19
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

Cristian Muscalu
  • 9,007
  • 12
  • 44
  • 76
  • 2
    Also works in IntelliJ IDEA Community Edition, which is free and does not require trial. [IntelliJ IDEA](https://www.jetbrains.com/idea/download). – omalyutin Apr 26 '19 at 09: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. – Stelios Adamantidis Aug 18 '23 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
R2AD
  • 31
  • 2
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

Bjverde
  • 36
  • 3
1

This did the work for me

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