Last line has CRLF or not?
-
Last line has CRLF or not?
Hi,
Should the last line of a text file have the end of line marker e.g. CRLF or not? I tend to think that each line should be the same, and each should have CRLF. I’ve seen a few programs complain if it’s missing.
If you have only two text lines and CRLF on each, then N++ shows you the line number for empty line 3, and 3 in the status line count. How many lines in the file? I think two, clearly N++ thinks three.
If you sort that file, the 3rd line goes to the top, and the new 3rd line has text and no CRLF.
If you don’t have the CRLF on the last line of files, and you join two files together, the last line of the first, and first line of 2nd become one line. This confirms my idea that all lines should have CRLF.
I think things would behave more logically if the last line always has CRLF, N++ warned you if it’s missing, and N++ ignored the empty last line (that I think isn’t there :) ) for counting and sorting.
(I’m sorry to seem slightly critical on my first post. I think N++ is fantastic, use it all the time.)
Richard
-
Should the last line of a text file have the end of line marker e.g. CRLF or not? I tend to think that each line should be the same, and each should have CRLF.
My opinion FWIW is that a non-empty last line should have a proper line-ending (be it CRLF for Windows files, or LF for Linux files). “Non-empty” is kind of a joke here, because if it is empty there is no last line, it’s the line above!
I feel strongly about this, in fact I have a script that runs upon file-save to put a line-ending on the last line if it is not present.
I’ve seen a few programs complain if it’s missing.
Programs that take such a file as input can complain if it is missing, although I think a great many follow Postel’s Law and will assume it (which is a pretty logical thing to do in this case).
A good many editors I’ve used have a configuration parameter for this; example:
Notepad++ sadly does not have this. I would not mind at all if such a config setting was added to Notepad++.
If you have only two text lines and CRLF on each, then N++ shows you the line number for empty line 3, and 3 in the status line count. How many lines in the file? I think two, clearly N++ thinks three.
Hmmm. Don’t know if I completely agree. Notepad++ may simply be giving you space for you to put the future line 3, and is conveniently numbering your as-yet-non-existant line. Same with the status bar, if your caret is on line 3 anything you now type will be on line 3… View (menu) -> Summary… may be a different story, though, as it reports “Lines: 3” instead of “2” for your example.
If you sort that file, the 3rd line goes to the top, and the new 3rd line has text and no CRLF.
This definitely looks like a problem:
Before #1:
After #1:
This may be related to a behavior of the various Edit (menu) -> Line Operations that I don’t like; if you include the line-ending on the last line of a selection, so that the caret is at the very beginning of the following line, the operation results in something you often don’t expect–the line following is included in the action, even though none of it was selected!
In your example you are operating on the whole file (you have no selection), but to cleanly sort such a case it is best to select text first (don’t include the line-ending on line #2), then sort, like this:
Before #2:
After #2:
This gives the desired result, but it is kind of sad to have to do it that way…
If you don’t have the CRLF on the last line of files, and you join two files together, the last line of the first, and first line of 2nd become one line.
If the last line of file 1 has no ending CRLF, and you combine it with file 2, I would expect the lines to run together–the computer is just doing what it is told to.
This confirms my idea that all lines should have CRLF.
You’ve already made your case, a few times over. :-D
The best place to report the sorting problem, and to make an enhancement request concerning this line-ending topic in general isn’t this forum, it’s here: https://github.com/notepad-plus-plus/notepad-plus-plus/issues/ . I encourage you to follow up and make this a real issue for the Notepad++ developers to consider.
-
Scott,
Thanks for thinking about this and trying the sort. Yes, I’ll follow it up on the github link.
Richard.
-
@Scott-Sumner said:
I have a script that runs upon file-save to put a line-ending on the last line if it is not present.
Here’s the Pythonscript I mentioned in case there is any interest. Set it to run at startup via
startup.py
.I call it
LineEndingAtEOF.py
:def callback_notepad_FILEBEFORESAVE(args): curr_buffer_id = notepad.getCurrentBufferID() notepad.activateBufferID(args['bufferID']) line_ending = ['\r\n', '\r', '\n'][notepad.getFormatType(args['bufferID'])] if not editor.getText().endswith(line_ending): editor.appendText(line_ending) notepad.activateBufferID(curr_buffer_id) notepad.callback(callback_notepad_FILEBEFORESAVE, [NOTIFICATION.FILEBEFORESAVE])
-
I found that SOMETIMES it is useful to NOT put the line-ending in at end-of-file. Thus I added in the functionality to detect if the ALT key is down at the time of save–if it is then the file is saved as-is. This is best used by holding down ALT while pressing the Save toolbar icon (holding ALT while you press the CTRL+S shortcut clearly isn’t going to work, nor will trying to invoke File (menu) -> Save). Here’s the revised code with this new feature:
import ctypes def callback_notepad_FILEBEFORESAVE(args): VK_MENU = VK_ALT = 0x12 if (ctypes.windll.user32.GetKeyState(VK_ALT) & 0x8000) != 0x8000: curr_buffer_id = notepad.getCurrentBufferID() notepad.activateBufferID(args['bufferID']) line_ending = ['\r\n', '\r', '\n'][notepad.getFormatType(args['bufferID'])] if not editor.getText().endswith(line_ending): editor.appendText(line_ending) notepad.activateBufferID(curr_buffer_id) notepad.callback(callback_notepad_FILEBEFORESAVE, [NOTIFICATION.FILEBEFORESAVE])
-
@Richard-Lawrence said:
Scott,
Thanks for thinking about this and trying the sort. Yes, I’ll follow it up on the github link.
Richard.
Here’s the github issue Richard created: https://github.com/notepad-plus-plus/notepad-plus-plus/issues/3906
-
I understand that it is good that npp is putting only those things in the text file that we type out or bring to it.
a good editor should not anything on its own, for whatever good reason.
however, I also face problem due to last line NOT HAVING CRLF.
when I copy the text to a firefox window and run foxreplace on that, foxreplace doesn’t find CRLF on last line, so it seems it foxreplace leaves that line unprocessed, and I have to check and do that change manually.due to this problem, I take care to check whether CRLF is there on the last line before I take the text to foxreplace, and add CRLF, if it is not there.
the suggestion above is good that npp should give a settings option like above.
thanks.
-
Hi guys,
Strictly speaking the CRLF (or LF) is actually a delimiter between lines. You can think of it as a caret positioning symbol. I agree that it is useful to have the last non-empty line have line termination (CRLF) or put in other words - to have an empty last line and I always do that manually but it is definitely not necessary. It can even be bad in some cases. For example if you want to use a text file as input to some process’ pipe the last LF might be a problem.
BR