DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Build a Command-Line Text Editor With Java (Part 3)
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • Java Developers: Build Something Awesome with Copilot CLI and Win Big Prizes!
  • Build an AI Chatroom With ChatGPT and ZK by Asking It How!

Trending

  • When Feature Flags Become Workflows: Configuring Multi-Tenant State Machines
  • Top Gen AI Frameworks for Go in 2026: A Hands-On Comparison
  • Building a Windows App for a Resilient SSH SOCKS Proxy and Selective Browser Routing
  • OwlVigil Open-Sources Its Go SDK for Enterprise AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. How To Build a Command-Line Text Editor With Java (Part 1)

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

Want to know how text editors work, how shell scripts change terminal text colors, update lines without scrolling, or move the cursor around? Here, learn how.

By 
Marco Behler
·
Sep. 09, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
16.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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.

Enjoy!


Text editor Build (game engine) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How To Build a Command-Line Text Editor With Java (Part 3)
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • Java Developers: Build Something Awesome with Copilot CLI and Win Big Prizes!
  • Build an AI Chatroom With ChatGPT and ZK by Asking It How!

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook