15 ms·
Build Your Own Text Editor
- adzm 10y agoThe next fun experiment is to handle gigabyte files without undue performance troubles with a lively ui thread! This is where scintilla et al run into limits.
- wila 10y agoScintilla is an editor for source code. Normally code isn't gigabytes in size and doesn't need a lexer, code folding or syntax highlighting. Not saying that it can't be done, but if you open a file like that then turning off those features would already make it a lot easier to handle huge files.
- thomastjeffery 10y agoIf that sounds fun to you, do it! I suppose that would be more along the lines of a hex editor, which is probably interesting to implement too.
- girvo 10y agoNeat! I have been trying to, in Nim, but I've had so little experience working with raw terminals and the IOCTL handles, I've been struggling! https://github.com/girvo/te https://github.com/girvo/te This looks like a great tutorial to work through with it :)
- nickpsecurity 10y agoQuickly looked at your source without the slightest knowledge of Nim language. It was certainly Python-like in ease of reading like some here told me. Nice, clean look to it vs the C stuff. Exception is types.nim file that just looks syntactically rougher than others with the asterisks. It looks like class or struct definitions but I'm curious if the asterisk has an effect like it does in C or what's it do?
- girvo 10y agoThe asterisks are what define a symbol as "exported" from a module -- every symbol is private to the module scope by default, but able to be exported by applying the asterisks to it; types.nim is funky because I was exploring a slightly different file/module structure (and having a singular place for top-level shared types that most other modules import is quite useful at times, and gets around circular dependencies), except it does end up looking slightly odd!
- nickpsecurity 10y agoI appreciate it. So, it looks weird because it's an experiment with something non-standard? Makes sense. Things stay clean overall.
- girvo 10y agoThat's it. For the most part, one would just export the symbols from given modules as needed (and a lot of the properties on some of the ref object types are needlessly exported, too, which makes it worse) One other thing is that the {.compile.} pragma in term.nim is not needed anymore, as Nim's stdlib has added a lot of those in[0], but it does show how easy it is to bridge between the two languages (and I'm not much of a C developer!) [0] https://nim-lang.org/docs/terminal.html https://nim-lang.org/docs/terminal.html
- nickpsecurity 10y agoAh. That link is the cleaner look I was expecting. Makes sense.
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- Dangeranger 10y agoReference implementations of medium sized applications are incredibly useful for leveling up as a programmer. While there are many large successful open source applications, many are overwhelming to read and learn from. Having something that outlines the key features and components and which ignores the important but complicated edge cases assists in keeping the attention focused. Now if there are annotation within the source code, that would be truely incredible.
- spooky_action 10y agoDo you have anything similar that you would recommend?
- gingerbread-man 10y agoXv6 is a pedagogical implementation of UNIX v6 that MIT created for an operating systems course. https://pdos.csail.mit.edu/6.828/2012/xv6.html https://pdos.csail.mit.edu/6.828/2012/xv6.html
- rjammala 10y agohttp://www.buildyourownlisp.com/contents http://www.buildyourownlisp.com/contents
- jnordwick 10y agoMinix (mini Unix) is a teaching operating system that goes with the well known os book "Operating Systems Design and Implementation". We used it at Cal. Very fun.
- jdc0589 10y agowas about to post a link. Minix is great.
- drworm 10y agoThis comes to mind: https://cheatdeath.github.io/research-bittorrent-doc/ https://cheatdeath.github.io/research-bittorrent-doc/
- tarboreus 10y agoJust getting further into C and this is fascinating. But I still have to share this: http://www.catb.org/esr/writings/unix-koans/editor-wars.html http://www.catb.org/esr/writings/unix-koans/editor-wars.html
- nojvek 10y agoThis was a great read and well explained. Thanks for the share.
- swah 10y agoIf you enjoy "redis style" C code, take a look at vis https://github.com/martanne/vis https://github.com/martanne/vis
- quickben 10y agoLong return statements and everything crammed into one line is horrible coding approach. It makes it a pain to debug. Don't do it.
- lacampbell 10y agoExample?
- quickben 10y agoIt's in the parent's "redis style" link.
- akkartik 10y agoIf you're so sensitive to superficial syntactic issues you're gonna miss out on a lot of reading/learning pleasure in this life. (It's one of my pet peeves: http://akkartik.name/post/readable-bad http://akkartik.name/post/readable-bad)
- quickben 10y agoI was taking about the ability to debug code. Various IDEs/compilers have sometimes issues and bugs on multistatement lines in DWARF/etc. I've worked on a low level debugger, so it was a big problem, even few years back.
- akkartik 10y agoFair enough. But where do you see multistatement lines in the link?
- milesrout 10y agoLooks like it's been slashdotted.
- Jeremysr 10y agoIt seems to be doing fine now, but you can always download a local copy (it's all static HTML) from https://github.com/snaptoken/kilo-tutorial/releases https://github.com/snaptoken/kilo-tutorial/releases
- hutusi 10y agoThanks for the share.
- strainer 10y agoScite is a barebone open source text editor created by Neil Hodgson to exercise his "Scintilla" text-editor c++ library which is used in others like notepad++. In hindsight, I would have a bit more caution programming text editors. I started tweaking and modifying Scite years ago, it was very interesting but it was no small undertaking and I came to understand why Neil advised in the support forum, to customise it using the inbuilt Lua scripting. Im still using this 6 year old customised version of Scite that I never managed to sync with the latest version, and it has 10 thousand lines of custom Lua facilities like file encryption, navigation panels, multi-edit mode etc.. which I wrote and stabilised a few years ago. I rarely venture to alter it now that I am at last comfortable with it, but its going to need serious attention sooner or later...
- jwmc 10y agoOkay, I won't lie, this is a bit of a side-swipe but... ... it sounds like you kind of wanted emacs. One of the most impressive things I find about emacs (especially since semi-proper packages became a thing) is just how easy it is to get stuff that is 5-10-15 years old working on it. No word of a lie, it's amazing how they've managed to break so little over the years.
- strainer 10y agoNo I appreciate the tip and have often considered diving into Emacs or Vim. Im very fond of the Scite and Lua combo as well now, and i would like to have the time to share and develop it more than to set up on a new system - but yes those other systems do look good...
- jasim 10y agoThose features sound really nice. Any links/screenshots?
- strainer 10y agoIve put just a couple of snaps here: http://imgur.com/a/QMhpf http://imgur.com/a/QMhpf I really must get round to sharing the source but it needs a bit of preparation.
- briansteffens 10y agoNice guide, especially the terminal raw mode stuff. I haven't seen much on that before, I really appreciate that this guide goes over that. I've been messing around with a little text editor component myself in golang: https://github.com/briansteffens/tui/blob/master/editbox.go https://github.com/briansteffens/tui/blob/master/editbox.go For a SQL editor project: https://github.com/briansteffens/prequel https://github.com/briansteffens/prequel
- ohmygeek 10y agotui looks pretty interesting! Can you please add a README? I would love to go over the source code :)
- briansteffens 10y agoThanks! It's still pretty rough but I'm hoping to put some notes up soon.
- bigger_cheese 10y agoIf you are looking for an interesting project that uses terminal input modes try writing a command line utility that reads in a password correctly. (eg. Masks the password input so it does not appear on screen whilst it is being typed, prevents someone from scrolling up in terminal and copy/pasting the typed password and zeros out the password after use to prevent it being visible in process address space). It is surprisingly tricky to get right. There used to be a POSIX function in the c standard library - "getpass" to handle this but the implementation was not thread safe and thus it was depreciated from posix spec - the only portable way I know to do this is to "roll your own code" using termios - not ideal. see: http://man7.org/linux/man-pages/man3/getpass.3.html http://man7.org/linux/man-pages/man3/getpass.3.html note the comment: "This function is obsolete. Do not use it" - there is a heap of potentially vulnerable code out in the wild that uses getpass.
- idbehold 10y agoYeah, this is still a problem. Trying to write a simple CLI in node and the library I'm using to prompt a user for their credentials only replaces the user's input with asterisks. There is still an open issue for properly masking the password input: https://github.com/SBoudrias/Inquirer.js/issues/177 https://github.com/SBoudrias/Inquirer.js/issues/177
- WalterBright 10y agoHere's one from the 1980s, which I still use and keep up to date: https://github.com/DigitalMars/me https://github.com/DigitalMars/me and translated to D: https://github.com/DigitalMars/med https://github.com/DigitalMars/med
- d99kris 10y agoNice! Btw, I think you are missing browse[.c|.o] for SRC|OBJ in osx.mak
- deleted 10y ago[deleted]
- TeMPOraL 10y agoA trivial thing, but README.md for me says "Micro Emacs in D", where it should say "Micro Emacs in C" (the repo description on Github is correct though).
- hwillis 10y agoThat's it, I'm upgrading to Windows ten.
- mhd 10y agoStill use for occasional quick edits instead of vi/notepad or for proper, longer development tasks?
- WalterBright 10y agoIt's my primary editor. I'm so used to it I don't even know what the commands are - I just watch my fingers do it. The first thing I do with a new system is port ME to it. I'll use vi to do what's necessary to get there (usually get ssh working so I can edit the files with ME on another machine). I've worked out how to do syntax highlighting on it, but never got around to doing it.
- 10y ago
- nojvek 10y agokilo.c code referenced in the tuturial if you're interested in compiling and hacking around. https://github.com/antirez/kilo/blob/master/kilo.c https://github.com/antirez/kilo/blob/master/kilo.c I wonder how mouse clicking and scrolling in editor works? Would love to make html/js/css for terminal node module. I think that would be fun.
- dchuk 10y agoWould something like this be a way for a beginner to C (barely any experience) to get their feet wet? Or is there an expectation of basic familiarity with C already?
- bmer 10y agoDo you have general programming experience?
- dchuk 10y agoYep, lots of Ruby/PHP/Python/Javascript, some Java. So I can comfortably read syntax in most languages, but obviously none of the languages I've used are as low level as C.
- SwellJoe 10y agoIt seems to be trying to explain the C as it goes along, but I don't think I'd get it if I hadn't done C programming in the past. But, if you've done any Java or Objective C, you can probably infer the gist of it without serious C study.
- sn9 10y agoIf you can write C at the level of K&R, you'll probably be fine. Hanson's C Interfaces and Implementations is another great book that would better prepare you for this.
- learningcn 10y agoThat's what I like
- japhyr 10y agoI'm curious if anyone here regularly codes in a text editor they wrote themselves? I've often thought of coding one for fun, with no intention to share it, just for the purpose of having a long-term project that evolves along with my skills. I've never made time for it, but I still consider it once in a while.
- bobochan 10y agoI would not say that I use it regularly, but I have a somewhat working version of what I remember to be the Norton Editor in C++ / ncurses. It was 100% motivated by seeing antirez's artistic triumph, and my effort is sadly lacking in comparison. Still, I look at it every few weeks and add a new feature, and now I am mostly editing the code in the editor itself, which is gratifying. I actually need to get off my duff and code what I really want, which is a version of what I remember the best parts of the Norton Editor implemented as an emacs mode. I have great memories of writing pascal code in ne.com, so it is a big nostalgia item for me.
- jason_slack 10y agoNorton Editor. Interesting that you mention this. I loved the Norton Editor. I have wanted to for years add a more Norton feel to the text editor that I write. I wonder if I can still get a copy to play around with and run in a virtual machine. Any idea?
- bobochan 10y agoI had a copy that I found on an old floppy about 15 years ago and I tried to see if I could use it in a Windows command window, but unfortunately it did not seem to work at all. Now that you mention a virtual machine though, I regret that I did not try and setup a FreeDOS VM. Googling "norton editor manual pdf" lead me to a few old copies of the manual, so that is what I have used as a guide in my work, but most of it is just driven by how I remember it.
- yjftsjthsd-h 10y ago
- nanospeck 10y agoDoes anyone have similar links but written in Java? If yes, please share here.
- jquast 10y agomy toy 300 loc text editor for python, https://github.com/jquast/blessed/blob/master/bin/editor.py https://github.com/jquast/blessed/blob/master/bin/editor.py
- gavinpc 10y agoThanks for making this! It is obviously a labor of love. This kind of incremental building is a lot of work, but adds a revealing dimension not seen in a flat annotated source. Great work.
- mistermumble 10y agoI think Kilo is a great little project, well-structured and very educational. Another useful resource I've relied upon in the past, dates back to the 1990s: Freyja, which is Craig Finseth's emacs-like editor written in C. Here is a list of features: * deletions are automatically saved into a "kill buffer" * ability to edit up to 11 files at once * ability to view two independent windows at once * integrated help facility * integrated menu facility, with help on all commands * can record and play back keyboard macros * supports file completion and limited directory operations * includes a fully-integrated RPN type calculator It was designed for MS-DOS with the Cygwin terminal library. I found the architecture to be very clean, and it is well explained in Finseth's classic book ("The Craft of Text Editing"). The book is worth reading even if you never touch the code: http://www.finseth.com/craft/ http://www.finseth.com/craft/ It uses a multi-buffer architecture roughly similar to Walter Bright's text editor (see sibling posting). (I knew about Finseth's editor years ago, but was not aware of Bright's work until now, thanks Walter!) The Freyja source can be downloaded from: http://www.finseth.com/parts/freyja.php http://www.finseth.com/parts/freyja.php Look for the link to "freyja30.exe" which turns out to be freyja30.zip (not an executable).
- m_andretti 10y agoGreat documentation
- har777 10y agoThis is excellent. Having a lot of fun going through it. Always wanted to start writing some C.
- evacchi 10y agomarginally relevant: I was looking for a terminal text editor for git commits and other similarly simple tasks: my only requirement is that I can save&leave with ^D. Any suggestions?
- unhammer 10y agoSimplest I can think of: $ echo 'cat > "$@"' >editor $ chmod +x editor $ GIT_EDITOR=./editor git commit -a stuff [master b2d3915] stuff 1 file changed, 1 insertion(+) You'll need to hit enter before ^D. You can add simple Emacs-like keybindings by changing that into $ echo 'rlwrap cat > "$@"' >editor Or just learn ed: $ GIT_EDITOR=ed git commit --amend -a 256 1c stuff and more stuff . w 271 [master c5092a6] stuff and more stuff Date: Thu Apr 6 12:58:18 2017 +0200 1 file changed, 1 insertion(+) (the numbers are ed telling me how much was read and written; `1c` means change the first line; `.` means I'm done inserting (go back to command mode) and `w` means write/save; exit with ^D)
- evacchi 10y agoThanks for the tips!
- kevinmgranger 10y agoIf anyone is having trouble getting the first one working, you need a shebang at the top of the file! $ printf '#!/usr/bin/env sh\ncat > "$@"\n' > editor But the second point is more valid. Ed is the standard editor.
- unhammer 10y agoWhat system are you on that needs the shebang? bash 4 on Ubuntu seems to just run it in bash, though I guess it probably shouldn't :-)
- teddyh 10y agoEmacs? (global-set-key (kbd "C-d") 'save-buffers-kill-terminal)
- JepZ 10y agoSometimes when I configure my vim, I feel like I am building an editor too :D But at least that is more of a LEGO style of putting different bricks (plugins) together.
- aethertron 10y ago1000 lines of code is considered small. But check this out http://kparc.com/edit.k http://kparc.com/edit.k Under 50 lines for a text editor written in K by the language's author. Way beyond my present understanding, but the promise of very small, powerful code is incredibly attractive.
- johnfn 10y agoJust to be that guy. Yeah, it's cool that the language author built a text editor in under 50 lines, and there's a sort of geeky hacky appeal to trying to cram as much functionality as possible into as small a space as possible. But what does it actually mean in terms of programming? How maintainable is "small code"? How readable is it? (Well, you answered that question already.) How hackable is it? It's a curiosity, and a really fun one! But it's not practical.
- aethertron 10y agoFine questions. But super-compact code could be very practical/readable/hackable, as long as there's the prerequisite knowledge. Fewer symbols means less complexity to parse. This works, provided that those symbols map to powerful operators that can be really understood and effectively combined to produce the desired outcome. And there's no need to scroll: perceive everything in one glance!
- de_Selby 10y ago> But what does it actually mean in terms of programming? Without trying to be too trite, what does this question mean in terms of English? > How maintainable is "small code"? Once you get the hang of it it's as maintainable as any other code base > How readable is it? (Well, you answered that question already.) There is a learning curve, but the code is actually reasonably readable. It takes time to get used to many operations occurring in one line but there are benefits (eg you can see everything that the CTRL-Z function for undo does at a glance). > How hackable is it? Again, I'm not sure what you're asking here. The editor is very bare bones so isn't a great example of production code. In a real system generally people are a bit more verbose.
- 10y ago
- spectaclepiece 10y agoIt's my birthday today and after a peaceful morning routine I hadn't yet decided what I would do today except that I would do whatever I felt like doing. I felt like writing a text editor in C.
- movedx 10y agoHappy (belated) birthday, friend! I hope you had a good day. How far did you end up getting with the C editor? :)
- vbernat 10y agoFor better portability, terminfo should be used instead of using hard-coded terminal sequences. Otherwise, this is a really great intro. I liked the beginning with how to put your configure your terminal.
- ash 10y agoWhy would you need to care about portability? Do people use terminals that are incompatible with xterm?
- ja_k 10y agoPretty sure that this is the same as the first project for Oxford ComSci students in first year
- xyclos 10y agofor some strange reason the 'Fira Mono' google font is displaying all &s as |s. Anyone else seeing this? Cool book though, I'm going to work through it this weekend.
- Jeremysr 10y agoSee https://github.com/snaptoken/kilo-tutorial/issues/4 https://github.com/snaptoken/kilo-tutorial/issues/4 You may have a broken version of the font locally. If you download the offline version of the tutorial, it'll come with all the fonts it uses and hopefully will work.
- zaf 10y agoSome time ago I went overboard and wrote an editor in PHP with the 'main' feature being auto encrypted files. Completely bananas. https://github.com/ziqbal/pubscripts/tree/master/logos https://github.com/ziqbal/pubscripts/tree/master/logos
- z3t4 10y agoI'm working on an editor in JavaScript. You would be surprised how fast string operations, like concatenation, are in JavaScript! You can hold the entire buffer in a String! While browsers renders text very well, the DOM is relative slow to interact with, but there are other ways to render in JavaScript, for example the Canvas, or into a terminal, or even stream a video, or talk directly to a display.
- thomastjeffery 10y agoThat's really what JavaScript was made for: editing HTML (strings) on-the-fly. It has been optimized well, indeed. What kind of data structure are you using for edits?
- pklausler 10y agoI got fed up with the standard offerings back in '07 or so and hammered out my idea of a code editor that's perfect for me over a long weekend. It mmaps files initially for instant response, it has a small sensible command set that I can remember completely, it depends on nothing more than the standard C library and a terminal emulator, and it's not many more lines of code than some Emacs configuration files that I've seen. I still use it for everything.
- netghost 10y agoJust have to say, this is ridiculously detailed, which is great. I'm not looking to build a text editor in C, but I learned a good amount just skimming the first few chapters. Great site, great presentation, thanks for digging into it with so much detail.
- bwidlar 10y agoGreat work, thanks.
- teddyh 10y agoThe Craft of Text Editing, by Craig A. Finseth: http://www.finseth.com/craft/ http://www.finseth.com/craft/
- MR_HANDS 10y agothis is great, thanks
- Zardoz84 10y agoI rememeber when some years ago, I wrote a CLI aventure game, using ncurses to handle windowsing, raw input, etc. How easy made my life....
- bleurghyflergy 10y agoThis is a really cool hacking primer. The guy who made this (Jeremy Ruten, I think) should be commended for taking something complicated and boiling it down into accessible terms. I hope we see more documents like this on HN in the future.