Maintaining Notepad is not a full-time job, but it’s not an empty job either

When I mentioned that one of my now-retired colleagues was responsible for maintaining Notepad, commenter Neil noted, "I can't imagine theres much actual maintenance."

You'd be surprised.

For one thing, Notepad is a common guinea pig. If a team is adding a new feature, they may ask Notepad to support the feature, in order to get some feedback on how well the feature works in a "real program", rather than a unit test.

For example, did you know that you can type into Notepad's File Open dialog?

Of the various improvements he made to Notepad, there are two that stand out in my memory.

First was the Status Bar. There is no obvious way to be notified when the caret moves within an edit control, but my colleague figured out a way: Notepad registers for its own object location change accessibility notifications. That way, it learns when the caret moves and can respond by updating the status bar with the caret's current location.

Second was how he got Notepad to load files so quickly. When he took over ownership of Notepad, the program read the file into memory before handing it to the edit control. He kept finding little shortcuts to shave off some time here, offload some work there, and when he was done, Notepad had gotten completely out of the file-parsing business. To load a file, Notepad maps a view of the file as a memory-mapped file and uses that as the source. The code figures out the encoding, performs a code page conversion to UTF-16LE if necessary, puts the result in a memory block, and then uses the EM_SET­HANDLE message to hand that entire block to the edit control.

Notepad beats them all.

He was particularly proud of the fact that he improved the performance of loading a file into Notepad by deleting lots of code from the "Load a file" function.

Comments (69)
  1. henke37 says:

    How appropriate timed, I hear that notepad finally gets support for other newline styles.

    1. Anon says:

      People who edit Unix files in Notepad have issues other than the \n vs \r\n. Unicode byte order marks for example, or the way that Notepad tries to guess text encoding. If you want to edit a shell script that has to be pure ASCII with no BOMs and with a specific newline style, use a third party tool that lets you force all those things to be done the way you want rather than a built in OS application that tries to hide that stuff from the user.

      1. Harry Johnston says:

        Mostly if I open something with notepad, I just want to look at it, so this is going to be a significant improvement for me.

      2. Mark Puddephat says:

        Anyone hear of using vi to edit shell scripts? Worked for me for 20 years so far.

    2. Timing was pure coincidence.

    3. Neil says:

      I failed to imagine that various improvements were actual maintenance.

  2. kantos says:

    Deleting code to make things better or faster is always a treat. I particularly enjoy doing this when converting code to be more conformant and safe. The feeling when the unit tests pass and everything is gold and you can commit the change and see that massive negative diff: Priceless.

    1. Simon Clarkstone says:

      “Delete Code” is my favourite refactoring too. A pity it’s so rare I get to use it.

    2. Nico says:

      A favorite story of mine from the Macintosh development days is when Bill Atkinson turn in a “progress report” showing -2000 lines of code as his weekly LoC productivity.

      Sometimes less really is more, especially in programming :)

    3. cheong00 says:

      It makes your inner self cry when you found that you have to add back those “dirty hacks” one by one in order to satisfy those unit tests (and make the code behaves correctly/expected)

      This appears a lot in places like payroll calculations, where within say a generic payroll calculation function, some of your clients are special snowflakes and add their own custom logic in calculation flow, so you need to add them too to produce “correct” result.

      1. kantos says:

        I’ve run into this a few times, generally I’ve been able to rewrite the code in a standards compliant way later making sure to comment what behavior I’m emulating very clearly. I’ve had to do this at least once because code ‘broke’ when the compiler changed.

  3. Martin says:

    My notepad loads 3.5MB big file 8 seconds. I tested it several times, so the file was most probably in disk cache. Tested on windows 7 x64 with SSD.

    1. Eliot says:

      Word wrap slows it down insanely.

      These are based on random plant text log files I had floating around:
      For a 23mb file with word wrap on: 8s
      For a 23mb file with word wrap off: Instant
      For a 70mb file with word wrap on: 32s
      For a 70mb file with word wrap off: 0.5s

      Opening a 3.5mb exe file with Notepad with word wrap off: 3.5s
      I assume it takes longer as it has to do something with the encoding?

      1. Notepad loads the entire file into memory. If you have word wrap off, it just needs to render a list of lines, which is really cheap. If you have word wrap on, then it has to calculate the dimensions of every line, find the soft wrap mark, split the line, calculate it against the new line, and repeat over and over again. This is expensive and Notepad doesn’t do this lazily; it calculates it across the whole file since it doesn’t load contents dynamically as you scroll (which makes sense, since you’re looking at text, not bytes).

  4. xcomcmdr says:

    Funny that. Since forever I avoid Notepad because it hungs up for very long on very large files. Instead, I use Notepad++.

    I guess Notepad++ does not load the whole file at once, but rather loads and parses the on-screen section on the fly.

    1. Mark says:

      Same here. I’m not sure the loading speed is anything to be particularly impressed by on files of any size. (And my primary use for Notepad is to open log files – they’re usually of a size where Notepad will take its own sweet time opening one.)

    2. DWalker07 says:

      I have some 2GB-sized files that, unfortunately, I need to look at from time to time. Notepad++ seems to load files larger than Notepad can, but for the really big files, I have to use one of the large file viewers… such as the one named Large File Viewer.

    3. LucasA says:

      Notepad will hung if you enable the Word Wrap option. Try to disable word wrapping and file loading will be faster.

      1. xcomcmdr says:

        It’s still very slow, whereas Notepad++ opens a 500 Mb log file instantly.

  5. The MAZZTer says:

    In my experience the url thing “just works” even with legacy apps. I think the Open File Dialog itself implements it. IIRC the file ends up in IE’s Cache which is then the file returned to the application, so I’ve assumed it’s completely transparent and the app doesn’t need to care.

    Also, the dialog window remains open while presumably downloading the file, since it can’t close until it has a filename to a completely downloaded file to return and trick the app with.

    Is this article prompted by the upcoming new Notepad feature to support Linux/Mac line endings? Can’t wait. That bit me today on a Windows 7 system,

    1. The MAZZTer says:

      Also, was using a Win3.1 VM the other day and saw the dreaded “This file is too big for Notepad” message. The file was 145kb in size. We, along with Notepad, have come a long way.

      1. Harold H20 says:

        “was using a Win3.1 VM the other day”

        Are you sending messages to time travelers from 1990?

        1. ender9 says:

          Windows 3.1 was actually supported until 2009 IIRC (and there’s still a lot of automototive stuff that only runs on it).

          1. DWalker07 says:

            Windows 3.1 embedded, through the OEM channel, was distributed until 2008. Windows 3.1 (non-embedded) was only supported until 2001.

      2. Getting Windows 3.x or older to run in VMs has been very finicky in my experience. More often than not I experience system hangs. It seems interrupt/mouse-related, but it’s hard to tell, and I’m not intrepid enough to hook up a debugger and find out.

        1. Old operating systems don’t fare well in modern VMs because the hardware being emulated is TOO NEW. What’s USB? What’s ACPI? What’s the year 2000?

          1. xcomcmdr says:

            DOSBox is near perfect for Windows 3.11 games and apps however.

            Now if only it could work with SHARE.EXE

          2. Joshua says:

            USB? Doesn’t matter. The VM Keyboard and Mouse are PS/2. (At least I think it’s PS/2. All I know is LMOUSE.COM doesn’t have an issue with it.)

            ACPI? If the OS ignores it it’s OK.

            Year 2000? I slipstreamed the patch into the 3.1 install CD.

            Now if it emulated a SATA CDROM I’d be in trouble.

          3. Patrick says:

            I have Win98 and NT4 running in VMs just fine here.
            Even with networking (virtual 3c509 or pcnet32 NICs) – though I haven’t been able to find any browser that works in Win98 and supports any modern SSL/TLS standards so I can’t use it for browsing random sites :(.

            But like another commenter said, you might very well be better off with Dosbox or similar, atleast for OSes that use a lot of 16 bit code.

      3. Anon says:

        IIRC 16 bit Notepad used a 16 bit edit control. Which had a 64KB limit.

        1. CN says:

          More importantly (or recently), even 32-bit Notepad on Win9x used Win9x style 16/32-bit thunked GDI/USER, so a 16-bit limitation on the edit control size prevailed there as well.

    2. skSdnW says:

      I assume the “UseUrl” value might apply here and apps can opt in to getting the raw URL without a cache file.

  6. mikeb says:

    The “Notepad beats them all” link raises the question of why mapping a file into memory is able to read en exclusively locked file when accessing it normally cannot. The stack overflow question & answer seem to indicate that the actual open of the file is the same. Both the working and non-working C# code calls `FileStream( processIdPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)` to open the file – the file mapping happens after that in the working example.

    I would have thought that attempting to open a file that has an exclusive lock would fail – you wouldn’t even get to the point of being able to create the memory mapping.

    So the question I’m left with is why does mapping the file allow the exclusive lock to be by passed?

    1. Harry Johnston says:

      There are two different kinds of locks. You can specify “no sharing” when opening a file, and that prevents other processes (including notepad) from opening it. Or you can lock a specific part of a file that you have open, but this doesn’t prevent other processes from opening the file. Memory mapping only bypasses the second kind, what that answer calls “range-based locks”.

    2. skal says:

      The file wasn’t opened with exclusive access but a range in the file was locked for exclusive access by a process (see his example Java code).

      You can accomplish the same in C# using FileStream.Lock method:

    3. Ray Koopa says:

      Isn’t that what FileShare.ReadWrite is for?

    4. dead1ne says:

      I would assume the reason range based file locking is not supported on memory mapped files is for performance reasons. The only way to catch the accesses without blocking the whole file would be to use guard pages. Then every CPU read/write anywhere in the entire page would cause a context switch to check the address. Say the file is a database of records each of which consist of 32 ints. Reading an unlocked record in the same page as a locked record would cause 64 context switches.

    5. florian says:

      I’m surprised to read this, yet the same is stated on MSDN for LockFile[Ex].

      I’ve always had a mess with file locking on Windows. I remember, when I was … younger (I have to put it like that, because somehow I can only feel the “ancient” part from my “ancient wisdom”), I’ve used a PHP script to test my programs with locked files.

      Then I believed that as long as you throw of the FILE_SHARE_* flags CreateFile, you can read everything (that is not exclusively locked). My mental model was that LockFile[Ex] would just “temporarily” modify the dwShareMode set by CreateFile (or, the other way round, that CreateFile internally uses LockFile[EX] to adjust for dwShareMode).

      But with today’s news about file mappings, I’m back on field one. Can somebody “share” (ah, not “share”, too confusing, just write down) a link to informative documentation on the topic?

      On the other hand, there’s plenty of locked files in my %LOCALAPPDATA% directory, and I can not read any one of them with Windows Notepad, that I could not also read with other applications, and Windows Notepad can’t read most of them, either, so it seems that at least reading from a file mapping won’t bypass exclusive locks?

      1. Harry Johnston says:

        It’s not all that complicated. The only reason you’re confused is that you’re trying to pretend that the sharing mode and range locking are the same thing really, and they’re not. They’re independent mechanisms, implemented in different ways and used for different things. In short:

        The sharing mode controls whether or not other processes can open the file. Typically it is used to make sure that no unrelated process messes with the file while you’re using it.

        Range locking determines which parts of the file other processes can read from or write to, except that it does not affect access via a memory mapping. Typically it is used to coordinate access to the file between cooperating processes.

        1. florian says:

          Thank you very much for your comments!

          I *mean* to remember having seen a lot of code to open a file with restrictive sharing flags, and still call LockFile before reading and writing.

          So this is not compelling: if you want to exclusively open a file, the restrictive sharing flags will do.

          But, you’re a “better citizen” if you open a file with permissive sharing flags, and lock it only during IO, so others can still open and read it (at least using file mapping). And you can also open files from others with permissive sharing flags (fails if either party is using restrictive sharing flags).

          Calling ReadFile always gave me the impression of “atomicity” (indivisibility), i.e. once you have a block of data copied from the OS file buffer to your own, nobody can touch it, any more (you may still end up with inconsistent data if reading in between partial writes by others).

          But what about file mapping? What happens if Windows Notepad is iterating the bytes of the file mapping, i.e. to detect the encoding before converting-on-copying to its own UTF-16 buffer, while someone else is writing to the file? Does Windows Notepad put a (shared) lock on the file? But on my systems, neither of the 3 copies of notepad.exe seems to import LockFile … (may be using some CRT-imported locking functions, but probably not).

          A very interesting topic, but confusion increasing, again …

          (This post written in Notepad. Bacardi feeling.)

          1. Harry Johnston says:

            Ordinarily we would expect the process that was writing to a file to have opened it for exclusive access, in which case notepad won’t be able to open it. If the other process doesn’t do this then, yes, notepad might get confused. We can be sure notepad doesn’t use range-based locking because otherwise it wouldn’t be able to open files with existing range-based locks. Plus, I checked with Process Monitor. :-)

            Unless you have a specific reason for using range-based locking, then IMO sharing flags are preferable. For one thing, range-based locking is liable to confuse other applications that aren’t expecting it. Typically, range-based locking should only be used if the file format is specifically designed to be used by multiple processes simultaneously, e.g., an Access database.

          2. florian says:

            Thank you again for your helpful comments.

            Maybe 20 years ago, when testing my programs with locked files for the first time, I needed a test program to acquire the lock. Unaware of LockFile[Ex], or unable to use it, I resorted to PHP’s flock() — and that was like black magic. And so was the whole topic, as I haven’t come across it in depth, since then, somehow.

  7. Gary Keramidas says:

    I think it’s still too slow loading large lof files. oi wish it would load enough to show in the window and then continue loading the rest in the background

  8. Harold H20 says:

    “did you know that you can type into Notepad’s File Open dialog? ”

    Why would you want to do that?

    I tried that on a third-party text editor that I use and it opened the HTML source for that page. I’m not sure if that is a good or bad thing.

    1. ender9 says:

      I use it to directly insert images from the internet into documents.

      1. Harold H20 says:

        Can you insert images into a notepad document?

        Being able to put “” into Notepad’s File Open dialog would make sense, but just a general URL, seems weird and rather pointless.

        1. If it’s got content, you can read it!

  9. “Did you know that you can type into Notepad’s File Open dialog?”\

    As far as typing is concerned, I can type anything there. But does any good come out of it? Certainly not.

  10. Joep Lijnen says:

    Perhaps this is not the place to put this comment, but please, make re-do and undo in notepad more usable/robust;
    I use notepad mostly as a temporary copy-paste location, I daren’t actually edit anything because two keystrokes after an accidental undo, you permanently lose the prior text; re-do has an absolutely archaic one-shot approach.

    1. Erik F says:

      Considering Notepad is just a wrapper around the edit control, that’s what you get: a single-level undo buffer. If you want multi-level undo, use Wordpad.

  11. Phil says:

    I always said that the most significant new feature in Windows 2000 was adding CTRL+S support to Notepad

    1. BZ says:

      Yes, agreed 100%

  12. jrivett says:

    That’s all very interesting, but I’m still waiting for fix for a rather annoying bug in Notepad.

    To see this bug in action, open a file in Notepad, go the end of the document and add some text, then press Ctrl-S to save it. The cursor will jump back several characters.

  13. Azarien says:

    `For example, did you know that you can type into Notepad’s File Open dialog? ` – and what is it supposed to do? all I get is out of memory error.

  14. fys says:

    show line number

  15. Anoni says:

    Notepad is clearly insufficiently documented.

  16. Sorry, but when you compare Notepad to Notepad++ anyone can see how immature and inadequate Notepad is. Notepad should have evolved into Notepad++. That’s one of the major things wrong with Windows and Microsoft’s development approach – Many things do not evolve as they should! There are so many areas that Windows has not evolved over the last 20 years that I’m compiling a very long list and will publish it on the web in the future. Some of us have high standards and want something better and something more. Windows has been very disappointing in this regard. Time for improvements and/or time for something better.

    1. Ben Voigt says:

      I think you’ve confused Notepad with “a program intended to be a good text editor”. Notepad isn’t intended to be good, it is intended to (as far as possible) still work no matter how screwed up your Windows installation is, so you can read the log file from your fancy text editor that crashes on startup, and figure out how to unscrew the nice editor.

      1. DWalker07 says:

        Exactly. Saying that Notepad “should” have evolved into a fully-featured text editor, or into one specific text editor, is wrong in my opinion. Notepad++ does a lot of different things (including syntax coloring for dozens of programming languages), and has plug-in capability, and much more.

        Any company can provide a program like that, and many do, and some companies sell those things. Microsoft should concentrate its resources on things that only the Operating System provider can do, rather than (for example) building the world’s best text editor or image viewer or music playing program.

  17. MWM says:

    It is great to see such a useful tool get a little love!

    Now if only the person maintaining Paint would optimize the code that takes (sometimes minutes) to change a Font, or Character size. Even launching Photoshop is faster than using Paint if you want to add some characters to an image. [Hint, move the cataloging of Fonts to the background just after Paint starts up and this delay will never been seen.]

  18. I did not know you could load from a URL… Notepad may be come my new wget replacement.

    1. Danny says:

      @Ward Durossette
      Notepad uses the system open dialog which is explorer at its core. You can do that in any open dialog of any program that uses the standard one instead of a custom one. And avoid doing that please, it’s the IExplorer full of security holes that nobody uses. Or I dunno, maybe you like to have a ransomware/crypto-miner on your system, so do it then :P.

  19. Troy says:

    A huge thank you to your team member. I use notepad all the time for small files because it is still truly faster than anything else for quick edits. I didn’t realise how much effort had been put into that, but it was worth it and as a user I appreciate it. For all the whining about what notepad does or doesn’t do, you can pry it from my cold dead hands. Other editors are useful, but so is notepad.

  20. Br. Bill says:

    It *sounds* really awesome, but anyone who’s loaded a 200 MB log file into Notepad knows, it’s really slow and lousy at doing what you just claimed it’s fast at.

  21. I can’t tell you how many times I lost data because notepad doesn’t persist after a reboot. I started using the Sticky Notes app instead, but still….why doesn’t notepad have this feature?

    1. Maggan says:

      How can that be? It’s even preseting you a modal popup that’s preventing windows to shutdown when told so.

  22. Z says:

    However, I can type into any text editor’s File Open dialog, and they all behave the same: download the URL content and open it.
    Is this feature provided by the File Manager?

  23. Maggan says:

    Shadow copy the file before opening it would have solved the locking problem in a more consistent way, would have worked even when there’s an exclusive lock on the file.

Comments are closed.

Skip to main content