Link Hints for xterm

Claude Code lives in my xterm. All day it prints links: a PR, an issue, a doc page. And file paths: components/mcp/CLAUDE.md:42, deps.edn:12:3.

And then I sat there with my hands on the keyboard and did one of two silly things. I reached for the mouse to select and copy. Or I typed "open that file in Emacs" or "open that website in the browser" and waited for Claude to do it for me.

An LLM agent as a very expensive mouse.

In Emacs I never have this problem. There I have link-hint.el: press a key, every visible link gets a little label, type the label, the link opens. 1 Keyboard-only, no aiming, no thinking. So I wanted that, in xterm.

What it is now

Ctrl+Shift+L in any xterm. A dmenu pops up with everything clickable on the screen, each with a label:

menu.png

Type d and that line jumps to the top:

menu-filtered.png

  • label + Enter opens it. URLs go to qutebrowser, files go to Emacs (emacsclient), at the right line and column.
  • label + Shift+Enter copies it. On my desktop it opens the verb menu instead, see below.

The labels are home row first (a s d f …), and the bottom of the screen gets the first ones, because the thing I want is almost always the thing that was just printed.

Only file paths that exist are offered. That is the whole trick against false positives: e.g., 3.14, v1.2 all look a bit like file names, but none of them are on disk. Relative paths are resolved against the directory of the program in the foreground of that terminal, so when Claude Code prints src/foo.clj:12, it means its src.

Half setup, half Claude Code

The funny part: Claude Code built it. I asked for "link-hint.el, but in xterm", and the session went like this.

xterm has no plugin system, so where do you even hook in? The answer is old and a bit absurd: the printer. xterm can "print" the screen, which means piping its text to a shell command of your choice. print() takes flags, and noNewLine glues wrapped lines back together, so a long URL that wraps over two screen lines comes out in one piece. 2 So the whole setup is two lines in .Xresources:

XTerm*printerCommand: xterm-link-hint
XTerm.vt100.translations: #override \n\
    Ctrl Shift <Key>L:  print(noAttrs,noNewLine,FormFeed)

The printer command is a bash script with a bit of Perl that finds the URLs and paths, labels them, and asks dmenu. Claude did not just trust the man page here. It started a hidden xterm with a logging "printer" and watched what really arrives: a fresh process per print, and the page ends with a form feed. Then it wrote the reader to match.

The second round was mine: "the delay, I don't like it". It measured every step instead of guessing:

step time
key press → text arrives at the script 30–60 ms
script finds links, starts the menu < 30 ms
menu on screen ~50 ms
emacsclient opens a file ~2 ms
a new qutebrowser process, imports only ~230 ms

So the menu was fine. Opening a URL was the slow part: xdg-open starts a whole new qutebrowser, which loads a lot of Python just to hand the URL to the qutebrowser that is already running. Now the script writes the URL directly into that running qutebrowser's IPC socket. 3 That takes about 45 ms. 4

It started with rofi, which can pick the moment a label is unique. I switched it to dmenu, the one I use everywhere else. That costs one Enter. I'll take that for having one menu on my desktop, not two.

Why this is good

It is the same move as Sources × Verbs: the things on my screen are nouns, and I want verbs on them without a mouse. Here the source is "whatever the terminal shows". Enter runs the default verb, open. Shift+Enter hands the target with its type (url or file, plus the line) to act, and then every verb I have is there: copy, send to Claude, capture into my notes, search. Default action plus a menu of the rest, the way Embark has embark-dwim and embark-act.

And there is a nice loop in it. The agent that produces most of the links in my terminal also built the tool that lets me follow them without asking the agent.

Open questions:

  • A source for pick? It could be one: "the last xterm screen" as a source. But a link hint wants to be one keypress from the terminal, not two menus deep.
  • Scrollback? Right now only the visible screen counts, like link-hint.el. print-everything() would give the whole history, but with 50,000 lines of scrollback the menu would become a search, not a hint.
  • Real overlays? link-hint.el draws the labels on the links. In xterm that is not possible from the outside. Is a list good enough, or do I miss the spatial thing?

Copy it

Repo: github.com/benjamin-asdf/xterm-link-hint. Or copy from here: save this as xterm-link-hint somewhere on your PATH, chmod +x it,

#!/usr/bin/env bash
# xterm printerCommand: reads screen dumps ended by a form feed from stdin and
# offers the URLs and existing file paths on them with hint labels in dmenu.
# Label + Enter opens the target (URL in qutebrowser, file:line in emacsclient),
# label + Shift+Enter copies it to the clipboard, or runs XTERM_LINK_HINT_ACT.
#
# XTERM_LINK_HINT_MENU replaces the menu command (default "dmenu -l 20").
# XTERM_LINK_HINT_ACT, when set, is run as "CMD -t TYPE" (TYPE url or file)
# with the target on stdin and PICK_LINE/PICK_COL in the environment.
#
# .Xresources:
#   XTerm*printerCommand: xterm-link-hint
#   XTerm.vt100.translations: ... Ctrl Shift <Key>L: print(noAttrs,noNewLine,FormFeed)

KEYS=(a s d f g h j k l q w e r t y u i o p z x c v b n m)

labels() {
    local n=$1 i j
    if ((n <= ${#KEYS[@]})); then
        printf '%s\n' "${KEYS[@]:0:n}"
    else
        for i in "${KEYS[@]}"; do for j in "${KEYS[@]}"; do echo "$i$j"; done; done | head -n "$n"
    fi
}

xterm_pid() {
    local pid=$$
    while ((pid > 1)); do
        [[ $(cat /proc/$pid/comm 2>/dev/null) == xterm ]] && { echo "$pid"; return; }
        pid=$(awk '{print $4}' /proc/$pid/stat 2>/dev/null) || return
    done
}

terminal_cwd() {
    local xt shell fg
    xt=$(xterm_pid) && [[ -n $xt ]] || { pwd; return; }
    shell=$(ps -o pid=,tty= --ppid "$xt" | awk '$2 != "?" {print $1; exit}')
    [[ -n $shell ]] || { pwd; return; }
    fg=$(awk '{print $8}' /proc/"$shell"/stat 2>/dev/null)
    readlink /proc/"$fg"/cwd 2>/dev/null || readlink /proc/"$shell"/cwd 2>/dev/null || pwd
}

# Prints "url US url US US US shown" and "file US path US line US col US shown" (US = \x1f),
# bottom of the screen first, duplicates dropped.
targets() {
    CWD=$1 perl -e '
        my $cwd = $ENV{CWD};
        my $url_re = qr{(?:https?|ftp|file)://[^\s<>"`{}|\\^\[\]]+};
        my @lines = split /\n/, do { local $/; <STDIN> };
        my %seen;
        sub resolve {
            my ($p) = @_;
            $p =~ s{^~(?=/|$)}{$ENV{HOME}};
            my @try = ($p);
            push @try, $1 if $p =~ m{^[ab]/(.+)};
            for my $t (@try) {
                my $abs = $t =~ m{^/} ? $t : "$cwd/$t";
                return $abs if -e $abs;
            }
            return;
        }
        for my $line (reverse @lines) {
            my @found;
            while ($line =~ m{$url_re}g) {
                my ($u, $pos) = ($&, $-[0]);
                1 while $u =~ s/[.,;:!?\x27>]+$//
                    or ($u =~ /\)$/ and ($u =~ tr/(//) < ($u =~ tr/)//) and chop $u);
                push @found, [$pos, "url\x1f$u\x1f\x1f\x1f$u", $u];
            }
            (my $rest = $line) =~ s{$url_re}{" " x length $&}ge;
            while ($rest =~ m{(?<![\w/.~\@+-])((?:~|\.{1,2})?/?[\w.\@+-]+(?:/[\w.\@+-]*)*)(?::(\d+))?(?::(\d+))?}g) {
                my ($p, $l, $c, $pos, $shown) = ($1, $2 // "", $3 // "", $-[0], $&);
                next unless $p =~ m{[/.]} and $p =~ /\w/;
                my $abs = resolve($p);
                if (!defined $abs and $p =~ s/[.,;:]+$//) { $abs = resolve($p); $shown =~ s/[.,;:]+$// }
                next unless defined $abs;
                push @found, [$pos, "file\x1f$abs\x1f$l\x1f$c\x1f$shown", "$abs:$l"];
            }
            for my $f (sort { $a->[0] <=> $b->[0] } @found) {
                print "$f->[1]\n" unless $seen{$f->[2]}++;
            }
        }'
}

qutebrowser_open() {
    local sock
    sock=$(ls "${XDG_RUNTIME_DIR:-/run/user/$UID}"/qutebrowser/ipc-* 2>/dev/null | head -1)
    [[ -S $sock ]] && python3 -I -c '
import json, socket, sys
s = socket.socket(socket.AF_UNIX)
s.connect(sys.argv[1])
s.sendall((json.dumps({"args": [sys.argv[2]], "target_arg": None, "protocol_version": 1}) + "\n").encode())
s.close()' "$sock" "$1" 2>/dev/null
}

open_url() {
    qutebrowser_open "$1" || setsid -f xdg-open "$1" >/dev/null 2>&1
}

elisp_string() {
    local s=${1//\\/\\\\}
    printf '"%s"' "${s//\"/\\\"}"
}

emacs_focus() {
    emacsclient -n --eval '(select-frame-set-input-focus (selected-frame))' >/dev/null 2>&1
}

# Text and directories go to Emacs, other files to their xdg default
# application, files without one to dired with point on them.
open_file() {
    local path=$1 line=$2 col=$3 mime
    if [[ -d $path || ! -s $path || $(file -Lb --mime-encoding -- "$path") != binary ]]; then
        if [[ -n $line ]]; then
            emacsclient -n "+$line${col:+:$col}" "$path"
        else
            emacsclient -n "$path"
        fi >/dev/null 2>&1
        emacs_focus
        return
    fi
    mime=$(file -Lb --mime-type -- "$path")
    if [[ -n $(xdg-mime query default "$mime" 2>/dev/null) ]]; then
        setsid -f xdg-open "$path" >/dev/null 2>&1
    else
        emacsclient -n --eval "(dired-jump nil $(elisp_string "$path"))" >/dev/null 2>&1
        emacs_focus
    fi
}

act_on() {
    local kind=$1 target=$2 line=$3 col=$4
    if [[ -n ${XTERM_LINK_HINT_ACT:-} ]]; then
        printf '%s' "$target" | PICK_LINE=$line PICK_COL=$col bash -c "$XTERM_LINK_HINT_ACT -t $kind"
        return
    fi
    [[ $kind == file && -n $line ]] && target="$target:$line"
    printf '%s' "$target" | xclip -selection clipboard
    notify-send -t 1500 "copied" "$target" 2>/dev/null
}

pick() {
    local page=$1 rows labs n choice label idx kind target line col
    mapfile -t rows < <(printf '%s' "$page" | targets "$(terminal_cwd)")
    n=${#rows[@]}
    ((n == 0)) && { notify-send -t 1500 "xterm-link-hint" "no links on screen" 2>/dev/null; return; }
    mapfile -t labs < <(labels "$n")
    choice=$(paste -d' ' <(printf '%s\n' "${labs[@]}") <(printf '%s\n' "${rows[@]}" | awk -F'\x1f' '{print $NF}') |
        bash -c "${XTERM_LINK_HINT_MENU:-dmenu -l 20} -p link") || return
    label=${choice%% *}
    for idx in "${!labs[@]}"; do [[ ${labs[$idx]} == "$label" ]] && break; done
    [[ ${labs[$idx]} == "$label" ]] || return
    IFS=$'\x1f' read -r kind target line col _ <<< "${rows[$idx]}"
    [[ -z $target ]] && return
    if [[ $choice != *" "* ]]; then
        act_on "$kind" "$target" "$line" "$col"
    elif [[ $kind == url ]]; then
        open_url "$target"
    else
        open_file "$target" "$line" "$col"
    fi
}

while IFS= read -r -d $'\f' page || [[ -n $page ]]; do
    pick "$page" &
done

add this to ~/.Xresources,

XTerm*printerCommand: xterm-link-hint
XTerm.vt100.translations: #override \n\
    Ctrl Shift <Key>L:  print(noAttrs,noNewLine,FormFeed)

then xrdb -merge ~/.Xresources and open a new xterm. It needs bash, perl, dmenu and xclip. Another menu: XTerm*printerCommand: env XTERM_LINK_HINT_MENU'rofi -dmenu' xterm-link-hint=.

Footnotes:

1

link-hint.el by noctuid describes itself as "Use avy to open, copy, etc. visible links". It labels the visible links with avy and runs an action on the one you pick. https://github.com/noctuid/link-hint.el

2

From the xterm man page, print(printer-flags): "noNewLine: no newline will be sent at the end of the last line printed, and wrapped lines will be combined into long lines". The text goes to the program in the printerCommand resource. print-everything() does the same for the whole scrollback. https://invisible-island.net/xterm/manpage/xterm.html

3

qutebrowser listens on a local socket ($XDG_RUNTIME_DIR/qutebrowser/ipc-<md5 of the user name>). A second qutebrowser URL call only connects to it and sends one line of JSON, {"args": [url], "target_arg": null, "protocol_version": 1}. See qutebrowser/misc/ipc.py in https://github.com/qutebrowser/qutebrowser

4

Timings measured on an Intel Core i5-14600, Arch Linux, kernel 7.2, X11 with StumpWM, xterm 411, dmenu 5.4.


Generated by Claude on Benjamin Schwerdtner's behalf.

Date: 2026-11-28 Sat 10:00

Email: Benjamin.Schwerdtner@gmail.com

About
Contact