14

I have recently learned C and want to start a project to solidify my knowledge. I've settled on making a very simple text editor, something like vim. The problem I face is that I genuinely have no clue how a text editor even works, and I don't know what to google for to learn about it.

Googling about it led to vim's GitHub repo, which is useless to me because the codebase is huge and the code is confusing me. I also found tutorials for making a text editor in C that functions kind of like vim.

Although I thought about following the tutorials, it feels like cheating. How did the vim developers figure out how to code vim without specific tutorials? Or did they start from simpler text editors? How did they figure that out just from knowledge of languages and their documentation?

What is it exactly that I need in order to start writing this text editor without directly following a tutorial? Another example I like to think of is: how did Dennis Ritchie and Ken Thompson code up Unix? I have an idea of how OS's function, but I have no idea how to put it into code. What is it that I'm missing? How do I transfer this knowledge of the language into actual, practical use?

  • 14
    You are comparing yourself against projects with long histories, and exceptional people. Unix originated in a research group with many years of experience in system level programming, and profited from the experience with the more complex Multics OS. Vim was based on vi which was based on ed. These programs didn't suddenly appear, they were an evolution, built by hundreds of people. So don't be frustrated when you find it difficult to skip over all that history. No one can. Instead, try to do projects that are at the edge of your understanding – just within your grasp, but still challenging. – amon Feb 10 '18 at 15:05
  • 8
    You have just learned to play some tunes on the xylophone, and now you want to play in a big international orchestra? Come on, you are expecting too much. Programming is like playing an instrument - you start with small, simple melodies and after several years of practice you learn to play a symphony. – Doc Brown Feb 10 '18 at 16:08
  • 3
    Agreed. Take It as a chance for you to learn something that many beginners learn in the hard way. Start little. And do read vim's code too. You can learn a lot reading existing code. – Laiv Feb 10 '18 at 16:10
  • 17
    Your fundamental problem is lack of clarity in your thinking. You say "a simple little text editor like vim", and then immediately follow that up by noting that its codebase is huge and confusing. This should be a strong signal to you that nothing which closely resembles vim is simple. Even experienced programmers fall into the mental trap you have fallen into. Things that you do not yet understand are not simple. They are complicated. Computer programming is transforming mental logic into reality; start by thinking more clearly about programming. – Eric Lippert Feb 10 '18 at 20:04
  • 4
    Also, stop worrying about "cheating". This isn't a game. You have goals, and they're good goals. Do what you need to do to achieve those goals. You think professional computer programmers don't look at the source when they want to learn how something works? Learning how to learn from source you didn't write is one of the most important programming skills, so start practicing it. – Eric Lippert Feb 10 '18 at 20:05
17

If this is your first programming project, even a simple text editor may be too complicated. Something like vim or an OS is completely out of the question.

Approaching the Problem

In general, the way to get started is roughly similar for most projects, though:

  • You gather your requirements. What exactly will the software do?
  • You start with very few requirements and then add functionality bit by bit.
  • You decompose the problems posed by your current requirements into sub-problems.
  • You keep decomposing your sub-problems until you have something you know how to implement.

Example

Let's take the text editor example.

  • You want to display a portion of a text file on the screen, insert and remove characters and save the current version.

  • Start with just reading a file and displaying its contents.

  • You'll identify (among others) the following sub-problems:

    • How do I know the file name to display?
    • Given a file name, how do I get the file contents?
    • Given a file's contents, how do I display them?

Once you reach a point where your requirement (load file & display it) is completed, you can start considering how to only display a portion that will fit the screen, navigate in your file, etc.

The Next Step

Over time, as you take on more and more complex problems, you'll realize that it becomes increasingly difficult to find suitable ways to decompose your problems. You'll also notice that changing code can become tedious over time.

At that point, it's time to learn some basic architecture and design concepts.

  • Hey man thanks for the advice! I think I'll take this approach. I am a little stubborn-like when it comes to changing my mind, but from what you've suggested, I think I'll try to see how to make a file viewer. Maybe also add a way to show the file metadata some way? I can probably figure that out myself. Thanks a lot! – Faithlesss Feb 10 '18 at 15:41
  • 2
    To add to this, "how do I display them" can probably be further broken down to writing a specific character to a specific position on the screen (if we're talking about a command-line editor), which should be something one can easily find an answer to online. – Dukeling Feb 10 '18 at 19:22
  • 2
    "even a simple text editor may be too complicated" text editors are surprisingly complicated. Imagine editing a 20 MB file. Big enough to require buffering, but not big enough to be taxing to a modern computer. You need to be able to scroll through it, insert and remove text, have the text rearrange itself in real-time, the scroll bar marker update in real time as the model behind it changes. If you have any type of formatting, this gets even more complicated. – user22815 Feb 10 '18 at 20:59
15

You don’t.

If you don’t even have a vague idea how to do something, it is a sign that it is beyond your current skills. Because if you have no idea how to even start, you’re certainly not going to have any idea when it comes to the hardest part of the app.

  • What would be a good project to start with then? I've already made my own hangman game (in the terminal), as well as a tic tac toe game. I just don't see a way to continue so I thought a text editor would be a good idea. – Faithlesss Feb 10 '18 at 15:08
  • 3
    @faithlesss - something that reads and writes files seems like a basic intermediate step. – Telastyn Feb 10 '18 at 15:11
  • 9
    A file viewer might be a good project then, e.g. a pager like the less, more, or view programs. They share some of the aspects of editors, without the complexity of mutable edit buffers. – amon Feb 10 '18 at 15:13
  • @Telastyn I got you. Gonna look at making a file viewer then, probably a simple one that can read basic text formats, and then move onto figuring out how to read something like, maybe a json or a csv file? Shouldn't be that hard to parse it, just have to really look at the string.h documentation I guess. Thanks for the advice! – Faithlesss Feb 10 '18 at 15:43
  • 2
    @Faithlesss Also have a look at the features of a line editor, the predecessor of text editor programs – Bergi Feb 10 '18 at 19:10
2

You need to decide how you want your text editor to work.

This is one of the most aggravating and rewarding experiences of developing your own projects from start to finish. No one is sending you requirements to build from. You need to develop your own requirements.

This means you'll need to do an awful lot of design work before you ever write your first line of code. You'll need to decide what the interface looks like. You'll need to decide what functionality to include. Both of the above questions will be guided by what you feel capable of doing. If you think of the "ideal" situation (how you would like the interface to work), but you don't feel capable of coding it, then you need to start looking at alternative approaches: How could I get this to work? That helps to focus you on the coding approaches you may want to learn more about.

As others have said, trying to copy vim may not be the best approach since it is a large and complicated code base. You're also denying yourself the design work that, in my humble opinion, helps to round your out as a developer.

This doesn't mean you'll need to have the entire application designed from start to finish before you write your first line of code. It's okay for requirements to change over time as you learn more. It's okay to add new features that you don't think of until you're testing/using your own application and you think, "Wouldn't it be nice if..." It's okay to start simple.

1

Writing a full-featured text editor from scratch with no programming experience is foolish: You'll be discouraged and abandon it before learning much.

Several alternatives come to mind:

  • Study the code of some product you are familiar with. If you are proficient in vim, look that over and try to understand something small and isolated, like how it represents the data, or searches for a single character (the f command).
  • Study the code of a very simple program and work your way up from there: cat command then wc then grep then sed for example.
  • Try to write a program which does only a single feature of the editor. Maybe delete the second character in every line of a file (without writing it back), or display only lines 50 through 70 of a file.
  • 1
    Without knowing a bit about common patterns, I think understanding even a tiny piece of a large project like vim is going to be futile. Looking at very small open source projects is a good idea, though. I would add that attempting to make a targeted change to such a program would be a great way to learn. Advantage: you'll learn some patterns used in real-world programs. Disadvantage: you might learn an anti-pattern just as well. – doubleYou Feb 10 '18 at 19:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.