Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Last active August 21, 2026 12:01
Show Gist options
  • Select an option

  • Save ericboehs/a91dfee8b2199f0dff3f870c7334d4bb to your computer and use it in GitHub Desktop.

Select an option

Save ericboehs/a91dfee8b2199f0dff3f870c7334d4bb to your computer and use it in GitHub Desktop.
pi extension: hide the prompt cursor when the terminal pane loses focus (tmux focus-events), so it's obvious which split pi you're typing into
// Replace pi's fake reverse-video prompt cursor with the terminal's native
// hardware cursor.
//
// pi already emits CURSOR_MARKER at the correct editor position. Enabling the
// hardware cursor makes pi-tui position the terminal cursor at that marker;
// removing the fake reverse-video block prevents two cursors from overlapping.
// tmux and the terminal then provide their native focus behavior:
//
// - active tmux pane, focused terminal: normal hardware cursor
// - active tmux pane, unfocused terminal: terminal's inactive cursor style
// - inactive tmux pane: no cursor
import { CustomEditor, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
import type { KeybindingsManager } from "@earendil-works/pi-coding-agent";
import type { EditorTheme, TUI } from "@earendil-works/pi-tui";
// pi's fake cursor is reverse video around the grapheme under the cursor (or a
// space at end of input). CURSOR_MARKER precedes this sequence and is preserved.
const FAKE_CURSOR = /\x1b\[7m([\s\S]*?)\x1b\[0m/g;
class NativeCursorEditor extends CustomEditor {
private readonly hardwareCursorTui: TUI;
constructor(tui: TUI, theme: EditorTheme, keybindings: KeybindingsManager) {
super(tui, theme, keybindings);
this.hardwareCursorTui = tui;
tui.setShowHardwareCursor(true);
}
render(width: number): string[] {
// On /reload pi fires session_start (which reinstalls this editor and turns
// the hardware cursor on) and only afterwards runs applyRuntimeSettings(),
// which resets the hardware cursor to the settings default. That would leave
// no cursor at all: the fake reverse-video cursor is stripped below while the
// hardware cursor is off. Re-assert it every render so the native cursor
// survives a reload. setShowHardwareCursor early-returns when unchanged, so
// this is idempotent and only triggers a render on the frame it flips back on.
this.hardwareCursorTui.setShowHardwareCursor(true);
return super.render(width).map((line) => line.replace(FAKE_CURSOR, "$1"));
}
}
export default function (pi: ExtensionAPI) {
pi.on("session_start", (_event, ctx) => {
if (ctx.mode !== "tui") return;
ctx.ui.setEditorComponent(
(tui, theme, keybindings) => new NativeCursorEditor(tui, theme, keybindings),
);
});
}
@ericboehs

ericboehs commented Aug 20, 2026

Copy link
Copy Markdown
Author

cursor-focus — use the terminal's native cursor in pi

A pi extension that replaces pi's fake reverse-video prompt cursor with the terminal's real hardware cursor.

This gives native focus behavior without focus listeners or custom cursor glyphs:

  • Active tmux pane, focused terminal: normal terminal cursor
  • Active tmux pane, unfocused terminal: terminal's native inactive cursor style
  • Inactive tmux pane: no cursor

The problem

pi normally hides the hardware cursor and draws a fake cursor by wrapping the character under it in reverse video. That fake cursor uses pi's text color rather than the terminal's configured cursor color, and remains painted in inactive tmux panes.

What it does

  1. Enables pi-tui's hardware cursor with tui.setShowHardwareCursor(true).
  2. Removes pi's fake reverse-video cursor while preserving CURSOR_MARKER.
  3. Lets pi-tui position the real cursor at the editor marker.
  4. Leaves pane/window focus behavior to tmux and the terminal.

No terminal focus reporting or tmux configuration is required.

Installation

mkdir -p ~/.pi/agent/extensions
curl -fsSL https://gist.githubusercontent.com/ericboehs/a91dfee8b2199f0dff3f870c7334d4bb/raw/cursor-focus.ts \
  -o ~/.pi/agent/extensions/cursor-focus.ts

Restart pi after installation.

Requirements

  • pi in interactive TUI mode
  • A terminal that supports a visible hardware cursor (standard terminal behavior)

Notes

The extension uses pi's official ctx.ui.setEditorComponent and tui.setShowHardwareCursor() APIs. It does not patch node_modules.

@ericboehs

ericboehs commented Aug 21, 2026

Copy link
Copy Markdown
Author

Correction: the extension is still needed for tmux pane focus

Pi's built-in showHardwareCursor setting only makes the terminal cursor visible for IME positioning. By design, Pi continues rendering its fake reverse-video cursor as well.

That means an inactive tmux pane hides the hardware cursor, but the painted fake cursor remains visible. This extension is still required to strip the fake cursor and get native tmux/terminal focus behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment