@xterm/xterm is a powerful terminal emulator for the web that enables applications to embed fully functional terminals directly in the browser. It supports features like ANSI/VT100 escape sequences, keyboard input handling, and customizable styling. xterm-addon-fit is an official addon for xterm.js that automatically adjusts the terminal's dimensions to fit its container element, ensuring optimal use of available space without manual calculation.
When building web applications that require terminal functionality, developers often face two distinct but related challenges: implementing a robust terminal emulator and ensuring it properly fits within the UI layout. The @xterm/xterm package solves the first problem, while xterm-addon-fit addresses the second. Let's explore how they work together and when each is needed.
@xterm/xterm provides a complete terminal emulator implementation that runs entirely in the browser. It handles:
// Basic terminal setup with @xterm/xterm
import { Terminal } from '@xterm/xterm';
import '@xterm/xterm/css/xterm.css';
const terminal = new Terminal({
cols: 80,
rows: 24,
fontSize: 14
});
terminal.open(document.getElementById('terminal-container'));
terminal.write('Hello from xterm.js!');
xterm-addon-fit is a lightweight addon that extends the terminal's capabilities by providing automatic dimension fitting. It doesn't render anything itself ā instead, it calculates the optimal number of columns and rows based on the container's size and the terminal's font metrics.
// Adding fit functionality to an existing terminal
import { Terminal } from '@xterm/xterm';
import { FitAddon } from 'xterm-addon-fit';
const terminal = new Terminal();
terminal.open(document.getElementById('terminal-container'));
const fitAddon = new FitAddon();
terminal.loadAddon(fitAddon);
// Fit terminal to container
fitAddon.fit();
// Handle window resize events
window.addEventListener('resize', () => fitAddon.fit());
These packages follow a clear separation of concerns pattern. The core terminal handles emulation logic, while addons like xterm-addon-fit provide optional enhancements. This design allows developers to include only the functionality they need.
The integration is straightforward but requires explicit steps:
@xterm/xtermFitAddonfit() method when needed// Complete integration example
import { Terminal } from '@xterm/xterm';
import { FitAddon } from 'xterm-addon-fit';
import '@xterm/xterm/css/xterm.css';
// Initialize terminal
const terminal = new Terminal({
cursorBlink: true,
fontFamily: 'monospace'
});
terminal.open(document.getElementById('terminal'));
// Add fit capability
const fitAddon = new FitAddon();
terminal.loadAddon(fitAddon);
// Initial fit
fitAddon.fit();
// Auto-fit on resize
let resizeTimeout;
window.addEventListener('resize', () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => fitAddon.fit(), 100);
});
@xterm/xterm when:Without this core package, you have no terminal functionality at all.
xterm-addon-fit when:This addon is optional but highly recommended for most real-world applications where terminals need to adapt to their containers.
The FitAddon works by measuring the container's client width and height, then calculating how many characters can fit based on the terminal's current font settings. This approach is more reliable than CSS-based solutions because it accounts for actual character dimensions rather than assuming fixed-width assumptions.
However, there are some considerations:
fit() method involves DOM measurements, so it should be debounced during resize eventsfit()fit() after the terminal is visible and after any font changes// Proper resize handling with debouncing
const fitAddon = new FitAddon();
terminal.loadAddon(fitAddon);
function debouncedFit() {
let timeoutId;
return () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
if (terminal.element) {
fitAddon.fit();
}
}, 150);
};
}
const fitOnResize = debouncedFit();
window.addEventListener('resize', fitOnResize);
Some developers attempt to handle terminal sizing manually by:
CSS-only solutions: Using width: 100%; height: 100% with overflow: hidden
Manual column/row calculation:
// ā Don't do this manually
const charWidth = 8; // Assumed width
const charHeight = 16; // Assumed height
const cols = Math.floor(containerWidth / charWidth);
const rows = Math.floor(containerHeight / charHeight);
terminal.resize(cols, rows);
The FitAddon solves these issues by using the actual rendered font dimensions from the DOM, ensuring accurate sizing regardless of the user's system configuration.
You're building a browser-based IDE with an integrated terminal panel that can be resized by the user.
@xterm/xterm (for terminal functionality) and xterm-addon-fit (for responsive sizing)You need to show read-only terminal output in a fixed-size container for documentation purposes.
@xterm/xtermxterm-addon-fit since dimensions are fixedYour application is essentially a full-screen terminal with minimal UI.
Always load the addon after opening the terminal
// Correct order
terminal.open(container);
terminal.loadAddon(fitAddon);
fitAddon.fit();
Handle the initial render timing
// Wait for next tick to ensure DOM is ready
setTimeout(() => fitAddon.fit(), 0);
Debounce resize handlers to prevent performance issues
// As shown in the debounced example above
Check terminal attachment before calling fit
if (terminal.element) {
fitAddon.fit();
}
Think of @xterm/xterm as the engine of your terminal implementation ā it's what makes everything work. The xterm-addon-fit is like an automatic transmission that makes the engine easier to use in varying conditions. You can drive without it (manual sizing), but most applications benefit significantly from the automatic adaptation it provides.
For virtually any production application using xterm.js in a responsive layout, you'll want both packages working together. The core package gives you terminal functionality, while the addon ensures it looks and works properly across different screen sizes and container dimensions.
Choose @xterm/xterm when you need to embed a fully functional terminal emulator in your web application. This package provides the core functionality for rendering terminal output, handling user input, and managing terminal state. It's essential for any project requiring interactive command-line interfaces in the browser, such as web-based IDEs, remote server access tools, or development environments.
Choose xterm-addon-fit when you're already using @xterm/xterm and need automatic resizing capabilities. This addon specifically addresses the common requirement of making the terminal adapt to its container's dimensions, eliminating the need for custom resize logic. It should be used alongside the core xterm package, not as a standalone solution.

Xterm.js is a front-end component written in TypeScript that lets applications bring fully-featured terminals to their users in the browser. It's used by popular projects such as VS Code, Hyper and Theia.
bash, vim, and tmux, including support for curses-based apps and mouse events.bash. Xterm.js can be connected to processes like bash and let you interact with them (provide input, receive output).First, you need to install the module, we ship exclusively through npm, so you need that installed and then add xterm.js as a dependency by running:
npm install @xterm/xterm
To start using xterm.js on your browser, add the xterm.js and xterm.css to the head of your HTML page. Then create a <div id="terminal"></div> onto which xterm can attach itself. Finally, instantiate the Terminal object and then call the open function with the DOM object of the div.
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="node_modules/@xterm/xterm/css/xterm.css" />
<script src="node_modules/@xterm/xterm/lib/xterm.js"></script>
</head>
<body>
<div id="terminal"></div>
<script>
var term = new Terminal();
term.open(document.getElementById('terminal'));
term.write('Hello from \x1B[1;3;31mxterm.js\x1B[0m $ ')
</script>
</body>
</html>
The recommended way to load xterm.js is via the ES6 module syntax:
import { Terminal } from '@xterm/xterm';
ā ļø This section describes the new addon format introduced in v3.14.0, see here for the instructions on the old format
Addons are separate modules that extend the Terminal by building on the xterm.js API. To use an addon, you first need to install it in your project:
npm i -S @xterm/addon-web-links
Then import the addon, instantiate it and call Terminal.loadAddon:
import { Terminal } from '@xterm/xterm';
import { WebLinksAddon } from '@xterm/addon-web-links';
const terminal = new Terminal();
// Load WebLinksAddon on terminal, this is all that's needed to get web links
// working in the terminal.
terminal.loadAddon(new WebLinksAddon());
The xterm.js team maintains the following addons, but anyone can build them:
@xterm/addon-attach: Attaches to a server running a process via a websocket@xterm/addon-clipboard: Access the browser's clipboard@xterm/addon-fit: Fits the terminal to the containing element@xterm/addon-image: Adds image support@xterm/addon-search: Adds search functionality@xterm/addon-serialize: Serializes the terminal's buffer to a VT sequences or HTML@xterm/addon-unicode11: Updates character widths to their unicode11 values@xterm/addon-web-links: Adds web link detection and interaction@xterm/addon-webgl: Renders xterm.js using a canvas element's webgl2 contextSince xterm.js is typically implemented as a developer tool, only modern browsers are supported officially. Specifically the latest versions of Chrome, Edge, Firefox, and Safari.
Xterm.js works seamlessly in Electron apps and may even work on earlier versions of the browsers. These are the versions we strive to keep working.
We also publish xterm-headless which is a stripped down version of xterm.js that runs in Node.js. An example use case for this is to keep track of a terminal's state where the process is running and using the serialize addon so it can get all state restored upon reconnection.
The full API for xterm.js is contained within the TypeScript declaration file, use the branch/tag picker in GitHub (w) to navigate to the correct version of the API.
Note that some APIs are marked experimental, these are added to enable experimentation with new ideas without committing to support it like a normal semver API. Note that these APIs can change radically between versions, so be sure to read release notes if you plan on using experimental APIs.
Xterm.js follows a monthly release cycle roughly.
All current and past releases are available on this repo's Releases page, you can view the high-level roadmap on the wiki and see what we're working on now by looking through Milestones.
Our CI releases beta builds to npm for every change that goes into master. Install the latest beta build with:
npm install -S @xterm/xterm@beta
These should generally be stable, but some bugs may slip in. We recommend using the beta build primarily to test out new features and to verify bug fixes.
You can read the guide on the wiki to learn how to contribute and set up xterm.js for development.
Xterm.js is used in several world-class applications to provide great terminal experiences.
<ng-terminal></ng-terminal> into your component.Do you use xterm.js in your application as well? Please open a Pull Request to include it here. We would love to have it on our list. Note: Please add any new contributions to the end of the list only.
If you contribute code to this project, you implicitly allow your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work.
Copyright (c) 2017-2022, The xterm.js authors (MIT License)
Copyright (c) 2014-2017, SourceLair, Private Company (www.sourcelair.com) (MIT License)
Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)