Select Page

How To Build a Command-Line Text Editor (Part 1)

Marco Behler
Published: September 9, 2022

Have you ever wanted to know how text editors work, or how shell scripts change terminal text colors, update lines without scrolling, or move the cursor around? Surprise, surprise: even as Java devs, we can do this! 

In this series, I’ll walk you through building a terminal-based text editor with Java

In part 1, learn how to use ANSI escape codes and native C libraries to do fancy text output with Java, interact with your terminal, and build a file viewer skeleton.

What’s in the Video

We’ll begin the video by explaining the general project setup. In fact, the only thing you need for our text editor to work is to have Java installed. We’ll start with a blank Java class, and from there on will work our way towards a working file viewer skeleton.

As a quick note, the video focuses on Unix/macOS terminals. Windows support is something that will be added in a future episode.

To understand how text editors work, you’ll first need to understand what ANSI escape codes are. They are specific strings you output to your terminal that let you delete the screen, single rows, position the cursor, change your text’s color, and much more. Even better, all modern terminals understand them by default.

Up next is understanding how to set your terminal into “raw mode.” By default, your terminal processes text line-by-line, echoing every character that we type and much more. We don’t want all of that. However, to enter raw mode, we’ll need to learn about native Unix APIs and how to access them with Java.

One such API is the Termios API. It effectively lets you set your terminal into raw mode; hence, I’ll show you how to use JNA to access the Termios API — and do a fair amount of bit-wise ANDs to get your terminal into the right mode.

With ANSI escape codes and terminal raw mode knowledge under our belts, we can finally create our terminal editor skeleton. When we start the editor, we want to see a clear screen. In addition, all empty rows should start with a “~” sign, and we should also have a nice little status bar at the bottom of the screen — time for implementation!

To properly implement the status bar and “~” signs, however, we need information on how many rows and columns our terminal window actually has. You cannot get that information with the Termios API, hence we need to use another native Unix API for that: the IOCTL API. There are a couple of caveats to watch out for when calling IOCTL, so we’ll focus on that. Eventually, with the IOCTL API hooked up, our terminal skeleton actually works as expected, using the width and height of the entire terminal.

We’re almost at the end of the episode, were it not for some tiny, pesky bugs, like the flickering of the screen and wrong cursor positioning. Hence, we’re going to optimize the method calls responsible for that behavior.

That was a lot of concepts we just went through! That’s why we’ll finish off now, having a look at what comes next: loading and displaying text files, as well as being able to move your cursor around.