Take a look at Rob Pike's description of his Sam text editor. Be sure to browse past the high-level overview and command language. He describes parsing, memory management, and data structures later in the paper.

In addition, take a look at Russ Cox's simple regular expression implementation. It's easy to follow and may open some doors outside existing regular expression libraries.


Over the years I've written quite a number of different text editors. Certainly the simplest way is to manage a long sequence of characters, where you copy everything around to insert any character. Other techniques that I've used include:

  • Represent the text file as a doubly linked list of text lines.
  • Construct a tree-like data structure (sometimes called a "rope") which starts off as a solid string of characters, but has the ability to split, insert, and delete blocks of text without having to move all the rest of the text around.

Many of the old Borland example books used a text editor as a tutorial example. You can occasionally still find copies of these at used bookstores for nearly free.


There's an excellent tutorial available here that covers a lot of relevant topics in a more modern context:

  • Design and Implementation of a Win32 Text Editor

The other answers to this question cover gap buffer.

Another modern coverage is the descriptions of AvalonEdit

  • http://www.codeproject.com/KB/edit/AvalonEdit.aspx

and extra detail from:

  • http://wiki.sharpdevelop.net/AvalonEdit.ashx
  • http://danielgrunwald.de/coding/AvalonEdit/document.php
  • http://danielgrunwald.de/coding/AvalonEdit/rendering.php

and there's a huge amount of detail/content (about SharpDevelop) in the book:

  • http://www.icsharpcode.net/opensource/sd/insidesharpdevelop.aspx

Promoted to answer by request:

The antique "Software Tools in Pascal" by Kernighan and Plaugher implements the ed editor in a language with neither real strings nor pointers. It contains a great overview of the design considerations that underlie any text editor.