7 ms·
Kilo: A text editor in less than 1000 LOC with syntax highlight and search
- thomasdziedzic 1y agoHow timely, I just finished going through a tutorial that builds a text editor like kilo from scratch: https://viewsourcecode.org/snaptoken/kilo/index.html https://viewsourcecode.org/snaptoken/kilo/index.html Would highly recommend the tutorial as it is really well done.
- ok_dad 1y agoHere’s a second recommendation for that tutorial. It’s the first coding tutorial I’ve finished because it’s really good and I enjoyed building the foundational software program that my craft relies on. I don’t use that editor but it was fun to create it.
- stevekemp 1y agoI remember that tutorial fondly. I played around with kilo when it was released, and eventually made a multi-buffer version with support for scripting with embedded Lua. Of course it was just a fun hack not a serious thing, I continue to do all my real editing with Emacs, but it did mean I got to choose the best project name: https://github.com/skx/kilua https://github.com/skx/kilua
- timw4mail 1y agoThis is one of my favorite moderate-level projects for playing with different programming languages. The original in C: https://git.timshomepage.net/tutorials/kilo https://git.timshomepage.net/tutorials/kilo Go: https://git.timshomepage.net/timw4mail/gilo https://git.timshomepage.net/timw4mail/gilo Rust: https://git.timshomepage.net/timw4mail/rs-kilo https://git.timshomepage.net/timw4mail/rs-kilo And the more rusty tutorial version (Hecto): https://git.timshomepage.net/tutorials/hecto https://git.timshomepage.net/tutorials/hecto PHP: https://git.timshomepage.net/timw4mail/php-kilo https://git.timshomepage.net/timw4mail/php-kilo ...and Typescript: https://git.timshomepage.net/timw4mail/scroll https://git.timshomepage.net/timw4mail/scroll
- pflenker 1y agoAuthor of hecto here, thank you for mentioning it! I wrote the first version around 5 years ago and I’m happy that people still use it. (I updated it in the meantime)
- 90s_dev 1y agoReading through this code is a veritable rite of passage. You learn how C works, how text editors work, how VT codes work, how syntax highlighting works, how find works, and how little code it really takes to make anything when you strip away almost all conveniences, edge cases, and error handling.
- nulld3v 1y agoIt also inspired this similar Rust project: https://github.com/ilai-deutel/kibi#comparison-with-kilo https://github.com/ilai-deutel/kibi#comparison-with-kilo Although it does cheat a bit in an effort to better handle Unicode: > unicode-width is used to determine the displayed width of Unicode characters. Unfortunately, there is no way around it: the unicode character width table is 230 lines long.
- 90s_dev 1y ago> It also inspired this similar Rust project And these projects: https://github.com/antirez/kilo/forks https://github.com/antirez/kilo/forks
- lifthrasiir 1y agoPersonally, this is the reason I don't really buy the extreme size reduction; such projects generally have to sacrifice some essential features that demand a certain but necessary amount of code.
- vidarh 1y agoA lot of those features are only "essential" for a subset of possible users. My own editor exists because I realised it was possible to write an editor smaller than my Emacs configuration. While my editor lacks all kinds of features that are "essential" for lots of other people, it doesn't lack any features essential for me. So in terms of producing a perfect all-round editor that will work for everyone, sure, editors like Kilo will always be flawed. Their value is in providing a learning experience, something that works for the subset who don't need those features, or a basis for people to customise something just right for their needs in a compact way. E.g. my own editor has quirks that are custom-tailored to my workflow, and even to my environment.
- lifthrasiir 1y agoYou are right, but then there is not much reason to make it public because it can't be very useful for general users. I have lots of code that was written only for myself and I don't intend to publish at all.
- fuzztester 1y agoon first look, the name sounds heavy, but the product actually turns out to be very light. go figure. ;)
- akkartik 1y agoFunny story: using kilo was the final straw [1] in getting me to give up on terminals. These days I try to do all my programming atop a simple canvas I can draw pixels on. Here's the text editor I use all the time these days (and base lots of forks off of): https://git.sr.ht/~akkartik/text2.love https://git.sr.ht/~akkartik/text2.love. 1200 LoC, proportional font, word-wrap, scrolling, clipboard, unlimited undo. Can edit Moby Dick. [1] https://git.sr.ht/~akkartik/teliva https://git.sr.ht/~akkartik/teliva
- volemo 1y ago> These days I try to do all my programming atop a simple canvas I can draw pixels on. Why?
- akkartik 1y agoTerminals are full of hacks. For example, in my terminal project linked above the Readme says this: "Backspace is known to not work in some configurations. As a workaround, typing ctrl-h tends to work in those situations." (https://git.sr.ht/~akkartik/teliva#known-issues https://git.sr.ht/~akkartik/teliva#known-issues) This is a problem with every TUI out there built using ncurses. "What escape code does your terminal emit for backspace?" is a completely artificial problem at this point. There are good reasons to deal with the terminal: I need programs built for it, or I need to interface with programs built for it. Programs that deal with 1D streams of bytes for stdin and stdout are simpler in text mode. But for anything else, I try to avoid it.
- ayrtondesozzla 1y agoSorry for jumping off topic but I came across mu recently - looks very interesting! Hope to try it out properly when I get a moment
- akkartik 1y agoThank you! Hit me up any time.
- lor_louis 1y agoKilo is a fun weekend project, but I learned the hard way that it's not a good base uppon which you should build your own text editor. The core data structure (array of lines) just isn't that well suited to more complex operations. Anyway here's what I built: https://github.com/lorlouis/cedit https://github.com/lorlouis/cedit If I were to do it again I'd use a piece table[1]. The VS code folks wrote a fantastic blog post about it some time ago[2]. [1] https://en.m.wikipedia.org/wiki/Piece_table https://en.m.wikipedia.org/wiki/Piece_table [2] https://code.visualstudio.com/blogs/2018/03/23/text-buffer-reimplementation https://code.visualstudio.com/blogs/2018/03/23/text-buffer-r...
- userbinator 1y agoThe core data structure (array of lines) just isn't that well suited to more complex operations. Modern CPUs can read and write memory at dozens of gigabytes per second. Even when CPUs were 3 orders of magnitude slower, text editors using a single array were widely used. Unless you introduce some accidentally-quadratic or worse algorithm in your operations, I don't think complex datastructures are necessary in this application.
- lifthrasiir 1y agoThe actual latency budget would be less than a single frame to be completely non-noticable, so you are in fact limited to less than 1 GB to move per each keystroke. And each character may hold additional metadata like syntax highlight states, so 1 GB of movable memory doesn't translate to 1 GB of text either. You are still correct in that a line-based array is enough for most cases today, but I don't think it's generally true.
- RetroTechie 1y agoMovement of GB's of data being noticeable should be considered a feature, imho. And if those GB's represent text, with user trying to edit that as a single file, well then... PEBKAC.
- lelanthran 1y ago
- jonstewart 1y agoed is the standard text editor.
- revskill 1y agoSo a text editor is about grid manipulation ?
- lbj 1y agoFunny. These days when I see a headline like that, I assume it's some type of web component. Why are all the commenters so eager to get out of terminals?
- nodesocket 1y agoThis seems like a great alternative for Nano; though Nano is really good and just works.
- curtisszmania 1y ago[dead]
- JdeBP 1y agoOne interesting thing is that even some of those 1000 lines could have been eliminated. It duplicates the C library's cfmakeraw() function, for instance. https://man.freebsd.org/cgi/man.cgi?query=cfmakeraw&sektion=3 https://man.freebsd.org/cgi/man.cgi?query=cfmakeraw&sektion=...
- anonzzzies 1y agoAh darn. Closing in on retirement (will never happen, coding is too much fun for profit or charity) age, I resistent building an editor but I want to. Need to. I hacked so much vim, emacs, eclipse, vs code and its all crap (the newer, the worse: all these useless gimmicks you won't use past grade school aaarrr while lacking power user features). Can I do better? This seems a good start.
- giancarlostoro 1y agoI made a similar editor using Lazarus... since it has syntax highlighting components... I guess that's cheating. The more I think about it though, I wonder if Freepascal could produce a nice GUI for Neovim. I did try to build one in Qt in C++ years ago, stopped at trying to figure out how to add Syntax Highlighting since I'm not really that much into C++. Pivoted it to work like Notepad so I was still happy with how it wound up. https://github.com/Giancarlos/qNotePad https://github.com/Giancarlos/qNotePad
- cies 1y agoLast serious work on this was in 2020. Lacks news worthiness imho.
- antirez 1y agoCheck my YouTube channel for an experiment on how I added UTF-8 support to Kilo showcasing what was, back then, some early experiment at using LLMs to ship working code. I need to push the changes after some cleanup, but that's not enough for a Kilo 2, as it makes things more complicated without showing worthwhile programming techniques. So it's sitting on my laptop.
- jonjacky 1y agoThis post might be a response to yesterday's announcement of Microsoft's new Edit editor, which has some of the same features as kilo. 1. https://news.ycombinator.com/item?id=44031529 https://news.ycombinator.com/item?id=44031529
- gnabgib 1y agoProbably not, since Kilo was first posted here 9 years ago https://news.ycombinator.com/item?id=12073926 https://news.ycombinator.com/item?id=12073926
- deleted 1y ago[deleted]