\n and \r are different characters in ASCII, used for representing new line in text. Linux's and general standard is just n (line feed) character for new line, but some/most Microsoft Windows' programs (at least these old ones) use \r\n instead (where r - carriage return). They are presented this way to be "printable" and readable, but to differ them for the normal characters - escape key is used (\ in most languages, ` in AHK by default).
\n is a line feed, the standard end of line in linux
\r\n is a hard line feed or carriage return linefeed, the standard end of line in windows, hated by contrarians
\n in windows is a soft line feed often used to mark where you hit ctrl+enter
both demark the end of a line but also in many case define if the next line is part of the same paragraph, a feature less commonly used now.
Lost is the reason of the use.... in typewriters, teletypes and dot matrix printers the carriage return instructed the machine to go to the start of the line... and linefeed instructed the machine to move to the next line. a \n without a \r the machine would simply scroll to the next line
could tell the printer\n
to move to the next line and write text there\r\n
to save on the speed on the printing
you could also send\n\b\b\b\b\b
to align text with the line above if the number of backspace would save on head movement
It did not matter if the machine encountered \r\n or \n\r the result would be the same. \r\n or \n\r, resulted in same head position
long story short. replacing all \r\n with \n is something you will often need to do when dealing with text. and writing \r\n for newline is something you should always do if your code is running under windows.
- The symbol (arrow) on the Enter key is a bit misleading, it suggests down first (line feed) and left (carriage return), but Windows uses CR then LF.
- Can anyone find a good video demonstrating the physical meanings on a typewriter/other hardware? I couldn't find one.
- Notepad finally added proper support for CR and LF in Windows 10.
Introducing extended line endings support in Notepad – Windows Command Line Tools For Developers https://blogs.msdn.microsoft.com/comman ... n-notepad/
- If it doesn't work in Notepad (i.e. the enters are there but don't display), you can try WordPad. You can also try copying in Notepad and pasting somewhere else, this can reveal the hidden enters.
- A further thing to know about is word wrap in Notepad, it creates CRCRLFs. Create at least one long line, set word wrap to on, and save the file, then try ControlGetText on the Edit control.
Why does Windows Vista's Notepad randomly move the cursor after saving a file? - Super User https://superuser.com/questions/57190/w ... ing-a-file
- Another thing is that in older versions of Windows (e.g. XP but not 7), if you do Ctrl+Left and Ctrl+Right, that will stop halfway through the 'enter' (line break), between the CR and the LF. Thankfully this was later changed (I believe it's to do with the properties of Edit controls rather than anything Notepad-specific.) I ended up with what I call lone enter characters, after having used Ctrl+Shift+Left/Right and Del to delete words. (I had assigned Ctrl+Backspace/Ctrl+Del hotkeys which I still use.)
EM_SETWORDBREAKPROC message (Windows) https://msdn.microsoft.com/en-us/librar ... s.85).aspx
>Can anyone find a good video demonstrating the physical meanings on a typewriter/other hardware? I couldn't find one.
I did not realise people were so out of touch.
The carriage is what the printer head sits on. it "carries" the print head left to right... or it carries the paper roller right to left for typewriter whose head does not move. backspace moves it in reverse
a line feed is when you would roll the roller by one notch, to the next line, some printer had half notches to allow subscript and super script. this could be done turning the roler with the knobs on both sides or nudging the arm lightly on the left...
The arm on the old carriage serves as line feed and return. if you push on it more you push the carriage to the start, returning it to its original position. it does the linefeed first then your arm does the carriage return
And that there is why linefeed by itself is wrongly associated with going at the start of the next line. without the carriage returning to the start, a line feed is just a tick of the roller.
as for the carret placing itself between \r\n I do believe that may have been buggy software
the wordwrap proc... it's actually EM_FMTLINES which is interesting, I never heard of it until recently.
Last edited by icuurd12b42 on 04 Jun 2018, 04:12, edited 1 time in total.
- Thanks icuurd12b42.
- I've seen a tonne of places describe in words, carriage return and line feed, but not a simple really explicit video.
- This one was quite good, but didn't explicitly mention CR and LF from an IT perspective.
How to Use a Typewriter - YouTube https://www.youtube.com/watch?v=FkUXn5bOwzk
- You should get the caret between CR and LF on Windows XP on any multiline Edit control, by doing Ctrl+Left/Ctrl+Right. Possibly on Windows Vista, I'm not sure.
- Re. technology, I heard of a child/teen asking: why do they say 'hang up' the phone, and a video of a small kid trying to swipe a book. Technology moves fast. Bring back the slide rule.
- Thanks for the videos, I like the Ferrero Rocher that they use to change the font/typeface.
- I wrote a script so that you can set word wrap to on without losing the caret's position, by getting the current column and CR/LF count, setting word wrap to on, then setting the caret position.
- I'm not entirely sure of the EM_FMTLINES details, I was working on a Notepad clone, when I get back to it, I might need to look at it then.
Caret \r\n: Not sure. I been on windows since the 90's (and typewriters before that ). ctrl+left/right have always been next word, prev word, but I may not remember things 100% being do old and going senile... and I did have my one experience with this caret between \r \n lately with coding my edit box to code box hack, placing the caret wrongly between the \r and \n... which really break things.
Watching the video...
"Turn the carriage knob like so"... it's the roller you %^&%$....
"Every time I do a Come back over". You mean every time you do a carriage return!!!
"Line space selector" You mean linefeed selector!
Hehe. well he got all bases covered anyway.
You can see where all them word processors reflect how you set margins and tabs
- Great post Helgef, one of the best on these forums, nicely done.
- Once I had to fill in a survey that had a section with a character limit, and each time you pressed enter, the number remaining went down by 2 characters.
- Of course it's like handwriting also, in a left-to-right writing system, when you reach the end of a line: hand moves left (CR), hand moves down (LF).
- Another technology classic: 8-inch, 5¼-inch, and 3½-inch floppy disks.