Writing your own text editor in C: part 1

“Build Your Own Text Editor” by Jeremy Ruten.

Hello! This is a free translation on how to write your own no one needs text editor in C.

So what can our editor do?

A little more about the editor implementation:

In total the tutorial consists of 184 steps. Each step you will add, change or remove different lines of code. At most steps you will be able to monitor the changes made simply by compiling and running the program.

I will try to explain each step along the way, sometimes in great detail. Don’t be afraid to skip the theory, because the point is in the actual writing of the code and the fact that you decided to do it in the first place! Everything you learn in the process is a bonus, you will learn something simply by introducing changes to the code and observing the results.

Settings

The first step, a new file and everything from scratch…
First, make sure that your environment is correctly configured for your language (in our case, for C), and that you at least roughly understand how to compile and run your program in this environment.

Fortunately for us, the editor we will write does not depend on any external libraries. The only thing we need is a compiler and the standard C library with which it comes. We will also use a make script to simplify the compilation commands. Make sure you have both a C compiler and make.

C language compiler

Since C is a compiled language, we need, oddly enough, compilerif you don’t have it, it’s Necessarily need to be installed like make. For example, in Ubuntu, this can be done like this:

sudo apt-get install gcc make

main() function

So, this is where our journey begins! Create a file kilo.c and write the basic one in it main() function.

Step 1
int main() {
    return 0;
}

To compile this file, just enter cc kilo.c -o kilo to your terminal. If no errors occur, an executable file will appear in the current directory kilo. To start the program, enter ./kilo. Unfortunately, our program can’t do anything yet, so it won’t print anything for us.

(it is not necessary that your editor be named the same as in the articleyou may well call your editor “uber_mega_super_redaktor1337” another aesthetic name)

Simplify compilation with make

Every time I print something like cc kilo.c -o kilo gets boring, so we’ll write make script.

Create a new one Makefile with similar content:

Step 2
kilo: kilo.c
    $(CC) kilo.c -o kilo -Wall -Wextra -pedantic -std=c99

First line Makefile-a tells us what we want to compile and what we need for this. The second line specifies the command that will be executed make script. $(CC) usually refers to the command cc.

What kind of magic words appeared?
These are the flags, namely:

  1. -Wall – from English all W“arnings”which tells the compiler to output almost all warnings if he doesn’t really like something.
  2. -Wextra And -pedantic asks the compiler to output even more warnings, if any.
  3. -std=c99 shows the compiler which version C language standard to use when compiling. The C99 standard will make the process of writing code a little easier for us.

Now that Makefile configured, try writing the command make to your terminal to compile the program. You can launch it the same way as always, ./kilo.

To be continued…

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *