Writing an editor

Learning Zig and writing an editor, just for fun

Return Home

Hello to anyone who's wandered into here, perhaps curious about Zig, or perhaps just curious about what goes into building a TUI application and/or text-editor. I wanted to start a new project this year, and it stemmed from my desire to learn a few new things. Firstly I wanted to learn Zig; my day job I'm working with TypeScript and I spent the last couple years working on a Rust project which I've found valuable in learning transferable concepts. Zig seems similar to Rust, but I'd wager learning a C-like language would bring useful new ideas. Secondly I've been getting a bit tired of my daily editor. As with most people I used VSCode, but when I was awarded with a low-spec laptop I moved to NeoVim. Then as I got tired of maintaining my plugin config I moved to Helix where I could get most of what I need quite painlessly. But I ended up making a fork to merge in some much-desired features. So now I'm thinking maybe it'll be a fun experiment to try and create an editor that works exactly the way I want!

Part of this was also inspired by discovery of kibi, which is itself inspired by kilo. And writing an editor in 1000 lines-of-code helps make it feel much more approachable. So this series is going to follow a similar structure but ditch the arbitrary lines-of-code limit and explore ideas that are more up-to-date for a text-editor. We'll have the following contents ahead of us.

Contents

  1. Enter raw-mode and become a TUI
  2. Read input, create a canvas, and draw to output
  3. Ropes, visitors, iterators, and other data-structures to load and display files.
  4. Creating commands and editing files
  5. Syntax highlighting using tree-sitter
  6. Language server protocol
  7. Search, child-processes, and the UNIX philosophy

Setup

One nice thing about Zig is its easy setup. Follow their guide and then run zig init in a fresh directory. As we progress, we can use zig build and zig test --test-filter "test-name" src/<module>.zig. I've also found the Zig docs, std docs, and Zig guide invaluable resources to use. I'll do my best to explain things that may be unfamiliar to users of higher-level languages, but some prior learning (e.g. with ziglings) will be advantageous.

Disclaimers

I'm not any sort of authority on writing editors, working with Zig, or writing tutorials. That being said, I hope you find the series interesting. I'll leave my understanding and learning noted throughout as well as my thoughts on Zig, so I'm sure there's a chance you'll learn something new like I have! If I make an error at any point, you're welcome to email me or raise an issue and I'll amend it (and attribute it at your request).

Another thing is the usage of AI throughout this series. I tend to avoid agents for learning exercises like this, but for transparency's sake, I ought to disclose that some code is inevitably derived from those unavoidable Google summaries and additionally Q&As I've exchange with Claude.