I made a console-based text editor in Zig

Hi folks.

I made a very minimal text editor in order to learn Zig in 10 days. It’s called zed and it’s inspired by antirez’s kilo editor. Clocking in at ~2k lines, this is a terminal editor that supports basic text editing, a clipboard, undos (no redo’s yet unfortunately), text search and line numbers. No syntax highlighting or anything else. It should run on any UNIX system, although I’ve only tested it on Linux.

Source: mntnmntn/zed: A very minimal text editor in Zig - Codeberg.org

21 Likes

The naming is a bit unfortunate: https://zed.dev/

3 Likes

Haha, I don’t really have a better name for this project. It’s an editor written in Zig, so zig-editor, but that’s probably too generic of a name. I don’t know of any good names, I’m open to suggestions.

I have renamed the project to zenith. Since the last update I have added UTF-8, a shortcut to duplicate lines, redoing, text replacement, indenting and dedenting, and a lot of changes under the hood.

My plan is to turn zenith into an actual fully fledged text editor. I’m probably going to add syntax highlighting and custom user config. For syntax highlighting, I’m not sure if there are any good regex implementations yet, so I’m gonna implement a custom pattern matching language inspired by Lua’s patterns to match source tokens. I’m gonna experiment with JSON5 as the format for the config file. It seems easy to parse and there are implementations in other languages.

2 Likes

From what I’ve read and seen in videos related to Neovim, I get the impression that Treesitter is to editor developers what LLVM is to compiler developers. Have you considered integrating it into your editor? I understand it handles indenting, highlighting, formatting and a slew of other stuff.

2 Likes

I have heard of treesitter before. It seems like it performs tokenization and grammar parsing not unlike an LSP library, if I understand it correctly. Although it looks way too complicated to be included in a minimal text editor, and it does a lot more than what is needed for highlighting.

From what I have heard it also has some level of support of handling code that is partially broken, which is especially helpful for editors.

1 Like

Once you have written your own pattern matcher and a whole bunch of patterns for different languages you’ll have at least as much complexity as tree-sitter. The tree-sitter core library is actually pretty tiny and very much ideal for a minimal text editor.

Flow’s syntax highlighter uses tree-sitter and is about 100 lines of code. You can have a look here.

I highly recommend you start off with tree-sitter and spend your valuable time and effort adding features to your text editor that actually make it unique and useful.

5 Likes

These two seem to be a little bit of a contradiction, I don’t intend to argue either way, go with what you want to do. (Although keeping things both minimal and highly functional is also an admirable and worthy goal, so maybe they don’t necessarily contradict)

It sounds like you want to reinvent and design everything from the ground up and I think that this especially makes sense, if it is also a project for personal, learning and research. (But doing a lot of research can eat up time quickly)

Overall I am not sure, if it really needs to be completely “fully fledged”, seems more important that it does a sizeable number of things in a very excellent / satisfying way.
Things like vscode basically do everything, but a lot of things in not particular good ways. I think something more geared towards minimalism can shine, by focusing on quality over quantity, even if the quantity slowly increases over time too.

So personally I wouldn’t focus on the “fully fledged” too much, that said I am curious what you will come up with!

2 Likes

So I’ve implemented the LPeg VM in Julia, the project is feature-complete with respect to LPeg, with a few flourishes of my own, I have a ways to go before I’ve added everything I want it to have.

Lately I’ve been reading up on Zig for related reasons, and I’m coming around to the idea of translating this engine into Zig to have a nice fast linkable library. That will be at its own pace, but the result should be quite usable for syntax highlighting, among many other tasks.

mkrieger1

Hello, don’t take offense at what I’m going to tell you:
I find the work accomplished great, but there is a but, too much dependence, the advantage of zig is that you can do everything in a zig, sometimes you have to put in a lot of elbow grease, but frankly it’s worth it, believe me, because I have the same approach as you, my project allows me to tour the owner (Zig) and I realize that zig goes much further than my needs. .