@xterm/xterm vs xterm-addon-fit
Terminal Emulation and Responsive Sizing in Web Applications
@xterm/xtermxterm-addon-fit

Terminal Emulation and Responsive Sizing in Web Applications

@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.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
@xterm/xterm020,2205.92 MB1303 months agoMIT
xterm-addon-fit020,22013.3 kB1303 years agoMIT

Terminal Emulation vs Automatic Resizing: @xterm/xterm and xterm-addon-fit Explained

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.

šŸ–„ļø Core Functionality: What Each Package Actually Does

@xterm/xterm provides a complete terminal emulator implementation that runs entirely in the browser. It handles:

  • Rendering of text with proper character encoding and escape sequence interpretation
  • Keyboard and mouse input processing
  • Scrollback buffer management
  • Customizable styling and themes
  • Bidirectional communication with backend processes
// 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());

šŸ”§ Integration Pattern: How They Work Together

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:

  1. Create and open the terminal using @xterm/xterm
  2. Instantiate the FitAddon
  3. Load the addon into the terminal instance
  4. Call the fit() 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);
});

šŸ“ When You Need Each Package

Use @xterm/xterm when:

  • You need to display terminal output in your web application
  • You must handle user input (keyboard commands, paste operations)
  • You're building a web-based SSH client, code editor terminal, or similar tool
  • You require control over terminal appearance and behavior

Without this core package, you have no terminal functionality at all.

Use xterm-addon-fit when:

  • Your terminal container has dynamic dimensions
  • You want the terminal to automatically use available space
  • You're building responsive layouts that adapt to different screen sizes
  • You want to avoid manual calculation of columns/rows based on font metrics

This addon is optional but highly recommended for most real-world applications where terminals need to adapt to their containers.

āš™ļø Implementation Details and Trade-offs

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:

  • Performance: The fit() method involves DOM measurements, so it should be debounced during resize events
  • Dependencies: The addon requires the terminal to be attached to the DOM before calling fit()
  • Timing: You must call 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);

šŸ”„ Alternative Approaches and Why They Fall Short

Some developers attempt to handle terminal sizing manually by:

  1. CSS-only solutions: Using width: 100%; height: 100% with overflow: hidden

    • Problem: Doesn't account for character dimensions, leading to scrollbars or unused space
  2. 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);
    
    • Problem: Font metrics vary across systems and browsers

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.

šŸ“± Real-World Usage Scenarios

Scenario 1: Web-Based Development Environment

You're building a browser-based IDE with an integrated terminal panel that can be resized by the user.

  • āœ… Required: Both @xterm/xterm (for terminal functionality) and xterm-addon-fit (for responsive sizing)
  • The terminal must adapt when users drag the panel divider

Scenario 2: Static Terminal Display

You need to show read-only terminal output in a fixed-size container for documentation purposes.

  • āœ… Required: Only @xterm/xterm
  • āŒ Not needed: xterm-addon-fit since dimensions are fixed

Scenario 3: Full-Screen Terminal Application

Your application is essentially a full-screen terminal with minimal UI.

  • āœ… Required: Both packages
  • The terminal should use the entire viewport and adapt to window resize events

šŸ› ļø Best Practices for Implementation

  1. Always load the addon after opening the terminal

    // Correct order
    terminal.open(container);
    terminal.loadAddon(fitAddon);
    fitAddon.fit();
    
  2. Handle the initial render timing

    // Wait for next tick to ensure DOM is ready
    setTimeout(() => fitAddon.fit(), 0);
    
  3. Debounce resize handlers to prevent performance issues

    // As shown in the debounced example above
    
  4. Check terminal attachment before calling fit

    if (terminal.element) {
      fitAddon.fit();
    }
    

šŸ’” Key Takeaway

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.

How to Choose: @xterm/xterm vs xterm-addon-fit

  • @xterm/xterm:

    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.

  • xterm-addon-fit:

    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.

README for @xterm/xterm

xterm.js logo

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.

Features

  • Terminal apps just work: Xterm.js works with most terminal apps such as bash, vim, and tmux, including support for curses-based apps and mouse events.
  • Performant: Xterm.js is really fast, it even includes a GPU-accelerated renderer.
  • Rich Unicode support: Supports CJK, emojis, and IMEs.
  • Self-contained: Requires zero dependencies to work.
  • Accessible: Screen reader and minimum contrast ratio support can be turned on.
  • And much more: Links, theming, addons, well documented API, etc.

What xterm.js is not

  • Xterm.js is not a terminal application that you can download and use on your computer.
  • Xterm.js is not bash. Xterm.js can be connected to processes like bash and let you interact with them (provide input, receive output).

Getting Started

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>

Importing

The recommended way to load xterm.js is via the ES6 module syntax:

import { Terminal } from '@xterm/xterm';

Addons

āš ļø 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:

Browser Support

Since 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.

Node.js Support

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.

API

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.

Releases

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.

Beta builds

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.

Contributing

You can read the guide on the wiki to learn how to contribute and set up xterm.js for development.

Real-world uses

Xterm.js is used in several world-class applications to provide great terminal experiences.

  • SourceLair: In-browser IDE that provides its users with fully-featured Linux terminals based on xterm.js.
  • Microsoft Visual Studio Code: Modern, versatile, and powerful open source code editor that provides an integrated terminal based on xterm.js.
  • ttyd: A command-line tool for sharing terminal over the web, with fully-featured terminal emulation based on xterm.js.
  • Eclipse Che: Developer workspace server, cloud IDE, and Eclipse next-generation IDE.
  • Codenvy: Cloud workspaces for development teams.
  • CoderPad: Online interviewing platform for programmers. Run code in many programming languages, with results displayed by xterm.js.
  • WebSSH2: A web based SSH2 client using xterm.js, socket.io, and ssh2.
  • Spyder Terminal: A full fledged system terminal embedded on Spyder IDE.
  • Cloud Commander: Orthodox web file manager with console and editor.
  • Next Tech: Online platform for interactive coding and web development courses. Live container-backed terminal uses xterm.js.
  • RStudio: RStudio is an integrated development environment (IDE) for R.
  • Terminal for Atom: A simple terminal for the Atom text editor.
  • Eclipse Orion: A modern, open source software development environment that runs in the cloud. Code, deploy, and run in the cloud.
  • Gravitational Teleport: Gravitational Teleport is a modern SSH server for remotely accessing clusters of Linux servers via SSH or HTTPS.
  • Hexlet: Practical programming courses (JavaScript, PHP, Unix, databases, functional programming). A steady path from the first line of code to the first job.
  • Selenoid UI: Simple UI for the scalable golang implementation of Selenium Hub named Selenoid. We use XTerm for streaming logs over websockets from docker containers.
  • Portainer: Simple management UI for Docker.
  • SSHy: HTML5 Based SSHv2 Web Client with E2E encryption utilising xterm.js, SJCL & websockets.
  • JupyterLab: An extensible computational environment for Jupyter, supporting interactive data science and scientific computing across all programming languages.
  • Theia: Theia is a cloud & desktop IDE framework implemented in TypeScript.
  • Opshell Ops Helper tool to make life easier working with AWS instances across multiple organizations.
  • Proxmox VE: Proxmox VE is a complete open-source platform for enterprise virtualization. It uses xterm.js for container terminals and the host shell.
  • Script Runner: Run scripts (or a shell) in Atom.
  • Whack Whack Terminal: Terminal emulator for Visual Studio 2017.
  • VTerm: Extensible terminal emulator based on Electron and React.
  • electerm: electerm is a terminal/ssh/sftp client(mac, win, linux) based on electron/node-pty/xterm.
  • Kubebox: Terminal console for Kubernetes clusters.
  • Azure Cloud Shell: Azure Cloud Shell is a Microsoft-managed admin machine built on Azure, for Azure.
  • atom-xterm: Atom plugin for providing terminals inside your Atom workspace.
  • rtty: Access your terminals from anywhere via the web.
  • Pisth: An SFTP and SSH client for iOS.
  • abstruse: Abstruse CI is a continuous integration platform based on Node.JS and Docker.
  • Azure Data Studio: A data management tool that enables working with SQL Server, Azure SQL DB and SQL DW from Windows, macOS and Linux.
  • FreeMAN: A free, cross-platform file manager for power users.
  • Fluent Terminal: A terminal emulator based on UWP and web technologies.
  • Hyper: A terminal built on web technologies.
  • Diag: A better way to troubleshoot problems faster. Capture, share and reapply troubleshooting knowledge so you can focus on solving problems that matter.
  • GoTTY: A simple command line tool that shares your terminal as a web application based on xterm.js.
  • genact: A nonsense activity generator.
  • cPanel & WHM: The hosting platform of choice.
  • Nutanix: Nutanix Enterprise Cloud uses xterm in the webssh functionality within Nutanix Calm, and is also looking to move our old noserial (termjs) functionality to xterm.js.
  • SSH Web Client: SSH Web Client with PHP.
  • Juno: A flexible Julia IDE, based on Atom.
  • webssh: Web based ssh client.
  • info-beamer hosted: Uses xterm.js to manage digital signage devices from the web dashboard.
  • Jumpserver: Jumpserver Luna project, Jumpserver is a bastion server project, Luna use xterm.js for web terminal emulation.
  • LxdMosaic: Uses xterm.js to give terminal access to containers through LXD
  • CodeInterview.io: A coding interview platform in 25+ languages and many web frameworks. Uses xterm.js to provide shell access.
  • Bastillion: Bastillion is an open-source web-based SSH console that centrally manages administrative access to systems.
  • PHP App Server: Create lightweight, installable almost-native applications for desktop OSes. ExecTerminal (nicely wraps the xterm.js Terminal), TerminalManager, and RunProcessSDK are self-contained, reusable ES5+ compliant Javascript components.
  • NgTerminal: NgTerminal is a web terminal that leverages xterm.js on Angular 7+. You can easily add it into your application by adding <ng-terminal></ng-terminal> into your component.
  • tty-share: Extremely simple terminal sharing over the Internet.
  • Ten Hands: One place to run your command-line tasks.
  • WebAssembly.sh: A WebAssembly WASI browser terminal
  • Gus: A shared coding pad where you can run Python with xterm.js
  • Linode: Linode uses xterm.js to provide users a web console for their Linode instances.
  • FluffOS: Active maintained LPMUD driver with websocket support.
  • x-terminal: Atom plugin for providing terminals inside your Atom workspace.
  • CoCalc: Lots of free software pre-installed, to chat, collaborate, develop, program, publish, research, share, teach, in C++, HTML, Julia, Jupyter, LaTeX, Markdown, Python, R, SageMath, Scala, ...
  • Dank Domain: Open source multiuser medieval game supporting old & new terminal emulation.
  • DockerStacks: Local LAMP/LEMP development studio
  • Codecademy: Uses xterm.js in its courses on Bash.
  • Laravel Ssh Web Client: Laravel server inventory with ssh web client to connect at server using xterm.js
  • Replit: Collaborative browser based IDE with support for 50+ different languages.
  • TeleType: cli tool that allows you to share your terminal online conveniently. Show off mad cli-fu, help a colleague, teach, or troubleshoot.
  • Intervue: Pair programming for interviews. Multiple programming languages are supported, with results displayed by xterm.js.
  • TRASA: Zero trust access to Web, SSH, RDP, and Database services.
  • Commas: Commas is a hackable terminal and command runner.
  • Devtron: Software Delivery Workflow For Kubernetes.
  • NxShell: An easy to use new terminal for SSH.
  • gifcast: Converts an asciinema cast to an animated GIF.
  • WizardWebssh: A terminal with Pyqt5 Widget for embedding, which can be used as an ssh client to connect to your ssh servers. It is written in Python, based on tornado, paramiko, and xterm.js.
  • Wizard Assistant: Wizard Assistant comes with advanced automation tools, preloaded common and special time-saving commands, and a built-in SSH terminal. Now you can remotely administer, troubleshoot, and analyze any system with ease.
  • ucli: Command Line for everyone :family_man_woman_girl_boy: at www.ucli.tech.
  • Tess: Simple Terminal Fully Customizable for Everyone. Discover more at tessapp.dev
  • HashiCorp Nomad: A container orchestrator with the ability to connect to remote tasks via a web interface using websockets and xterm.js.
  • TermPair: View and control terminals from your browser with end-to-end encryption
  • gdbgui: Browser-based frontend to gdb (gnu debugger)
  • goormIDE: Run almost every programming languages with real-time collaboration, live pair programming, and built-in messenger.
  • FleetDeck: Remote desktop & virtual terminal
  • OpenSumi: A framework helps you quickly build Cloud or Desktop IDE products.
  • KubeSail: The Self-Hosting Company - uses xterm to allow users to exec into kubernetes pods and build github apps
  • WiTTY: Web-based interactive terminal emulator that allows users to easily record, share, and replay console sessions.
  • libv86 Terminal Forwarding: Peer-to-peer SSH for the web, using WebRTC via Bugout for data transfer and v86 for web-based virtualization.
  • hack.courses: Interactive Linux and command-line classes using xterm.js to expose a real terminal available for everyone.
  • Render: Platform-as-a-service for your apps, websites, and databases using xterm.js to provide a command prompt for user containers and for streaming build and runtime logs.
  • CloudTTY: A Friendly Kubernetes CloudShell (Web Terminal).
  • Go SSH Web Client: A simple SSH web client using Go, WebSocket and Xterm.js.
  • web3os: A decentralized operating system for the next web
  • Cratecode: Learn to program for free through interactive online lessons. Cratecode uses xterm.js to give users access to their own Linux environment.
  • Super Terminal: It is a http based terminal for developers who dont like repetition and save time.
  • graSSHopper: A simple SSH client with file explorer, history and many more features.
  • DomTerm: Tiles and tabs. Detachable sessions (like tmux). Remote connections using a nice ssh wrapper with predictive echo. Qt, Electron, Tauri/Wry, or desktop browser front-ends. Choose between xterm.js engine (faster) or native DomTerm (more functionality and graphics) - or both.
  • Cloudtutor.io: innovative online learning platform that offers users access to an interactive lab.
  • Helix Editor Playground: Online playground for the terminal based helix editor.
  • Coder: Self-Hosted Remote Development Environments
  • Wave Terminal: An open-source, ai-native, terminal built for seamless workflows.
  • eva: Eva is a web application for SSH remote login, developed in Go.
  • OpenSFTP: Super beautiful SSH and SFTP integrated workspace client.
  • balena: Balena is a full-stack solution for developing, deploying, updating, and troubleshooting IoT Edge devices. We use xterm.js to manage & debug devices on balenaCloud.
  • Filet Cloud: The lean and powerful personal cloud ā›….
  • pyTermTk: Python Terminal Toolkit - a Spiced Up Cross Compatible TUI Library šŸŒ¶ļø, use xterm.js for the HTML5 exporter.
  • ecmaOS: A kernel and suite of applications tying modern web technologies into a browser-based operating system.
  • LabEx: Interactive learning platform with hands-on labs and xterm.js-based online terminals, focused on learn-by-doing approach.
  • EmuDevz: A free coding game where players learn how to build an emulator from scratch.
  • And much more...

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.

License Agreement

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)