Developer Tools Emacs 1 vues

Stop Using GUI Emacs! kitty-graphics.el Brings Images to Terminal

M
Mohamed IDBRAHIM
Auteur
Stop Using GUI Emacs! kitty-graphics.el Brings Images to Terminal

Stop Using GUI Emacs! kitty-graphics.el Brings Images to Terminal

What if everything you believed about terminal Emacs was wrong?

For decades, developers have accepted a brutal compromise: run Emacs in the terminal for speed and remote workflow purity, or surrender to GUI Emacs for the one thing that matters—seeing your images. Org-mode inline previews? Gone. PDF documents? Blind. LaTeX fragments? Cryptic source code forever. The terminal Emacs experience has been fundamentally crippled by this single missing capability.

Until now.

Imagine toggling inline images in org-mode with C-c C-x C-v, previewing LaTeX equations instantly, scrolling through PDFs with n and p, and browsing image directories in dired—all inside emacs -nw. No X11 forwarding. No sluggish GUI startup. No surrender.

This isn't science fiction. This is kitty-graphics.el, and it's about to obliterate the last excuse for avoiding terminal Emacs.


What is kitty-graphics.el?

kitty-graphics.el is an Emacs Lisp package created by cashmere that renders images directly inside terminal Emacs using either the Kitty graphics protocol or Sixel. Released on GitHub at cashmeredev/kitty-graphics.el, this package solves a problem that has plagued terminal Emacs users for years: the inability to display visual content without launching a graphical frame.

The project's origin story is fascinating. The author, after subscribing to Claude's highest tier, used AI assistance to explore whether libvterm could be extended with the Kitty image protocol—an idea they'd contemplated for months. The result bypasses vterm entirely, instead integrating directly with Emacs's overlay display engine for a cleaner, more native solution.

What's remarkable is the automatic backend dispatch. The package requires zero configuration for terminal detection—it probes for Kitty support first (via the KITTY_PID environment variable), then falls back to Sixel via a DA1 device attributes query. The mode-line displays [K], [S], or [?] to indicate which backend is active. This means 20+ terminals work out of the box, from Kitty and WezTerm to foot, Konsole, xterm, mlterm, mintty, and even VS Code Terminal.

The package is trending now because it arrives at a perfect convergence: terminal emulators have finally matured their graphics protocols, remote development via SSH and tmux is ubiquitous, and developers are increasingly rejecting GUI bloat in favor of keyboard-driven, low-latency workflows.


Key Features That Change Everything

Dual-Protocol Architecture with Zero Configuration

The backend selection happens automatically at startup. Kitty protocol gets priority for its truecolor support and stateful image transmission—send once, place many times. Sixel provides broad compatibility but requires re-emission on scroll since it's stateless. You can override with kitty-gfx-preferred-protocol if needed.

Native Emacs Overlay Integration

Unlike external hacks, images integrate with Emacs's display property system. They scroll with text, survive buffer switches, work in split windows, and respect Emacs's redisplay cycle. Position caching eliminates redundant re-placement when nothing moves, and synchronized output wrapping (BSU/ESU, DEC mode 2026) prevents the flickering that plagues lesser terminal image solutions.

Precision Cell-Based Scaling

The package queries actual cell pixel dimensions via CSI 16 t (XTWINOPS) for accurate image sizing. No more guessing at font metrics—images fit your terminal's actual geometry. Fallback to 8x16 ensures functionality even on quirky terminals.

Comprehensive Major Mode Integrations

The package doesn't just display raw images—it hooks into Emacs's entire visual ecosystem:

Integration Trigger What It Does
org-mode C-c C-x C-v Toggle inline images in documents
org LaTeX C-c C-x C-l Render LaTeX fragments as images
doc-view Open PDF Full PDF viewer with navigation and zoom
image-mode Open image file Terminal-aware image file viewing
shr/eww Browse HTML Inline images in eww, mu4e, gnus
dired P key Side-window image preview
dirvish Automatic Native preview dispatcher

Performance Optimizations for Real Work

The Kitty backend transmits image data once via APC escape sequences (a=t, store only), then positions with direct placements (a=p) using unique placement IDs (p=PID). The Sixel backend leverages img2sixel from libsixel for 2.2x smaller payloads and ~10x faster encoding than ImageMagick—critical in tmux where every refresh re-emits the full DCS payload.


Use Cases Where kitty-graphics.el Dominates

Remote Server Development Over SSH

You're SSH'd into a beefy server, editing documentation with org-mode. Previously, emacs -nw meant losing all inline images—diagrams, screenshots, architecture overviews. With kitty-graphics.el, C-c C-x C-v reveals every image instantly. No X11 forwarding latency. No ssh -Y security exposure. Pure terminal efficiency with full visual fidelity.

Academic Writing with LaTeX Preview

Writing papers in org-mode with embedded LaTeX? The C-c C-x C-l command renders mathematical fragments as crisp images. Verify your equations visually without compiling the entire document. For researchers on shared cluster login nodes where GUI Emacs is impossible, this is transformative.

Email and RSS with Image Content

Using mu4e for email or gnus for news? HTML messages with embedded images render inline via the shr/eww integration. No more context-switching to a browser to view newsletters, screenshots, or diagrams sent by colleagues.

Document Review and Technical Reading

The doc-view integration turns terminal Emacs into a genuine PDF reader. Navigate pages with n/p, zoom with +/-/0. For developers reviewing specifications, datasheets, or academic papers in constrained environments (containers, minimal VMs, remote workstations), this eliminates the need for separate PDF tools.


Step-by-Step Installation & Setup Guide

Prerequisites Check

First, verify your Emacs version:

emacs --version  # Must be >= 27.1

Install required system dependencies:

# For Sixel backend (strongly recommended: img2sixel)
# On Nix:
nix profile install nixpkgs#libsixel

# On Debian/Ubuntu:
sudo apt-get install libsixel-bin

# Fallback: ImageMagick (slower, causes flicker in tmux)
sudo apt-get install imagemagick

# For LaTeX preview
texlive-full  # or minimal with dvipng

# For doc-view PDF support
sudo apt-get install ghostscript

Critical terminal launch configuration:

# Emacs often fails to find xterm-kitty terminfo
TERM=xterm-256color emacs -nw

# Or with emacsclient:
TERM=xterm-256color emacsclient -nw

Installation via straight.el

Add to your Emacs configuration:

(use-package kitty-graphics
  :straight (:host github :repo "cashmeredev/kitty-graphics.el")
  :if (not (display-graphic-p))  ; Only load in terminal Emacs
  :config
  (kitty-graphics-mode 1))       ; Enable globally

Manual Installation

;; Add local clone to load path
(add-to-list 'load-path "~/projects/kitty-graphics")

;; Load the package
(require 'kitty-graphics)

;; Enable only when running in terminal
(when (not (display-graphic-p))
  (kitty-graphics-mode 1))

Verification

After restarting Emacs in terminal mode, check your mode-line for [K] (Kitty backend), [S] (Sixel backend), or [?] (no backend detected). Enable debug logging if troubleshooting:

(setq kitty-gfx-debug t)  ; Logs to /tmp/kitty-gfx.log

REAL Code Examples from the Repository

Example 1: Basic Global Mode Activation

The simplest entry point—enable the global minor mode:

(kitty-graphics-mode 1)

This single command activates the entire system. Behind the scenes, it triggers the backend dispatch: first checking KITTY_PID for Kitty protocol support, then sending a DA1 query to probe for Sixel. The mode-line indicator updates to show which protocol is active. All overlay hooks are installed, and integrations for org-mode, doc-view, image-mode, and others become live.

When to use: Place this in your terminal Emacs startup configuration. The :if (not (display-graphic-p)) guard in the use-package declaration prevents loading in GUI Emacs, where it's unnecessary.

Example 2: straight.el Installation with Conditional Loading

From the README, the recommended modern Emacs installation:

(use-package kitty-graphics
  :straight (:local-repo "~/projects/kitty-graphics")
  :if (not (display-graphic-p))
  :config
  (kitty-graphics-mode 1))

This use-package declaration demonstrates several best practices. The :straight keyword fetches from a local repository path—substitute (:host github :repo "cashmeredev/kitty-graphics.el") for direct GitHub installation. The :if condition prevents byte-compilation and loading in GUI sessions, keeping startup fast. The :config block runs after successful load, ensuring kitty-graphics-mode only activates when the package is actually available.

Pro tip: For doom-emacs or similar frameworks, wrap in (after! org ...) to ensure org-mode integration hooks are properly sequenced.

Example 3: Manual Installation for Custom Setups

For developers with non-standard Emacs configurations:

(add-to-list 'load-path "~/projects/kitty-graphics")
(require 'kitty-graphics)
(when (not (display-graphic-p))
  (kitty-graphics-mode 1))

This explicit approach reveals the package's minimal dependencies. No complex autoloads, no mandatory package manager. The add-to-list prepends your local path, require loads the feature, and the when guard ensures terminal-only activation. This pattern is ideal for literate configurations where you want explicit control over load order, or for embedding in custom Emacs distributions.

Note the path: The README uses ~/projects/kitty-graphics without the .el suffix—this refers to a directory containing the repository contents, not a single file.

Example 4: Critical Environment Variable for Terminal Launch

The README emphasizes this shell command:

TERM=xterm-256color emacsclient -nw

This isn't merely a suggestion—it's essential for correct operation. Emacs's terminal detection relies on terminfo databases. The xterm-kitty entry often doesn't exist in system terminfo databases, causing Emacs to fall back to dumb terminal mode with severely degraded functionality. By forcing TERM=xterm-256color, you guarantee:

  • 256 color support detection
  • Correct escape sequence generation
  • Proper cursor addressing
  • Functional CSI 16 t XTWINOPS queries for cell size

Without this, kitty-graphics.el's cell pixel queries fail, images scale incorrectly, and the entire display system degrades. Add this to your shell aliases immediately:

alias emacst='TERM=xterm-256color emacs -nw'
alias emacsc='TERM=xterm-256color emacsclient -nw'

Advanced Usage & Best Practices

Optimize Sixel Performance in tmux

The Sixel backend's Achilles heel is tmux's cell buffer, which isn't pixel-aware. Images may stick to old positions after scrolling—this is an upstream tmux limitation. Mitigate by:

  • Using Kitty protocol outside tmux when possible
  • Setting kitty-gfx-tmux-allow-sixel to nil to disable problematic Sixel in tmux
  • Prioritizing img2sixel over ImageMagick for 10x faster encoding

Tune Image Dimensions for Your Workflow

(setq kitty-gfx-max-width 80   ; Narrower for side-by-side editing
      kitty-gfx-max-height 30  ; Shorter for laptop screens
      kitty-gfx-render-delay 0.05)  ; Slower debounce for remote connections

Protocol Override for Testing

Force a specific backend for debugging or terminal compatibility:

(setq kitty-gfx-preferred-protocol 'sixel)  ; or 'kitty, 'auto

Debug Production Issues

When images silently fail, enable debug logging:

(setq kitty-gfx-debug t)
;; Then check /tmp/kitty-gfx.log for "no encoder found" or backend selection details

Comparison with Alternatives

Feature kitty-graphics.el iimage-mode org-inline-pdf Terminal + External Viewer
Works in emacs -nw ✅ Yes ❌ No ❌ No N/A
Backend protocols Kitty + Sixel N/A N/A N/A
Terminal auto-detection ✅ Yes N/A N/A N/A
Org-mode integration ✅ Native ⚠️ Limited ❌ No ❌ No
LaTeX preview ✅ Built-in ❌ No ❌ No ❌ No
PDF viewing ✅ doc-view ❌ No ⚠️ Only PDF ✅ External
Image scrolling with text ✅ Yes ❌ No ❌ No N/A
Buffer switch survival ✅ Yes ❌ No ❌ No N/A
Split window support ✅ Yes ❌ No ❌ No N/A
Remote/SSH friendly ✅ Yes ❌ No ❌ No ⚠️ Varies

Why kitty-graphics.el wins: It's the only solution that brings genuine image rendering into terminal Emacs with native integration across multiple major modes. External viewers break workflow continuity. GUI Emacs sacrifices remote efficiency. This package finally eliminates the false choice.


FAQ

Does kitty-graphics.el work in tmux?

Partially. Sixel works in tmux >= 3.4 with native sixel rendering, but images may stick to old positions after scrolling due to tmux's cell buffer limitations. Kitty graphics passthrough inside tmux remains unimplemented. Set kitty-gfx-tmux-allow-sixel to nil to disable if problematic.

Why are my images not appearing at all?

Most commonly: no Sixel encoder on PATH. Install img2sixel (strongly recommended) or ImageMagick. Check /tmp/kitty-gfx.log with kitty-gfx-debug enabled. Also verify you're launching with TERM=xterm-256color.

Can I use this with animated GIFs?

Only the first frame displays. This is a current limitation of both backends. For full animation, consider external viewers or GUI Emacs.

Does this work with Emacs 28/29/30?

Requires Emacs >= 27.1. All newer versions are fully supported. The package uses stable overlay APIs that haven't changed significantly.

Is the Kitty protocol better than Sixel?

Yes, when available. Kitty provides truecolor versus Sixel's 256 colors, stateful image transmission (send once, place many) versus stateless re-emission, and generally smoother performance. Sixel wins on terminal compatibility.

Will this slow down my Emacs?

Minimal impact. Position caching skips redundant re-placement. The default 0.016s render delay debounces rapid changes. Sixel in tmux with large images is the main performance concern—use img2sixel and consider kitty-gfx-max-width limits.

Can I contribute or report issues?

The repository is at cashmeredev/kitty-graphics.el. The author encourages judgment on project merits despite AI-assisted development.


Conclusion

Terminal Emacs has finally shed its last major limitation. kitty-graphics.el delivers what once seemed impossible: rich image rendering inside emacs -nw with zero configuration, broad terminal compatibility, and deep integration across org-mode, doc-view, dired, and beyond.

The technical execution is impressive—automatic protocol dispatch, precise cell-based scaling, overlay-native integration, and performance optimizations for real-world conditions like tmux and remote connections. Whether you're writing documentation with inline images, reviewing PDFs on remote servers, or simply refusing GUI bloat on principle, this package removes every excuse.

My assessment? This is a must-have for terminal Emacs devotees. The project demonstrates that AI-assisted development, when directed by genuine curiosity and technical rigor, produces tools that solve real problems elegantly.

Stop compromising. Stop switching to GUI Emacs for images. Install kitty-graphics.el today and experience terminal Emacs as it was always meant to be.

👉 Get it now: github.com/cashmeredev/kitty-graphics.el

Star the repository, test it in your workflow, and join the growing community of developers who've finally reclaimed their terminal.


Last updated: 2024 | Terminal: liberated.

Commentaires 0

Aucun commentaire pour l'instant. Soyez le premier à réagir !

Laisser un commentaire