EOL conversion in notepad ++
Asked Answered
E

4

140

For some reason, when I open files from a unix server on my windows machine, they occasionally have Macintosh EOL conversion, and when I edit/save them again they don't work properly on the unix server. I only use notepad ++ to edit files from this unix server, so is there a way to create a macro that automatically converts EOL to Unix format whenever I open a file?

Elfredaelfrida answered 26/4, 2013 at 15:1 Comment(2)
possible duplicate of Change EOL on multiple files in one goSaros
Your issue may be with whatever FTP program you are using. For example, I use WinSCP to remote into a Unix server, Notepad++ is set as my default editor, but I had to go into WinSCP's settings and set the transfer mode to Binary in order to keep line endings preserved. So, you may be able to reconfigure your FTP/SCP/etc program to transfer the files in a different manner.Aerospace
R
251

That functionality is already built into Notepad++. From the "Edit" menu, select "EOL Conversion" -> "UNIX/OSX Format".

screenshot of the option for even quicker finding (or different language versions)

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

Rack answered 26/4, 2013 at 19:44 Comment(6)
I know about the conversion in the edit menu, and the settings you mentioned are only for new documents. I want to automatically make the conversion for every file I open (or every file I save)Elfredaelfrida
The problem is when I open an existing Unix file, the "EOL Conversion" --> UNIX/OSX Format is greyed out. Notepad++ is to frustrating to use with UNIX files, so I use Notetab Lite which allows me to save All files as UNIX, not just new ones.Donitadonjon
@OffTheGold The UNIX EOL option is greyed out because it is the current selection.Cinder
RIght on. Thank you from November 2017 via Google.Delanie
This is no working for me. I keep selecting "EOL Conversion -> Unix", but it does nothing. I go back into the menu and see that Windows is the greyed out (therefore selected?) option, so I click Unix again. I even tried to select-all my text first in case it only works on selection, then selected Unix again. Still it does nothing.Pipsissewa
… and now I figured out why. The file had its read-only flag set (not to be confused with a lack of write permission). In Notepad++ I had to go to the Edit menu and first select "Clear Read-Only Flag", then I could make an EOL Conversion selection and it worked.Pipsissewa
P
20

In Notepad++, use replace all with regular expression. This has advantage over conversion command in menu that you can operate on entire folder w/o having to open each file or drag n drop (on several hundred files it will noticeably become slower) plus you can also set filename wildcard filter.

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

to

\n

This will match every possible line ending pattern (single \r, \n or \r\n) back to \n. (Or \r\n if you are converting to windows-style)

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.
Pamper answered 24/7, 2015 at 3:56 Comment(3)
This can cause loss of EOLs due to replacing a \r\n\r\n with a single \n. I think.Vitebsk
Confirm worked in Notepad++ 6.9.1. It will search forward, therefore not recursively replace those are behind.Pamper
In replying to: "This can cause loss of EOLs due to replacing a \r\n\r\n with a single \n" >> It will not, as it uses | (or) operator between 2 possible group.Pamper
F
3

I open files "directly" from WinSCP which opens the files in Notepad++ I had a php files on my linux server which always opened in Mac format no matter what I did :-(

If I downloaded the file and then opened it from local (windows) it was open as Dos/Windows....hmmm

The solution was to EOL-convert the local file to "UNIX/OSX Format", save it and then upload it.

Now when I open the file directly from the server it's open as "Dos/Windows" :-)

Fishgig answered 18/1, 2016 at 11:33 Comment(0)
V
1

Depending on your project, you might want to consider using EditorConfig (https://editorconfig.org/). There's a Notepad++ plugin which will load an .editorconfig where you can specify "lf" as the mandatory line ending.

I've only started using it, but it's nice so far, and open source projects I've worked on have included .editorconfig files for years. The "EOL Conversion" setting isn't changed, so it can be a bit confusing, but if you "View > Show Symbol > Show End of Line", you can see that it's adding LF instead of CRLF, even when "EOL Conversion" and the lower bottom corner shows something else (e.g. Windows (CR LF)).

Valeryvalerye answered 10/10, 2019 at 0:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.