Skip to content

Editing Ghostty's config without losing a byte

A Rust TUI for trying Ghostty themes taught me every way a small config edit can quietly destroy colours, symlinks and prompts, and how to ship it to brew and crates.io in one command.

▸▸ live preview · Enter applies it~/.config/ghostty/config# coloursbackground = #1E1E2Efont-size = 14background = #24273Atheme = dark:…,light:…last one wins · the rest kept byte for byteFIG. 1 · EVERY THEME, PREVIEWED FIRSTdrawn for this post
themes and the config · drawn for this postfig. 1

Ghostty is configured with a text file. One key, one value, one line. That is a good design, right up until you want to try forty themes in a row, because each attempt is: open the file, change a line, save, reload, squint, repeat. Ghostty ships a theme preview, but previewing is not the same as living with it, and nothing manages the settings around a theme, or the Starship prompt that has to match.

So I wrote hauntty: a keyboard-driven terminal app that lists every theme with a live preview, applies one on Enter, and does the same for common settings and Starship presets. It is nearly ten thousand lines of Rust. Far too many of those lines exist because of what follows.

A terminal UI with a filtered list of TokyoNight themes on the left, a live preview with the 16-colour palette, a sample prompt and some highlighted Rust on the right, and a status bar reading Saved your colors as My Saved Theme, applied TokyoNight Moon.
Filter, arrow, Enter. The status line is doing more work than it looks.

The rule

A tool that edits someone else’s config has one job above all others: never lose anything. Not a comment, not a blank line, not a colour they set by hand three years ago and forgot about. Everything below is a way I found to break that rule.

The parser is lossless. It reads the file into a list of lines, each tagged as a key-value pair, a comment, or blank, and only the lines hauntty manages are ever rewritten. Everything else is emitted byte for byte. Before any write, the current file is copied to a timestamped backup. Writes go to a temporary file first and are renamed into place. That is the easy part.

Last one wins, and I was keeping the first

Ghostty resolves a repeated key by taking the last occurrence. My code, when it saved a backup of your current colours before applying a theme, looked up each key and took the first. If your file said background = #000000 near the top and background = #1a1b26 further down, Ghostty showed the second and hauntty backed up the first. Then it deleted both lines and wrote the new theme. Your real background was gone.

There was a second version of the same bug. The backup only kept values it could parse as six-digit hex. Ghostty accepts more than that: X11 colour names, cell-foreground, palette indices up to 255. Anything hauntty could not parse was silently dropped from the backup, while the line itself was still removed from the config.

The theme line that is really two themes

Ghostty allows theme = dark:TokyoNight Moon,light:TokyoNight Day. The terminal picks one depending on the system appearance. My parser read that as a theme called dark:TokyoNight Moon,light:TokyoNight Day, found no such theme, concluded you had no base theme, and backed up only your overrides. Then it reported success and deleted the line.

The same mis-parse later made the colour customiser preview the wrong colours, because it too thought there was no base theme. Both paths now share one function that resolves the active theme names for the current appearance, and if hauntty cannot work out what your current look is, it refuses to apply rather than guessing.

The Starship preset that did not exist yet

The Starship tab fetches presets from Starship’s own repository. Presets on the main branch are written against whatever Starship is about to release. One day catppuccin-powerline gained a [jj_bookmark] section. The installed Starship did not know that key. Applying the preset produced an error on every single prompt until you fixed it by hand.

The fix was to run starship --version, turn that into a tag, and fetch the preset listing from that tag rather than from main:

pub fn starship_ref_from_version(version_line: &str) -> Option<String> {
    version_line.split_whitespace().find_map(|token| {
        let end = token
            .find(|c: char| !c.is_ascii_digit() && c != '.')
            .unwrap_or(token.len());
        let version = &token[..end];
        let parts: Vec<&str> = version.split('.').collect();
        let is_semver = parts.len() == 3
            && parts
                .iter()
                .all(|p| !p.is_empty() && p.chars().all(|c| c.is_ascii_digit()));
        is_semver.then(|| format!("v{version}"))
    })
}

My first version of this fell back to main if the tag returned a 404. I later removed the fallback. A missing tag means something is wrong, and the right answer is to say so, not to quietly serve a preset that might break the prompt.

Half a theme file

Saving a custom theme used create_new, which claims the filename and then writes into it. Kill the process between those two steps, which a laptop lid will happily do, and you are left with an empty file. The next save refuses because the name is taken, and the theme list shows a colourless theme that cannot be applied.

The replacement writes the whole file under a temporary name first, then creates a hard link to the final name. link(2) fails with EEXIST if the name exists, even as a dangling symlink, so the claim and the content arrive together or not at all:

pub fn save_atomic_new(&self, path: &Path) -> std::io::Result<()> {
    let tmp = self.write_tmp(path)?;
    let claimed = match std::fs::hard_link(&tmp, path) {
        Err(e) if e.kind() != std::io::ErrorKind::AlreadyExists => {
            claim_then_rename(&tmp, path)
        }
        linked => linked,
    };
    let _ = std::fs::remove_file(&tmp);
    claimed
}

Related: the atomic rename that protects the main config file was also, for anyone who manages their dotfiles with a symlink farm, replacing the symlink with a plain file. Their dotfiles repository stopped tracking Ghostty without telling them. The path is now canonicalised before the temp-and-rename dance, so the write lands where the symlink points.

Things that were not bugs in hauntty

Two reports were real problems with no code fix. Changing the cursor style “did nothing”, because Ghostty’s shell integration forces a bar cursor at the prompt unless you set shell-integration-features = no-cursor. And on a Mac, characters typed with Option held were being dropped from text fields, because my key guard rejected anything with a modifier other than Shift. Option is how you type accented characters. The guard now rejects only Control and Super.

The honest answer to the first one is a toast that explains it. The honest answer to the second was embarrassment.

Shipping it

Releasing is one command. The script bumps the version and refuses to bump a patch from a release candidate, because 1.1.0-rc.3 sorts before 1.1.0 and Cargo will not let you go backwards. Then it tags, pushes, waits for CI to build four archives, writes the real checksums into the Homebrew formula, pushes the tap, and publishes to crates.io.

A flow from release.sh through CI building four targets to the Homebrew tap and crates.io, with a list of three toolchain-caused release failures underneath.
Four archives, two registries, one command. The list at the bottom is why the command keeps growing.

Three times the release broke without a line of application code changing. A newer clippy learnt a new lint, and the script denies warnings, so the release stopped. The declared minimum Rust version was 1.80, but a dependency had moved to 1.88, so the minimum was fiction until CI started checking it. And cargo publish honours a registry.default setting, which means a stray line in a config file could have pushed the crate somewhere other than crates.io. It now passes --registry crates-io explicitly and checks the crate’s owners before it does anything.

Where it is

Version 1.1.0, with an update check and a layout that stacks the preview under the list when the terminal is narrower than 72 columns. brew install winterweken/tap/hauntty on a Mac or Linux box, or cargo install hauntty. The repository is at github.com/winterweken/hauntty, including the demo tape the screenshots came from.

If you take one thing from this: a config editor is a backup tool first and an editor second. Get the backup right, and the editing can be as clever as you like.