When you press the Enter key on Windows computers, two characters are actually stored: a carriage return (CR) and a line feed (LF). The operating system always interprets the character sequence CR LF the same way as the Enter key: it moves to the next line. However when there are extra CR or LF characters on their own, this can sometimes cause problems.
There is a bug in the Windows XP version of Notepad that can cause extra CR characters to be stored in the display window. The bug happens in the following situation:
If you have the word wrap option turned on and the display window contains long lines that wrap around, then saving the file causes Notepad to insert the characters CR CR LF at each wrap point in the display window, but not in the saved file.
The CR CR LF characters can cause oddities if you copy and paste them into other programs. They also prevent Notepad from properly re-wrapping the lines if you resize the Notepad window.
You can remove the CR CR LF characters by turning off the word wrap feature, then turning it back on if desired. However, the cursor is repositioned at the beginning of the display window when you do this.
Update 11/14/2010: If you are familiar with a hex editor, you can modify one bit in notepad.exe to prevent Notepad from inserting CR CR LF characters at wrap points.
Any place where the Enter key was pressed still has the normal CR LF characters.
The CR CR LF characters are only in Notepad's display window. The CR CR LF characters were not saved in the text file. The long wrapped lines were saved unbroken.
After saving with word wrap on, the cursor may move, long lines may be re-wrapped, and Notepad may not repaint its window properly.
When turning on word wrap, Notepad wraps all long lines in the display window without inserting any new CR CR LF sequences.
When turning off word wrap, any sequence of CR CR LF characters in the display window are removed.
If word wrap is turned off when saving a file, all characters in the display window are saved.
If word wrap is turned on when saving a file, any sequence of CR CR LF characters in the display window are removed before the file is saved. After the file is saved, all the long lines in the display window are re-wrapped and new CR CR LF sequences are inserted at the wrap points.
After saving with word wrap on, the cursor may move. The number of characters from the beginning of the display window to the cursor position remains the same before and after the save; each CR and LF character is counted as a separate character. If the removal and insertion of CR CR LF sequences aren't exactly the same, then the cursor will move.
If you are saving a new Untitled window for the first time, or if you use the Save As command to save, then Notepad repaints its display window.
If you are re-saving an existing file, then Notepad doesn't repaint its window. This is particularly confusing if the lines were re-wrapped. One way you can force the display to repaint is by switching to another window then switching back to Notepad.
When moving the cursor using the arrow keys or by clicking the mouse, Notepad treats CR LF and CR CR LF sequences as indivisible units. But when a file is saved when word wrap is on, Notepad may end up repositioning the cursor in the middle of one of these sequences. If you start typing, you may end up creating stray CR or LF characters which appear as a box.
In the following example, normally invisible characters are shown in pink. A centered dot represents a space, a left arrow represents a CR, and a down arrow represents an LF.
Here are some relevant AutoHotkey scripts.
The following AutoHotkey script will show the contents of the clipboard in a system tray balloon whenever the clipboard is changed. Control characters are decoded; for example, CR appears as ^M and LF appears as ^J.
This script is useful to experiment with the way Notepad behaves by copying text from Notepad to see what the invisible characters are.
#Persistent OnClipboardChange: x := Clipboard n := StrLen(x) out := "" Loop, %n% { c := SubStr(x, A_Index, 1) a := Asc(c) if( a < 32 ) { b := Chr(64 + a) out := out . "^" . b if( a = 10 ) { out := out . "`r`n" } } else { out := out . c } } TrayTip ,Clipboard contents,%out% return
This AutoHotkey script will remove any CR CR LF sequences and other stray CR and LF characters from the clipboard whenever you press Ctrl+C or Ctrl+X in Notepad. Only CR LF sequences will remain unchanged.
This script may be helpful if you often use Notepad with word wrap on, and want to copy long lines without the CR CR LF characters.
(Update 11/14/2010: If you are familiar with a hex editor, you can instead modify one bit in notepad.exe to prevent Notepad from inserting CR CR LF characters at wrap points.)
#IfWinActive ahk_class Notepad ^c:: Send ^{Ins} ClipWait Gosub CorrectClip return ^x:: Send +{Del} ClipWait Gosub CorrectClip return #IfWinActive CorrectClip: x := Clipboard n := StrLen(x) out := "" cr_count := 0 Loop, %n% { c := SubStr(x, A_Index, 1) a := Asc(c) if( a = 13 ) { ++cr_count } else if( a = 10 ) { if( cr_count = 1 ) { out := out . "`r`n" } cr_count := 0 } else { out := out . c cr_count := 0 } } Clipboard := out return
Fix Notepad's CR CR LF bug with a hex editor
bavih.blogspot.com/2010/11/fix-notepad-bug.html
Follow up post that shows how to modify notepad.exe with a hex editor to prevent Notepad from inserting CR CR LF characters at wrap points.
AutoHotkey
www.autohotkey.com
A keyboard shortcut, macro, and script processor for Windows.