A terminal emulator is a software application that mimics a physical hardware terminal by providing a graphical window to interact with a shell. Most people mistake the emulator for the shell itself, but the emulator is merely the interface that handles keyboard input and renders the text output sent by the shell.

What Parts Make Up a Terminal Emulator?

The anatomy of a terminal emulator is divided into three primary layers: the user interface (UI), the PTY (pseudo-terminal) interface, and the rendering engine. The UI handles the window borders, tabs, and menu bars. The PTY acts as the bridge, tricking the operating system into thinking a physical serial device is connected when it is actually just a software process.

The rendering engine is the most complex part. It must translate a stream of bytes into visual characters on a screen. This involves managing a grid of cells, where each cell contains a character, a foreground color, and a background color. When the shell sends a character, the emulator places it in the current cursor position and moves the cursor forward. When the shell sends a special control sequence, the emulator changes the state of the grid instead of printing a character.

The Role of the PTY

A pseudo-terminal consists of two parts: the master and the slave. The terminal emulator holds the master end, and the shell (like Bash or Zsh) holds the slave end. When you type a key, the emulator writes that byte to the master end. The kernel then passes it to the slave end, where the shell reads it. This separation allows the emulator to handle things like window resizing and keyboard shortcuts without the shell needing to know the specifics of the graphics hardware.

How Does the Emulator Render Text?

Rendering is not as simple as printing letters; it is a process of interpreting a protocol. Most modern emulators follow the VT100 or Xterm standards. These standards define how to handle "escape sequences," which are strings of characters that start with an Escape character (ASCII 27) and tell the emulator to do something other than print text.

For example, if a program wants to move the cursor to the top-left corner, it doesn't send a command like "move_cursor(0,0)". Instead, it sends a sequence of bytes like \e[H. The emulator sees the \e, recognizes it as the start of a command, and executes the move. This allows a simple stream of text to create complex interfaces like text-based editors or system monitors.

Sequence Type Action Taken by Emulator Example Result
ANSI Color Code Changes the current pen color Text turns green or red
Cursor Movement Changes the active cell coordinate Moving the cursor up 2 lines
Clear Screen Wipes the cell grid buffer Blank screen with cursor at 0,0
Window Title Updates the OS window header Title changes to "ssh - user@host"

What Factors Change Emulator Performance?

Not all emulators behave the same way. The primary difference lies in how they handle the rendering pipeline. Some use the CPU to draw every character, while others use the GPU to treat the terminal as a texture, which is significantly faster for high-throughput data.

Feature CPU Rendering GPU Rendering
Latency Lower for small amounts of text Higher initial overhead
Throughput Slows down during large file cats Handles millions of lines smoothly
Power Use Higher CPU spikes Higher GPU power draw
Font Quality Standard rasterization Often supports sub-pixel antialiasing

How Does the Buffer Manage History?

The scrollback buffer is a memory region that stores lines of text that have moved off the top of the visible screen. When the shell sends a newline character, the emulator shifts the entire grid of cells upward. The top line is pushed into the scrollback buffer, and a new empty line is created at the bottom.

The size of this buffer is a critical setting. A buffer of 1,000 lines uses very little RAM, but a buffer of 100,000 lines can consume significant memory because each cell must store character data and color attributes. If you set the buffer to "unlimited," the emulator will continue to allocate memory as long as the session is open, which can lead to system slowdowns during long-running processes that output massive amounts of logs.

An Example of Memory Usage

Consider a terminal with a grid size of 80 columns by 24 rows and a scrollback buffer of 10,000 lines. If each cell requires 4 bytes to store the character and its color attributes, the visible screen uses 80 * 24 * 4 = 7,680 bytes. The scrollback buffer uses 80 * 10,000 * 4 = 3,200,000 bytes, or about 3.2 MB. While this seems small, high-resolution terminals with 200 columns and 1 million lines of scrollback can easily exceed 800 MB of RAM just for the text buffer.

What Happens When You Type a Character?

The path from a keypress to a character appearing on the screen follows a specific loop. First, the OS captures the hardware interrupt from the keyboard. The OS then sends this event to the terminal emulator window. The emulator converts the key event into a byte (e.g., the letter 'A' becomes ASCII 65) and writes it to the PTY master.

The kernel moves that byte to the PTY slave. The shell, which is listening to the slave, reads the byte. In "canonical mode," the shell waits for a newline before processing the input. In "non-canonical mode" (used by programs like Vim), the shell passes the byte to the application immediately. The application then decides what to do. If it wants to echo the character back to the user, it writes ASCII 65 back to the PTY slave. The emulator reads this from the master and renders the 'A' in the current cell.

The Echo Misconception

A common misconception is that the terminal emulator automatically displays what you type. In reality, the emulator usually does not show any characters until the shell or the running program explicitly sends those characters back to the emulator. This is why, when you type a password into a sudo prompt, the screen remains blank; the program has simply told the PTY not to echo the characters back to the emulator.

How Do Fonts and Glyphs Work?

Terminal emulators rely on monospaced fonts, meaning every character occupies the exact same width and height. This ensures that the grid of cells remains perfectly aligned. If a variable-width font were used, a single space would be narrower than a capital 'W', and the cursor coordinates would no longer match the visual positions of the characters.

Modern emulators also support "ligatures" and "wide characters." Ligatures allow the emulator to replace two characters (like > and =) with a single visual glyph (like an arrow) without changing the underlying data in the cell. Wide characters, common in CJK (Chinese, Japanese, Korean) languages, occupy two cells instead of one. The emulator must track these double-width cells to ensure that the cursor moves correctly and that text does not overlap.

What Happens During a Window Resize?

When you drag the corner of a terminal window to change its size, the emulator must notify the shell so the shell can redraw the interface. The emulator sends a SIGWINCH (Signal Window Change) signal to the process group associated with the PTY. This signal tells the shell, "The dimensions have changed to X columns and Y rows."

The shell then queries the PTY for the new dimensions. If you are using a full-screen application like top or vim, the application catches the SIGWINCH signal and recalculates how to layout its UI. It then sends a series of escape sequences to clear the screen and redraw the interface to fit the new dimensions. If an emulator fails to send this signal, the text will appear wrapped or cut off until the user manually triggers a redraw.