@comingstorm is basically right. To display, you start with the cursor and scroll back until you are sure that you are behind the top of the screen. Then you scan back to the end of the line, assuming that you can identify the scan of the end of the line back. Now you are looking ahead, calculating and saving the starting positions of the screen line until you go far enough. Finally, you select the line that you want to start displaying and turning off.
For plain text, this can be done on an archaic processor fast enough to redraw the video image displayed on the map at each key press. [I invented this technology 30 years ago]. The right way to do this is to fix the cursor in the middle line of the screen.
For actual file changes, you can look at using Gnu ropes. A rope is basically a linked list of buffers. The idea is that all local changes can be made in only one small buffer, sometimes adding a new buffer and mixing neighboring buffers from time to time.
I would think about combining this technology with differential storage: this is what all modern version control systems do. Basically there is to use this kind of transaction-based editing if you want to implement the undo function.
The key to this is a reversible transaction, that is, one that contains enough information to apply back to reverse what it did when applied forward. The main transaction of the editor:
at pos p replace old with new
which has the opposite
at pos p replace new with old
This handles the insert (the old one is empty) and deletes (the new one is empty) and also replaces. Given the list of transactions, you can undo changes in place in the row by applying the opposite to the list of reverse transactions.
Now you use the concept of the old checkpoint: you save a fairly recent modified image in place, along with some recent transactions that have not yet been applied. To display, you apply transactions on the fly. To cancel, you simply throw away some transactions. Sometimes you actually apply transactions to create a โcheckpointโ image. This speeds up the display by reducing the undo time.
Finally: to rewrite a huge text file, you usually rewrite all the text, which is terrible. If you can fool a bit and allow arbitrary 0 characters in the text, and you have access to the virtual memory system manager and access to a low-level disk, you can do much better by preserving all the unchanged pages of the text and simply rebuilding them: in other words, the idea ropes on the disk.